I am trying to return a string from a c++ dll export function. I am calling this function from c#. I have seen a lot of examples on the internet and I am really confused what to
On windows you can return string directly using BSTR
(from wtypes.h
included by windows.h
). BSTR
can be created from standard wide string bySysAllocString
function:
_declspec(dllexport) BSTR getDataFromTable(const char* tableName)
{
return SysAllocString(L"String to return");
}
Then in C# you marshal the return type as BStr
:
[DllImport("\\SD Card\\ISAPI1.dll", CallingConvention = CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
static extern string getDataFromTable(string tableName);