0

I have a function that works with std::vector, declared like this:

void MyFunc(std::vector<std::wstring> &vFillArray){
//....
//fill the vector with wstrings
//....
}

Now I would like to export my function in a DLL library and make it available to a vb 6 application. Using the library as it is, would crash the vb 6 application. I have to change the declaration to LPWSTR** (or wchar_t** I think) but now, how to reconvert this type internally in my c++ function? Internally I use std::vector to fill the vector with string. Any advice?

paul
  • 19
  • 3
  • 1
    Are you sure about the LPWSTR? Last time I wrote a DLL to be used by VB, I had to use BSTR. – Karsten Koop Apr 10 '18 at 13:58
  • `LPWSTR` is (under windows) `wchar_t *`, so `LPWSTR *` would be `wchar_t **`. Asking for conversion to/from `LPWSTR **` doesn't make sense, since the types are incompatible, unless you also specify WHY the function you're calling has an additional `*` in its argument type. If you actually need a `LPWSTR *`, I'll offer a hint - both `std::vector` and `std::wstring` elements are contiguous (guaranteed C++11 & later, not guaranteed before C++11 but usually the case) but both also dynamically allocate memory for their elements so the `wchar_t`s in a `vector` are NOT contiguous. – Peter Apr 10 '18 at 14:31
  • every allocating techniques in cross-process memory (DLL too) is difficult/prohibited. You can use Microsoft COM API, but is very hard – Jacek Cz Apr 10 '18 at 14:34
  • @KarstenKoop No, i'm not sure. I'm sure that for normal wstring I had to use LPWSTR and vb 6 stop to crash: before -> std::wstring after -> LPWSTR Works as expected I think (but I'm a beginner in c++ i'm not sure) that for std::vector I need to use LPWSTR** or WCHAR_T** I can test later if I have to user * or **. Now I would like to know how to reconvert it internally from LPWSTR data type to std::vector – paul Apr 10 '18 at 15:40
  • @Peter I need to use some data type compatible with vb 6 (i think LPWSTR** is what I need). Then I need to convert it to std::vector beacuse my function in c++ use vector of wstring – paul Apr 10 '18 at 15:45
  • @JacekCz if you know a better way to do it I'm open-minded. I need to create an empty string array in vb 6, pass to the function in external dll library. Then, the function fill the array and then vb 6 show the result. I'm already able to do it with other data type (int, array of int, wstring) but not vector of wstring. I'm almost sure some Windows API do the same.. – paul Apr 10 '18 at 16:03
  • @paul - jst telling us you "think" you need a `LPWSTR **` for compatibility with vb6 is insufficient. That leaves out a lot of information that is critical if you expect help. You need to read documentation for on interfacing vb6 to C (or C++) on what your vb6 code ASSUMES about the argument passed to it (e.g. layout of string data in memory in a way that does not rely on knowledge of vb6). You're assuming you've provided enough information for someone to answer your question. You haven't. I'm voting to close the question, accordingly. – Peter Apr 11 '18 at 08:59
  • If you want to work with VB use a SAFEARRAY of BSTR. – Ben Apr 11 '18 at 10:03
  • @Peter, you're right I was in a hurry and I answered badly thinking that you would have understood.I wrote LPWSTR** by mistake. I meant to write LPWSTR*. However, I could not correctly pass strings arrays from vb6 using as input parameters LPWSTR* or wchar_t** and handle them correctly in C++. In the end I solved using an array of BSTR as function parameter because array of strings in vb are SAFEARRAY and are not implicitly converted like normal string. There is too short space in the comments to explain everything exhaustively but thanks to all I finally managed to do as I wanted – paul Apr 11 '18 at 21:31

1 Answers1

0

Assuming LPWSTR is the same as wchar_t, you can use the usual iterator-assignment constructors and functions of std::vector:

wchar_t const *raw_data[] = {L"Hello", L"World", L"Test"};
std::vector<std::wstring> vec(raw_data, raw_data+2); // construct with first 2 elements
std::wcout << vec[0] << ' ' << vec[1] << '\n';
wchar_t const ** raw_ptr = raw_data;
vec.assign(raw_ptr, raw_ptr+3); // assing 3 elements
std::wcout << vec[0] << ' ' << vec[1] << ' ' << vec[2] << '\n';
chtz
  • 17,329
  • 4
  • 26
  • 56