P/Invoke 运行时错误
我正在尝试围绕 C++ dll 创建一个托管包装器。当我尝试运行测试应用程序时,收到一条错误消息,指出加载 dll 失败。事实证明,这是寻找 CRT 时出现的问题。将 CRT 复制到同一目录可以解决此问题,但随后会抛出错误,指出应用程序错误地加载了 C 运行时 (R6034)。本机 C++ 程序不会遇到这些问题。
该dll是用vc 2008编译的 - 托管代码是在2010年编译的。
该dll有一个清单,我尝试将CRT的确切版本复制到工作目录中,结果与上面相同。
我的问题是:
- 为什么本机程序可以加载CRT,但托管包装器找不到它?
- 我可以在哪里寻找解决有关加载 CRT 的第二个错误的信息? 这可能是因为名称修改、CallingConvention 等原因吗?
C++ 代码(我只有标题):
class Foo {
public:
static void startCall(std::string hostname);
}
C# 代码:
[DllImport("mydll.dll")]
public static extern void startCall(string hostname );
public Test()
{
string hostname = "";
startCall(hostname);
}
I am trying to create a managed wrapper around a C++ dll. When I try to run a test app, I get an error saying loading the dll failed. This turns out to be a problem with trying to find the CRT. Copying the CRT to the same directory moves past this problem, but then throws an error saying the application loaded the C Runtime incorrectly (R6034). A native C++ program has no trouble with either of these issues.
The dll was compiled with vc 2008 - the managed code is compiled in 2010.
The dll has a manifest and I have tried copying the exact version of the CRT into the working directory with the same result as above.
My questions are :
- Why can the native program load the CRT, but the managed wrapper can't find it?
- Where could I look for things to try to fix the second error about loading the CRT?
Could this be because of name mangling, CallingConvention, etc?
C++ Code (I only have the header):
class Foo {
public:
static void startCall(std::string hostname);
}
C# Code:
[DllImport("mydll.dll")]
public static extern void startCall(string hostname );
public Test()
{
string hostname = "";
startCall(hostname);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,如果你想使用C#中的函数,函数接口中不可能有STL。
如果您可以更改 C++ 库接口,请将其更改为使用 char* 而不是字符串。如果您无法编写一个代理库,该库使用 char* 公开接口并将其转换为 std::string,然后调用具有 std::string 函数的 C++ 库
As far as I know it is impossible to have STL in function interfaces if you want to use the functions in C#.
If you can change the C++ library interface, change it to use char* instead of strings. If you can't write a proxy library which exposes the interface with char* and converts it to std::string and then calls the c++ library which has functions with std::string