c++dlllpwstr

How to set a LPWSTR pointer which has been provided by reference to a function?


I am replacing an old DLL which has a function, passing an LPWSTR by reference. The functions should create a new wchar buffer and return the newly created LPWSTR pointer. I found a solution, but I think it could be solved better!?

By the main proramm the DLL function is called like this:

LPWSTR data = null;
funcA(&data);

The function shoud creat a new wchar buffer and return the address.

So here is what I do (it works):

void funcA(LPWSTR szData)
{
  LPWSTR newData = L"DEMODATA";
  LPWSTR newBuffer = new TCHAR[9];
  wmemcpy(newBuffer, newData, 9);

  memcpy(szData, (LPWSTR)&newBuffer , sizeof(LPWSTR));  
}

Is it possibe to write the last line better readable? I tried to assign the new pointer but this does not work:

szData = (LPWSTR)&newBuffer; // not working!

Solution

  • memcpy(szData, (LPWSTR)&newBuffer , sizeof(LPWSTR));  
    

    is equivalent to

    *(LPWSTR*)szData = newBuffer; //this copies into the address pointed to by szData
    

    not to

    szData = (LPWSTR)&newBuffer; // this copies into szData