调用非托管代码目标函数的签名错误 [英] Calling Unmanaged code target function's signature error

查看:79
本文介绍了调用非托管代码目标函数的签名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好伙计



我必须使用以下功能(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 = @&quot;E:\ConsoleApplication1\ConsoleApplication1\bin\Debug\ck.dll&quot;;
        [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(&quot;123456789012&quot;);

            var dataToEncrypt = new byte[Int16.MaxValue];
            dataToEncrypt = Encoding.ASCII.GetBytes(&quot;1232345456789012&quot;);

            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>

推荐答案

请参阅我的评论这个问题。使用P / Invoke搞砸堆栈太容易了。



你可以使用CodePlex提供的开源PInvoke Interop助手:http://clrinterop.codeplex.com/releases/view/14120 [ ^ ]。



参见: http://clrinterop.codeplex.com [ ^ ]。



-SA
Please see my comment to the question. It's way too easy to screw up the stack using P/Invoke.

You can use open-source PInvoke Interop Assistant available from CodePlex: http://clrinterop.codeplex.com/releases/view/14120[^].

See also: http://clrinterop.codeplex.com[^].

—SA


除了由谢尔盖,看起来你将无效/错误数据传递给函数:

- 你的密钥长12个字节 - 非常奇怪的数字。我宁愿期待8/16/32等字节。

- dataToEncrypt是16个字节长,但你设置in_data_len = 64
Apart form the hints given by Sergey, it looks like you are passing invalid / wrong data to the function:
- your key is 12 bytes long - quite an odd number. I'd rather expect 8 / 16 / 32 etc. bytes.
- dataToEncrypt is 16 bytes long, but you set in_data_len = 64


这篇关于调用非托管代码目标函数的签名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆