Reputation: 51
//------------------------------------- C# Code ------------------------------------
[DllImport("MarshallStringsWin32.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
extern static void Test([MarshalAs(UnmanagedType.AnsiBStr)] out String str);
[DllImport("MarshallStringsWin32.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
extern static void FreeString([MarshalAs(UnmanagedType.AnsiBStr)] String str);
static void Main(string[] args)
{
String str;
Test(out str);
FreeString(str);
}
//------------------------------------- C++ Code ------------------------------------
void Test(__out BSTR* str)
{
const std::string stdStr = "The quick brown fox jumps over the lazy dog";
_bstr_t bstrStr = stdStr.c_str();
*str = bstrStr.copy();
}
void FreeString(BSTR str)
{
SysFreeString(str);
}
I am getting a System.EntryPointNotFoundException when calling Test() . Would anybody know what I am doing wrong? Is this the correct way to marshall strings?
Upvotes: 1
Views: 3149
Reputation: 2056
Maybe you need to add the code for c++ code in the header file:
extern "C" void __declspec(dllexport) FreeString(BSTR str);
extern "C" void __declspec(dllexport) Test(BSTR* str);
Upvotes: 3
Reputation: 2785
This is almost certainly because C# cant map your name of the method Test to the Test method in the native code. Try specifying the EntryPoint="Test" attribute for the method as follows:
[DllImport("MarshallStringsWin32.dll", EntryPoint="Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
extern static void Test([MarshalAs(UnmanagedType.AnsiBStr)] out String str);
Upvotes: 1