参考C ++ dll到C#应用程序 [英] Reference C++ dll to C# Application
问题描述
Dears
我用C ++编写了dll,我需要在C#控制台应用程序中引用它。
我搜索了如何这样做但我面临这个例外。
C ++ Func
void加密(unsigned char key [32],unsigned char IV [ 32],unsigned char输入[],unsigned char输出[],int Length);
C#
Dears
I have dll written with C++, and I need to reference it in C# Console Application.
I searched how to do that but I'm facing this exception.
C++ Func
void Encrypt(unsigned char key[32], unsigned char IV[32], unsigned char Input[], unsigned char Output[], int Length);
C#
namespace ConsoleApplication1
{
class Program
{
[DllImport("enc.dll")]
static extern void Encrypt([In, Out] byte[] key, [In, Out] byte[] IV, [In, Out] byte[] Input, [In, Out] byte[] Output, int Length);
static void Main(string[] args)
{
byte[] IV = new byte[32] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
byte[] Key = new byte[32] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
byte[] Input = new byte[16] { 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74 };
byte[] Output = new byte[16];
int Length = 16;
char[] Input1 = new char[16];
char[] Output1 = new char[16];
Encrypt(Key, IV, Input, Output, Length);
}
}
检测到PInvokeStackImbalance
消息:调用PInvoke函数'ConsoleApplication1!ConsoleApplication1.Program :: Encrypt'使堆栈失去平衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'ConsoleApplication1!ConsoleApplication1.Program::Encrypt' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
推荐答案
Hi Moustafa,
通过以下链接: -
http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx [ ^ ]
谢谢,
Mukesh
Hi Moustafa,
go through the following link:-
http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx[^]
Thanks,
Mukesh
尝试为该DLL创建一个包装器可能会更好...
尝试类型库导入程序 [ ^ ]工具。之后你应该像项目中的任何其他程序集一样引用包装器......
It may be better to try and create a wrapper for that DLL...
Try the Type Library Importer[^] tool. After that you should reference the wrapper like any other assembly in your project...
我认为你需要做一些Marshling。这是因为C ++生成了非托管代码,而C#生成了托管代码。请谷歌一下。
You need to do some Marshling I think. It is because C++ generates the unmanaged code and C# generated the managed code.Please google it about.
这篇关于参考C ++ dll到C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!