[英]System.EntryPointNotFoundException when calling C++ code from C#
//------------------------------------- C# Code ------------------------------------ // ------------------------------------- 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 ------------------------------------ // ------------------------------------- C ++代码--------- ---------------------------
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() . 我在调用Test()时收到System.EntryPointNotFoundException。 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: 也许你需要在头文件中添加c ++代码的代码:
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. 这几乎可以肯定是因为C#不能将您的方法Test的名称映射到本机代码中的Test方法。 Try specifying the EntryPoint="Test" attribute for the method as follows: 尝试为方法指定EntryPoint =“Test”属性,如下所示:
[DllImport("MarshallStringsWin32.dll", EntryPoint="Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
extern static void Test([MarshalAs(UnmanagedType.AnsiBStr)] out String str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.