marshall c++ char* to a vb.net string BYREF

unclepauly

New member
Joined
Aug 9, 2010
Messages
1
Programming Experience
5-10
hello,

the scenario - i have a native c++ dll that creates and initializes a string, which is a char array.

VB.NET:
char pRet[1024];
memset(pRet, 0, 1024);
strcpy(pRet, "change me!");

the c++ dll then fires a callback function, and the callback function must change the value of the string. however, the callback function is in a VB.NET app.

the callback function is defined in the c++ dll as :

VB.NET:
typedef VOID (WINAPI *CALLBACK_ROUTINE)(char* pRet);

so the delegate type that i have created in the VB.NET app is:

VB.NET:
Public Delegate Sub CALLBACK_ROUTINE(ByVal pRet As IntPtr)

then the actual VB.NET callback function itself is like this:

VB.NET:
Private Sub pCallback(ByRef pRet As IntPtr)
    pRet = Marshal.StringToCoTaskMemAnsi("response from vb.net app !")

     ' do not call CoTaskMemFree because the C++ dll created the char array and will therefore be responsible for freeing the memory !
.End Sub

however when the C++ dll has finished calling this VB.NET callback, pRet still says 'change me !'.

so how do i define the delegate type (assuming what i have done is wrong) and how do i modify the value of the string in the VB.NET callback function, such that the c++ dll 'sees' this change ? in other words how do i write the code so that the C++ dll and the VB.NET app share and access the same string buffer ?

any help very much appreciated.
 
Back
Top