P/Invoke 运行时错误

发布于 2024-12-18 07:54:27 字数 732 浏览 0 评论 0原文

我正在尝试围绕 C++ dll 创建一个托管包装器。当我尝试运行测试应用程序时,收到一条错误消息,指出加载 dll 失败。事实证明,这是寻找 CRT 时出现的问题。将 CRT 复制到同一目录可以解决此问题,但随后会抛出错误,指出应用程序错误地加载了 C 运行时 (R6034)。本机 C++ 程序不会遇到这些问题。

该dll是用vc 2008编译的 - 托管代码是在2010年编译的。

该dll有一个清单,我尝试将CRT的确切版本复制到工作目录中,结果与上面相同。

我的问题是:

  1. 为什么本机程序可以加载CRT,但托管包装器找不到它?
  2. 我可以在哪里寻找解决有关加载 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 :

  1. Why can the native program load the CRT, but the managed wrapper can't find it?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-12-25 07:54:27

据我所知,如果你想使用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文