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

问题描述

你能帮我解决一下这个问题吗?



我有一个C ++函数dll,它将由另一个C#应用程序调用。

我需要的功能之一如下:



  unsigned   long  makeArray( unsigned   char  * sendArr, unsigned   long  sendArrLen, unsigned   char  * recvArr, unsigned   long  * recvArrLen);< pre> 





我尝试过:



我用C#编写了以下代码:



 [DllImport(  test.dll,CallingConvention = CallingConvention.Cdecl)] 
public static extern ulong makeArray( byte [] sendArr, ulong sendArrLen, byte [] recvArr, ulong recvArrLen);

private byte [] MakeArray()
{
byte [] arrSend = new byte [] {0x00,0x12,0x34};

ulong nRecvArrLen = 0 ;
byte [] arrRecv = null ; // 在c ++ dll函数中指定(可变大小)

if (makeArray(arrSend,( ulong )arrSend.Length,arrRecv,nRecvArrLen)== 1 // System.AccessViolationException
{
return arrRecv;
}

return null ;
}





不幸的是,上面的代码无效......

(错误是System.AccessViolationException。而不是EntryPointNotFoundException)



我可以知道如何将指针传递给C ++函数?

如果不可能,是否有解决方法?



谢谢。

解决方案

Could you guys please help me solve the following issue?

I have a C++ function dll, and it will be called by another C# application.
One of the functions I needed is as follow:

unsigned long makeArray(unsigned char* sendArr, unsigned long sendArrLen, unsigned char *recvArr, unsigned long *recvArrLen);<pre>



What I have tried:

I wrote the following code in C#:

[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern ulong makeArray(byte[] sendArr, ulong sendArrLen, byte[] recvArr, ulong recvArrLen);

private byte[] MakeArray()
{
    byte[] arrSend = new byte[] { 0x00, 0x12, 0x34 };

    ulong nRecvArrLen = 0;
    byte[] arrRecv = null; // assign in c++ dll function (variable size)

    if(makeArray(arrSend, (ulong)arrSend.Length, arrRecv, nRecvArrLen) == 1) //System.AccessViolationException
    {
        return arrRecv;
    }

    return null;
}



Unfortunately, the above code is not working...
(the error is "System.AccessViolationException". not "EntryPointNotFoundException")

May I know how can I pass a pointer-to-pointer to the C++ func?
If it is not possible, is there any workaround?

Thank you.

解决方案


这篇关于C#编组(C#调用C ++ DLL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:07