本文介绍了调用非托管代码目标函数的签名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好伙计
我必须使用以下功能(C语言)来自dll-
hello Guys
I have to use following function (In C language) from a dll-
int encrypt(unsigned char *key, unsigned char *dataToEncrypt,
unsigned int in_data_len, unsigned char *out_data,
unsigned int *out_data_len)
and i am using following c# code to access the function but its give me
error= "A call to PInvoke function 'ConsoleApplication1!ConsoleApplication1.abc::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."
Used C# code:
<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
abc.crypto();
}
}
public static class abc
{
const string _dllLocation = @"E:\ConsoleApplication1\ConsoleApplication1\bin\Debug\ck.dll";
[DllImport(_dllLocation)]
public static extern int encrypt([In, Out] byte[] key, [In, Out] byte[] dataToEncrypt, uint in_data_len, [Out] byte[] out_data, ref uint out_data_len);
//int encrypt(unsigned char *key, unsigned char *dataToEncrypt, unsigned int in_data_len, unsigned char *out_data, unsigned int *out_data_len)
public static void crypto()
{
var key = new byte[Int16.MaxValue];
key = Encoding.ASCII.GetBytes("123456789012");
var dataToEncrypt = new byte[Int16.MaxValue];
dataToEncrypt = Encoding.ASCII.GetBytes("1232345456789012");
uint in_data_len = 64;
var out_data = new byte[Int16.MaxValue];
uint out_data_len = 0;
int i = encrypt(key, dataToEncrypt, in_data_len, out_data, ref out_data_len);
}
}
}</pre>
推荐答案
这篇关于调用非托管代码目标函数的签名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!