简体   繁体   English

如何将C / C ++ dll导入.NET应用程序

[英]How to Import a C/C++ dll to .NET application

In my new project I want to use a existing C C++ functions . 在我的新项目中,我想使用现有的C C ++函数。 For that I would need to add the C/C++ dlls to C# project. 为此,我需要将C / C ++ dll添加到C#项目中。

My C/C++ projects is of type UnManaged and it is not a COM Project. 我的C / C ++项目属于UnManaged类型,它不是COM项目。 I have seen few articles after from MSDN and tried: 我从MSDN看过几篇文章并尝试过:

[DllImport("user32.dll", EntryPoint="MessageBoxA")]

But I am not sure Where should I place the user32.dll or my.dll . 但是我不确定将user32.dllmy.dll Do I need to place my dll in GAC or in some other physical folder. 我是否需要将dll放入GAC或其他物理文件夹中。

If that is going to be a physical folder then , Don't I need to use either relative or absolute path? 如果那将是一个物理文件夹,那么,我不需要使用相对路径还是绝对路径?

Also read few articles talking about porting the C++ dlls using InterOp but not quite sure how to start with. 还阅读了几篇有关使用InterOp移植C ++ dll的文章,但不太确定如何开始。

Please don't put your own dll's in C:\\Windows\\system32. 请不要将您自己的dll放在C:\\ Windows \\ system32中。 If you're going to use part of Windows, like user32.dll, you don't put it anywhere, it's already there and will be found at runtime because C:\\Windows\\system32 is on the path. 如果要使用Windows的一部分(例如user32.dll),则不要将其放置在任何地方,因为它已经在路径中,所以可以在运行时找到,因为它在路径上。 If you're going to use your own DLL, put it in the bin or bin\\Debug or whatever folder, right next to your exe. 如果要使用自己的DLL,请将其放在exe旁边的bin或bin \\ Debug或任何文件夹中。 This is most easily accomplished by adding it to the project so it will copy into the output directory when you build, but you could have an MSI or whatever. 通过将其添加到项目中可以很容易地做到这一点,以便在构建时将其复制到输出目录中,但是您可以拥有MSI或其他任何东西。 If it's used by multiple applications you have to decide if you want them all to start using the new one when it's updated, or to sit happy with their existing copies. 如果有多个应用程序使用了它,则必须决定是希望它们都在更新时开始使用新的应用程序,还是对它们的现有副本感到满意。 That can be a complicated question and you have enough going on getting it working for one app, right? 这可能是一个复杂的问题,您有足够的精力来让它适用于一个应用程序,对吗?

As for Entry Point, it's the name of the function inside the DLL. 至于入口点,它是DLL中函数的名称。 If you're using the same name in your managed code, you can leave it out. 如果您在托管代码中使用相同的名称,则可以将其省略。 It lets you map a DLL function name that you may find complicated into a simpler name you're using in your managed code. 它使您可以将可能会发现复杂的DLL函数名称映射为在托管代码中使用的更简单的名称。

您可以将其放在应用程序bin文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM