I am using C# code and need to call this method inside a C++ dll.
static std::wstring DecryptData(const std::wstring& strKey);
I've read many things and my best guess would be to pass something that is easier to read for both languages, like char-array or even byte-array and build the wstring in C++ and the string in C# afterwards.
Did anyone do that already?
Edit:
I read the linkes Topic but none of the answers helps me:
Using const
didn't help.
This is what I have now: C#
[DllImport(DLL_PATH, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string DecryptData([MarshalAs(UnmanagedType.LPWStr)]string strKey);
C++
extern "C" __declspec(dllexport) const std::wstring DecryptData(const std::wstring& strKey) {
return WlanHelper::CWirelessHelper::DecryptData(strKey);
}
This gives me a PInvokeStackImbalance =/
You may find this question and this question to be relevant. There are two issues:
std::string
/std::wstring
, andCWirelessHelper::DecryptData
).An approach is to copy the string to a plain
wchar_t*
buffer allocated usingCoTaskMemAlloc
(the framework will handle the string conversion and free the allocated memory).On the unmanaged side, the code becomes:
And on the managed side: