Search code examples
c#c++string-conversion

Unable to retrieve wchar_t* from C++ to C#


I have been trying to call an API from DLL like below:

 [DllImport(@"TELCompress.dll", EntryPoint = "TELMonDecode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    public static extern int TELMonDecode(ref bool a, ref bool b, byte[] ab, System.IntPtr pDestBuf, int j, int byteCount);

Call from C# code

int returnval = TELMonDecode(ref a, ref b, bytes, destPnt, k, bytesRec);

C++ code in the DLL

__declspec(dllexport) int TELMonDecode(bool *bUnicode, bool *bCompress, BYTE *pSourceBuf, wchar_t* pDestBuf, int pDestBufSize,int byteCount)

{
 ...
    CString decodedMsg = _T("<Empty>");
    int erc = DecodeByteStream(bUnicode, bCompress, pSourceBuf, &decodedMsg);

    ::MessageBox(NULL,L"Decoding byte done",L"Caption",0);
    pDestBuf = decodedMsg.GetBuffer();
    ::MessageBox(NULL,pDestBuf,L"Caption in TELMonDecode",0);
 ...
}

I have referred to many links here but still I am unable to figure out what wrong I am doing. Please guide.


Solution

  • Use BSTR* instead on wchar_t* and you should be able to use ref String at C# side.