Rajendar Manda
Rajendar Manda

Reputation: 373

How to convert c# string to c++ string when we pass string as parameter using winapis

Using SendMessageW function I am passing c# string as a parameter to c++ function. I am typecasting to CString in c++ but it's value is empty. Please check below code and provide solution

-----------------------c# code ------------------

public unsafe IntPtr Testing()
{

  string string_aux = "Stringtochange";
  void* pt = Marshal.StringToBSTR(string_aux).ToPointer();
  IntPtr ab = new IntPtr(pt);
  return ab;
}

public void GetValue()  
{

   SendMessageW(utilityHandle1, TVM_GETITEMHEIGHT, handle,Testing());

}

--------------------- C++ code --------------

CString *st = (CString*)lParam;

MessageBox(NULL,*st,L"stringvalue",NULL);

Here *st value is empty.

Upvotes: 0

Views: 460

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

You seem to be abusing TVM_GETITEMHEIGHT. Why not use a custom message.

CString is a C++ class. It is not binary compatible with a BSTR.

Personally I would use Marshal.StringToCoTaskMemUni in the C# and cast to wchar_t* in the C++. Remember to destroy the unmanaged memory after you've used it when SendMessageW returns, by calling Marshal.FreeCoTaskMem.

Upvotes: 3

Related Questions