简体   繁体   English

从C ++非托管DLL传递C#函数

[英]Passing C# function from C++ unmanaged dll

I have device with C++ DLL interface and I should create C# class to control it. 我的设备带有C ++ DLL接口,我应该创建C#类来对其进行控制。 I have no problem with (dis)connecting to this device, but I could not get read function : 我没有(断开)连接到此设备的问题,但是我无法获得读取功能:

Definiton of function in interface is : 接口中的功能定义为:

bool Driver_Read(int DeviceNo, unsigned char *pReadBuf, unsigned long ReadLen, unsigned long *pReadLen)

I did som research, and I have found that this code should work : 我进行了som研究,发现该代码可以正常工作:

[DllImport("..\\..\\..\\..\\Driver.dll")]
public static extern bool Driver_Read(int deviceNo, StringBuilder pReadBuf, ulong readLen, ref UInt32 pReadLen);

But when I run it, I get an AccessViolationException : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." 但是,当我运行它时,我得到一个AccessViolationException:“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”

Lots of people had similar problem, but nothing was helpful for me. 很多人都有类似的问题,但对我没有帮助。 Do you know, where could be the problem? 您知道哪里可能出问题吗? Thanks in advance. 提前致谢。

try this: 尝试这个:

[DllImport("..\\..\\..\\..\\Driver.dll")]
public static extern bool Driver_Read(int DeviceNo, IntPtr pReadBuf, uint ReadLen, ref uint pReadLen)

this should help with the conversions of types from C++ to C#: 这应该有助于将类型从C ++转换为C#:
http://msdn.microsoft.com/en-us/library/ac7ay120(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ac7ay120(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM