本文介绍了从C#调用C ++ dll函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在C#中遇到一个blowfish问题的第三个线程。尽管事实上我的应用程序中没有实现blowfish,但我决定把它用作一个外部的C ++ DLL。
请注意,我已经尝试过Blowfish.NET和其他任何其他问题是我正在将代码从C ++转换为C#,而C#代码必须与C ++代码完全相同。

This is my 3rd thread concerning a blowfish problem in C#.Despite the fact I cannot have blowfish implemented in my application, I decided to use it as an external C++ dll.Please note I've tried Blowfish.NET and any other, the problem is that I'm translating code from C++ to C# and the C# code must do exactly the same as the C++ code does.

到目前为止:

---> C ++ DLL源代码

注意,导出的函数在代码末尾

C#代码(定义)

    [DllImport("TestDLL.dll", EntryPoint = "Initkey" ,ExactSpelling = true , CallingConvention = CallingConvention.Cdecl)]
    public static unsafe extern void Initkey(byte[] key);

    [DllImport("TestDLL.dll", EntryPoint = "encode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    public static unsafe extern void encode(UInt32 *stream);

C#代码(函数调用)

C# code(function calling)

- 初始化blowfish key

-Initialize blowfish key

UInt32[] keyarray = new UInt32[2];
//some code
Extern.Initkey(Misc.ConvertFromUInt32Array(keyarray));
//
//
//Helper function used to convert a UInt32 array into Byte array.
public static byte[] ConvertFromUInt32Array(UInt32[] array)
{
    List<byte> results = new List<byte>();
    foreach (UInt32 value in array)
    {
        byte[] converted = BitConverter.GetBytes(value);
        results.AddRange(converted);
    }
    return results.ToArray();
}

- 对数据进行编码。

-Encode the data.

            UInt32[] keyarray2 = new UInt32[2];
            //some code
            unsafe
            {
                fixed (UInt32* LPBYTE = keyarray2)
                {
                    Extern.encode(LPBYTE);
                }
            }

在键盘2被Encode功能覆盖后,我检查C ++代码中的值通过解密来确保一切正常。

After keyarray2 is overwritten by the Encode function, I check the values in the C++ code by decrypting them to make sure everything is alright.

嗯,没关系,这是我的问题,这就是为什么我要求你帮忙

Well, It's not alright.That's my problem, That's why I am asking you for your help.

当我解密它们时,值是不同的,但是如果我加密它们并在C ++源码中解密它们是相等的.C ++代码是完全相同的,除了没有DLL,因为代码在C ++中。

The values are different when I decrypt them,but If I encrypt them and decrypt them in the C++ source, they are equal.The C++ code is absolutely the same,except that there's no DLL since the code is in C++.

可能是因为Initialize函数。我已经读了几个月前,C ++中的数组是作为指针传递,我不相信,但即使如此 - 这可能是问题?

Could that be, because of the Initialize function.I had read a couple of months ago that arrays in C++ are passed as Pointers.I don't believe it,but even so - could that be the problem?

我找不到一个线索。我在C#中浪费了我的生命。至少这个解决方案应该有效,但是它不是 - 为什么?

I can't find a clue. I have wasted my life with that blowfish in C#. At least that solution should work, but it doesn't - Why?

推荐答案

要添加到jachymko所说的内容,还要检查BitConverter的文档 - 您需要确保以您想要的字节顺序传递密钥和数据。
注意 - 从以前的线程,我使用修改的Blowfish.NET加密器成功加密数据,并将其与C ++代码的结果相匹配。

To add to what jachymko has said, also check the documentation for BitConverter - you need to be sure you are passing the key and data in the byte order you intended.Note - from your previous thread, I successfully encrypted data using the modified Blowfish.NET encryptor and got it to match the result of your C++ code.

这篇关于从C#调用C ++ dll函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!