C#编组(C#调用C ++ DLL) [英] C# marshalling (C# call 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 ++函数?
如果不可能,是否有解决方法?
谢谢。
它看起来很好,所以我认为问题出在dll中。我的第一个猜测是函数的导出不正确。
看看我的关于为C#调用C ++的文章或者用好旧的 Dependency Walker <打开dll / A>
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.
It looks fine, so I think that the problem is in the dll. My first guess is that the export of the function is not correct.
Take a look at the code in my article about calling C++ for C# or open the dll with the good old Dependency Walker.
这篇关于C#编组(C#调用C ++ DLL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!