如何在 C# 中调用 C++ DLL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16332701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:46:20  来源:igfitidea点击:

How to call C++ DLL in C#

c#dll

提问by user1333098

I have written a DLL in dev C++. The DLL's name is "DllMain.dll" and it contains two functions: HelloWorldand ShowMe. The header file looks like this:

我已经用 dev C++ 编写了一个 DLL。DLL 的名称是“DllMain.dll”,它包含两个函数:HelloWorldShowMe. 头文件如下所示:

DLLIMPORT  void HelloWorld();
DLLIMPORT void ShowMe();

And the source file looks like this:

源文件如下所示:

DLLIMPORT void HelloWorld ()
{
  MessageBox (0, "Hello World from DLL!\n", "Hi",MB_ICONINFORMATION);
}

DLLIMPORT void ShowMe()
{
 MessageBox (0, "How are u?", "Hi", MB_ICONINFORMATION);
}

I compile the code into a DLL and call the two functions from C#. The C# code looks like this:

我将代码编译成 DLL 并从 C# 调用这两个函数。C# 代码如下所示:

[DllImport("DllMain.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void HelloWorld();

[DllImport("DllMain.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void ShowMe();

When I call the function "HelloWorld" it runs well and pops up a messageBox, but when I call the function ShowMean EntryPointNotFoundExceptionoccurs. How do I avoid this exception? Do I need to add extern "C"in the header file?

当我打电话功能的“HelloWorld”,它运行良好,弹出一个消息框,但是当我调用该函数ShowMeEntryPointNotFoundException发生。如何避免此异常?我需要extern "C"在头文件中添加吗?

采纳答案by atoMerz

The following code in VS 2012 worked fine:

VS 2012 中的以下代码运行良好:

#include <Windows.h>
extern "C"
{
    __declspec(dllexport) void HelloWorld ()
    {
        MessageBox (0, L"Hello World from DLL!\n", L"Hi",MB_ICONINFORMATION);
    }
    __declspec(dllexport) void ShowMe()
    {
        MessageBox (0, L"How are u?", L"Hi", MB_ICONINFORMATION);
    }
}

NOTE:If I remove the extern "C"I get exception.

注意:如果我删除extern "C"我得到异常。

回答by mjb

using System;
using System.Runtime.InteropServices;

namespace MyNameSpace
{
    public class MyClass
    {
        [DllImport("DllMain.dll", EntryPoint = "HelloWorld")]
        public static extern void HelloWorld();

        [DllImport("DllMain.dll", EntryPoint = "ShowMe")]
        public static extern void ShowMe();
    }
}

回答by Shimon Doodkin

things that helped:

有帮助的事情:

  • The: extern "C" { function declarations here in h file } will disable C++ name encoding. so c# will find the function

  • Use __stdcall for the C declaration or CallingConvention.Cdecl in the C# declaration

  • maybe use BSTR/_bstr_t as string type and use other vb types. http://support.microsoft.com/kb/177218/EN-US

  • download "PInvoke Interop Assistant" https://clrinterop.codeplex.com/releases/view/14120paste function declaration from .h file in the 3rd tab = c# declaration. replace with dll filename.

  • : extern "C" { function declarations here in h file } 将禁用 C++ 名称编码。所以c#会找到这个函数

  • 将 __stdcall 用于 C 声明或 C# 声明中的 CallingConvention.Cdecl

  • 也许使用 BSTR/_bstr_t 作为字符串类型并使用其他 vb 类型。http://support.microsoft.com/kb/177218/EN-US

  • 下载“PInvoke Interop Assistant” https://clrinterop.codeplex.com/releases/view/14120从.h 文件中的第三个选项卡= c# 声明中粘贴函数声明。替换为 dll 文件名。