问题描述
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.
推荐答案
这篇关于参考C ++ dll到C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!