用C ++编写的C#DllImport库 [英] C# DllImport library written on C++

查看:91
本文介绍了用C ++编写的C#DllImport库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中很陌生。
我自己用C ++编写了Dll,我将在C#应用程序中使用该dll中的函数。

I'm pretty new in C#. I have Dll written on C++ by myself and I'm going to use functions from that dll in C# app.

因此,我在声明时执行以下操作C ++项目中的函数:

So, I do the following when declare functions in C++ project:

public static __declspec(dllexport) int captureCamera(int& captureId);

然后我要在C#应用程序中导入此方法:

Then I'm trying to import this method in C# app:

[DllImport("MyLib.dll")]
public static extern int captureCamera(ref int captureId);

但我有一个例外:

Unable to find an entry point named 'captureCamera' in DLL 'MyLib.dll'.

任务是在不指定EntryPoint参数的情况下执行dllimport。有人可以帮我吗?

The task is to do dllimport without specifying EntryPoint parameter. Could anyone help me?

推荐答案

您正在定义没有外部 C块的C ++函数。由于C ++允许您重载函数(即使用不同的参数集创建许多captureCamera()函数),因此DLL中的实际函数名称将有所不同。您可以通过打开Visual Studio命令提示符,进入二进制目录并运行以下命令进行检查:

You are defining a C++ function without an extern "C" block. As C++ allows you to overload functions (i.e. create many captureCamera() functions with different sets of arguments), the actual function name inside the DLL will be different. You can check it by opening Visual Studio Command prompt, going to your binary directory and running this:

dumpbin /exports YourDll.dll

您将获得以下内容:

Dump of file debug\dll1.dll

File Type: DLL

  Section contains the following exports for dll1.dll

    00000000 characteristics
    4FE8581B time date stamp Mon Jun 25 14:22:51 2012
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00011087 ?captureCamera@@YAHAAH@Z = @ILT+130(?captureCamera@@YAHAAH@Z)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

?captu reCamera @@ YAHAAH @ Z 是实际上编码您指定参数的错误名称。

The ?captureCamera@@YAHAAH@Z is the mangled name that actually encodes the arguments you've specified.

如其他答案所述,只需添加extern C

As mentioned in other answers, simply add extern "C" to your declaration:

extern "C" __declspec(dllexport) int captureCamera(int& captureId)
{
    ...
}

您可以通过重新运行dumpbin来重新检查名称是否正确:

You can recheck that the name is correct by rerunning dumpbin:

Dump of file debug\dll1.dll

File Type: DLL

  Section contains the following exports for dll1.dll

    00000000 characteristics
    4FE858FC time date stamp Mon Jun 25 14:26:36 2012
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 000110B4 captureCamera = @ILT+175(_captureCamera)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

这篇关于用C ++编写的C#DllImport库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆