简体   繁体   中英

System.EntryPointNotFoundException when calling C++ code from C#

//------------------------------------- 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?

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM