简体   繁体   English

从C#调用C ++代码时出现System.EntryPointNotFoundException

[英]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.

相关问题 尝试从C#调用非托管函数时出现'System.EntryPointNotFoundException' - 'System.EntryPointNotFoundException' when trying to call unmanaged function from C# 从 DLL 调用 function 时出现 System.EntryPointNotFoundException - System.EntryPointNotFoundException when calling function from DLL wolfssl C# 包装客户端为 CTX_set_psk_client_callback 提供 System.EntryPointNotFoundException - wolfssl C# wrapper client gives System.EntryPointNotFoundException for CTX_set_psk_client_callback 使用aff文件时System.EntryPointNotFoundException - System.EntryPointNotFoundException when using aff file 在C#中绑定C ++ DLL时的EntryPointNotFoundException - EntryPointNotFoundException when binding C++ dll in C# 从C#调用C ++时出现System.AccessViolationException - System.AccessViolationException when calling C++ from C# C# EntryPointNotFoundException - C# EntryPointNotFoundException 在C#中使用TaskDialog时的EntryPointNotFoundException - EntryPointNotFoundException when using TaskDialog in C# System.EntryPointNotFoundException以平行红色显示在输出中 - System.EntryPointNotFoundException in Parallel red in output 尝试将 C/C++ DLL 导入 C# 时出现 DLL EntryPointNotFoundException - DLL EntryPointNotFoundException when trying to import C/C++ DLL into C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM