如何将const unsigned char * var传递给C ++非托管DLL [英] How to pass const unsigned char *var to C++ unmanaged DLL

查看:146
本文介绍了如何将const unsigned char * var传递给C ++非托管DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用extern关键字在C ++非托管DLL中使用函数
函数参数之一是const unsigned char * var,
我试图将该参数作为字符串Byte数组传递,但这没用.

您能帮我找到正确的数据类型以传递该参数吗?

Hi,

I''m using extern keyword to use function in C++ unmanaged DLL
one of the function parameters is const unsigned char *var,
I tried to pass that parameter as a string , Byte array but that was useless.

Would you please help me to find the correct data type to pass that parameter

[DllImport("DMT.dll")]
        static extern int RequestData(int comm_type, int conn_num, int slave_addr, int func_code, const unsigned char *data, int datalen);



它是一个用于ModBus通信的库,data参数通常以字符串形式传递,但是我得到的是每个字符的十六进制代码.



it''s a library for ModBus communication and the data parameter is usually passed as string but what i get instead is the Hex code of each char.

推荐答案

而不是const unsigned char *datastringSystem.Runtime.InteropServicesMarshalAsAttribute(UnmanagedType)一起使用;使用System.Runtime.InteropServices.LPStr作为参数.

参见:
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.marshalasattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.unmanagedtype.aspx [ ^ ].

另外,请查看该函数的调用约定.找出"DMT.DLL"使用的调用约定(也许是Cdecl?),并为System.Runtime.InteropServices.DllImportAttribute使用适当的值.参见:
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.dllimportattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.callingconvention.aspx [ ^ ].

—SA
Instead of const unsigned char *data use string with System.Runtime.InteropServicesMarshalAsAttribute(UnmanagedType); use System.Runtime.InteropServices.LPStr as a parameter.

See:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx[^].

Also, look at calling conventions for the function. Figure out what calling conventions are used by "DMT.DLL" (maybe Cdecl?) and use appropriate value for System.Runtime.InteropServices.DllImportAttribute. See:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx[^].

—SA


这篇关于如何将const unsigned char * var传递给C ++非托管DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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