[英]Creating a C++ DLL and then using it in C#
Ok I'm trying to make a C++ DLL that I can then call and reference in ac# App. 好的我正在尝试创建一个C ++ DLL,然后我可以在ac#App中调用和引用它。 I've already made a simple dll using the numberous guides out there, however when I try to reference it in the C# app I get the error
我已经使用了很多指南制作了一个简单的dll,但是当我尝试在C#应用程序中引用它时,我得到了错误
Unable to load DLL 'SDES.dll': The specified module could not be found. 无法加载DLL“SDES.dll”:找不到指定的模块。
The code for the program is as follows (bear with me I'm going to include all the files) 该程序的代码如下(跟我一起,我将包含所有文件)
//These are the DLL Files.
#ifndef TestDLL_H
#define TestDLL_H
extern "C"
{
// Returns a + b
__declspec(dllexport) double Add(double a, double b);
// Returns a - b
__declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
__declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0
__declspec(dllexport) double Divide(double a, double b);
}
#endif
//.cpp
#include "test.h"
#include <stdexcept>
using namespace std;
extern double __cdecl Add(double a, double b)
{
return a + b;
}
extern double __cdecl Subtract(double a, double b)
{
return a - b;
}
extern double __cdecl Multiply(double a, double b)
{
return a * b;
}
extern double __cdecl Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
//C# Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("SDES.dll")]
public static extern double Add(double a, double b);
static void Main(string[] args)
{
Add(1, 2); //Error here...
}
}
}
Anyone have any idea's what I may be missing in my program? 任何人都知道我的程序中可能缺少什么? Let me know if I missed some code or if you have any questions.
如果我错过了一些代码或者您有任何问题,请告诉我。
Download the Dependecy Walker and open your SDES.dll
. 下载Dependecy Walker并打开您的
SDES.dll
。 Check if all the dependent DLLs can be loaded. 检查是否可以加载所有相关DLL。 If you see a missing dependence, put that dll in the target directory, too.
如果您发现缺少依赖性,也将该dll放在目标目录中。
Are you using a 64-bit system? 您使用的是64位系统吗? If yes, you should target your C# and C++ to the same architecture (both 32 or both 64 bit).
如果是,则应将C#和C ++定位到相同的体系结构(32位或64位)。
I just tested your functions and it worked out. 我刚刚测试了你的功能,结果很好。
[DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern double Add(double a, double b);
static void Main(string[] args)
{
Console.WriteLine(Add(1.0, 3.0));
Console.ReadLine();
}
Output: 输出:
4
This is what I did with Visual Studio 2010: 这就是我在Visual Studio 2010中所做的:
add this P/Invoke signature to the C# file: 将此P / Invoke签名添加到C#文件:
[DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern double Add(double a, double b);
[DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern double Add(double a, double b);
Note: I had to pass the parameter CallingConvention.Cdecl
, otherwise I got an exception. 注意:我必须传递参数
CallingConvention.Cdecl
,否则我得到一个异常。
PS: I didn't have to set the architectures. PS:我没有必要设置架构。 It just worked.
它刚刚起作用。 (I'm using a x64 machine, with a 64-bit OS.)
(我正在使用带有64位操作系统的x64机器。)
it means when it tried to load the dll, it could not find it! 这意味着当它试图加载DLL时,它找不到它!
http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx
most likely its not in the same directory as your C# app. 很可能它不在你的C#应用程序的同一目录中。
also, good time to mention 还有,好时机提一下
http://www.dependencywalker.com/ http://www.dependencywalker.com/
helps find DLL dependency problems. 有助于发现DLL依赖问题。
将SDES.dll
放到放置C#.exe的文件夹中。
Where is SimulateGameDLL
defined? SimulateGameDLL
在哪里定义? I dont see it in the C/C++ code. 我没有在C / C ++代码中看到它。
I just compiled the code, and got no error. 我只编译了代码,没有错误。 Here is my command lines.
这是我的命令行。
> cl /LD test.cpp -o SDES.dll
> csc test.cs
> test.exe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.