从 C# 程序访问 DLL 中的方法 [英] Access a method from a DLL from C# program
问题描述
我有一个 C 程序并且我创建了一个 DLL 文件.我使用的是 Windows Vista 和 Visual C++.
I have a C program and i have created a DLL file. I am using Windows Vista, and Visual C++.
现在我需要从 C# 代码的 Main() 方法访问该 DLL 中的方法.这样做的步骤是什么?
Now I need to access a method from that DLL, from the Main() method of a C# code. What are steps of doing so?
到目前为止我已经添加了DLL文件作为参考,之后我该怎么办?
So far I have added the DLL file as reference, thereafter what should I do?
这只是一个例子:
int main1( void ) {
prinf("Hello World");
}
请注意,这个类也使我们可以使用其他 .lib 函数,但我能够成功地从中创建一个 DLL.(我不知道这是否相关)
Please note that this class also makes us of other .lib functions, but i was able to successfully create a DLL out of it. (I don't know if this is relevant)
现在我需要从我的 C# Main() 访问这个方法;
Now i need to access this method from my C# Main();
[STAThread]
static void Main()
{
// I need to call that main1() method here
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
推荐答案
参见 使用在 c# 代码中的 c++ dll 中定义的类,这是一个很好的公认答案.正如 Hans Passant 在评论中所写,您不能添加本机 DLL 作为对 C# 项目的引用.
See using a class defined in a c++ dll in c# code which has a great accepted answer. And as Hans Passant wrote in the comments, you cannot add a native DLL as a reference to a C# project.
当我引用自己的本机 DLL 之一时,我通常要么在 C# 项目和生成本机 DLL 的项目之间添加依赖项,要么将 DLL 添加为 C# 项目中的链接内容文件,如下所示:
When I refer to one of my own native DLLs, I usually either add a dependency between the C# project and the project which generates the native DLL, or I add the DLL as a linked content file in the C# project, like so:
- 右键单击该项目,然后选择添加">现有项目".
- 浏览并选择您想要的 DLL,但先不要点击添加.
- 点击添加按钮右侧的小箭头,然后选择添加为链接.
- 选择现在出现在您的 C# 项目中的 DLL 并转到其属性.
- 确保将构建操作设置为内容.
- Right-click on the project and choose Add > Existing Item.
- Browse and select the DLL you want, but don't click Add yet.
- Click on the tiny arrow at the right of the Add button and select Add As Link.
- Select the DLL that now appears in your C# project and go to its properties.
- Make sure you have Build Action set to Content.
这会将 DLL 复制到 C# 项目的 binDebug
文件夹中,并确保一旦您决定创建一个安装项目,您就可以轻松引用所有内容文件 并将它们包含在 Microsoft 安装程序包中.
This will copy the DLL to the binDebug
folder of the C# project and make sure that, if you once decide to create a Setup project, you can easily reference all content files and include them in the Microsoft Installer package.
现在,为了能够看到在您的本机 DLL 中编写的函数,您必须注意导出它们(请参阅 导出 C 函数以用于 C 或 C++ 语言可执行文件 和 使用 __declspec(dllexport) 从 DLL 导出).因此,您必须在函数声明周围添加一个 extern "C"
块(我假设您在 .cpp 源文件中编写了代码,这意味着如果您不将它们声明为 extern "C"
),编译器将发出 mangled 函数名:
Now, to be able to see the functions written in your native DLL, you have to take care of exporting them (see Exporting C Functions for Use in C or C++ Language Executables and Exporting from a DLL Using __declspec(dllexport)). So you'd have to add an extern "C"
block around your function declaration (I am assuming you wrote your code in a .cpp source file and this means that the compiler will emit mangled function names if you do not declare them as being extern "C"
):
extern "C"
{
__declspec (dllexport) void __cdecl Foo(const char* arg1);
}
...
void Foo(const char* arg1)
{
printf ("Hello %s !", arg1);
}
__declspec (dllexport)
修饰意味着编译器/链接器必须使函数从 DLL 外部可见.而 __cdecl
定义了如何将参数传递给函数(标准的C"方式这样做).
The __declspec (dllexport)
decoration means that the compiler/linker will have to make the function visible from outside of the DLL. And the __cdecl
defines how parameters will be passed to the function (the standard "C" way of doing this).
在您的 C# 代码中,您必须引用 DLL 的导出方法:
In your C# code, you'll have to refer to your DLL's exported methods:
class Program
{
[DllImport("mydll.dll")]
internal static extern void Foo(string arg1);
static void Main()
{
Program.Foo ("Pierre");
}
}
您应该阅读平台调用教程 给出了所有血腥的细节.
You should read the Platform Invoke Tutorial which gives all the gory details.
这篇关于从 C# 程序访问 DLL 中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!