[英]Creating a CPP DLL for use in a C# program
So I have a WPF solution. 所以我有一个WPF解决方案。 I added a new project and added a CPP Dll project to it.
我添加了一个新项目,并为其添加了一个CPP Dll项目。
I used this example. 我用过这个例子。 Pretty straight forward.
挺直的。
http://www.codeproject.com/Articles/9826/How-to-create-a-DLL-library-in-C-and-then-use-it-w http://www.codeproject.com/Articles/9826/How-to-create-a-DLL-library-in-C-and-then-use-it-w
Here is my code 这是我的代码
CppTestDll.cpp
CppTestDll.cpp
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf("Hello from DLL !\n");
}
}
When I build this I do in fact get a DLL 当我构建这个时,我确实得到了一个DLL
Now when I go into my WPF app and attempt to add a reference to this DLL I get this error. 现在,当我进入我的WPF应用程序并尝试添加对此DLL的引用时,我收到此错误。
"A reference to 'C:\\DIR\\testcppdll.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."
“无法添加对'C:\\ DIR \\ testcppdll.dll'的引用。请确保该文件可访问,并且它是有效的程序集或COM组件。”
If you look in the example you cite: 如果你看一下你引用的例子:
Creating a simple C# application: 创建一个简单的C#应用程序:
[DllImport("TestLib.dll")] public static extern void DisplayHelloFromDLL ();
In C#, keyword extern indicates that the method is implemented externally. 在C#中,关键字extern表示该方法是在外部实现的。
Your code should look something like this: 您的代码应如下所示:
using System;
using System.Runtime.InteropServices; // DLL support
class HelloWorld
{
[DllImport("TestLib.dll")]
public static extern void DisplayHelloFromDLL ();
public void SomeFunction()
{
Console.WriteLine ("This is C# program");
DisplayHelloFromDLL ();
}
}
You don't add a reference to the to the DLL - you P/Invoke
the Function using DLLImport
您没有添加对DLL的引用 - 您使用
DLLImport
P/Invoke
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.