C#包装类C ++的lib DLL [英] C# wrapper class for c++ lib dll

查看:169
本文介绍了C#包装类C ++的lib DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想C#创建一个类来访问一个C ++ lib中的功能。在C ++函数的DLL:结果
布尔WriteReply(const的无符号字符*答复,const的无符号长reply_length)



的样本如何将其在C ++使用: -

 无符号短MSG_ID = 0×0000; 
字节msg_body [] = {(字节)(的GetTickCount()/为0x100)}; //为回送数据

一个随机值//消息ID和消息体结合成一个大的味精
无符号长msg_length = sizeof的(MSG_ID)+的sizeof(msg_body);
字节* big_msg =新的字节[msg_length]
big_msg [0] = LOBYTE(MSG_ID);
big_msg [1] = HIBYTE(MSG_ID);
的memcpy((无效*)及big_msg [2](无效*)msg_body,sizeof的(msg_body));

//发送消息大
如果(!big_dev.WriteReply(big_msg,msg_length))
{
//做的东西在这里
$} b $ b

我似乎无法从C#的功能传递给DLL( AccessViolationException )。这是我试过的命令: -

 字节[]字节组=新的字节[3] {0×01,0×02,×03 }; 
IntPtr的unmanagedPointer = Marshal.AllocHGlobal(bytearray.Length);
Marshal.Copy(字节数组,0,unmanagedPointer,bytearray.Length);

布尔writestatus =(布尔)NativeMethods.WriteReply(unmanagedPointer,(UINT)bytearray.Length);

和进口方面: -

 函数[DllImport(dllname.dll,入口点=WriteReply)] 
[返回:的MarshalAs(UnmanagedType.U1)
内部静态外部布尔WriteReply( IntPtr的味精,UINT reply_length);

请让我知道我在哪里出了错?谢谢!


< DIV CLASS =h2_lin>解决方案

我觉得最新的代码提供了一个线索,真正的问题:

 如果(!big_dev.WriteReply(big_msg,msg_length))

这可不行因为 WriteReply 是一个成员函数。你需要调用C风格的函数,而不是C ++成员函数。后者需要(代码示例中 big_dev )的实例。


I am trying to create a class in c# to access the function in a c++ lib. The function in the c++ dll :
bool WriteReply(const unsigned char *reply, const unsigned long reply_length).

A sample of how its used in c++:-

unsigned short msg_id = 0x0000;                      
byte msg_body[] = {(byte)(GetTickCount()/0x100)};    // a random value for loopback data

    // combine the message id and message body into an big msg
    unsigned long msg_length = sizeof(msg_id)+sizeof(msg_body);
    byte* big_msg = new byte[msg_length];
    big_msg[0] = LOBYTE(msg_id);
    big_msg[1] = HIBYTE(msg_id);
    memcpy((void*)&big_msg[2], (void*)msg_body, sizeof(msg_body));

    // send the big message
    if (!big_dev.WriteReply(big_msg, msg_length))
    {
        //do something here
    }

I can't seem to pass the function from c# to the dll (AccessViolationException). This is the command i've tried:-

byte[] bytearray = new byte[3] { 0x01, 0x02, 0x03 };
IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytearray.Length);
Marshal.Copy(bytearray, 0, unmanagedPointer, bytearray.Length);

bool writestatus = (bool)NativeMethods.WriteReply(unmanagedPointer, (uint)bytearray.Length);

and on the import side:-

[DllImport("dllname.dll", EntryPoint = "WriteReply")]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern bool WriteReply(IntPtr msg, uint reply_length);

Please let me know where have i gone wrong?Thanks!

解决方案

I think the latest addition of code provides a clue as to the real problem:

if (!big_dev.WriteReply(big_msg, msg_length))

This cannot work because WriteReply is an member function. You need to be calling a C style function rather than a C++ member function. The latter requires an instance (big_dev in the code sample).

这篇关于C#包装类C ++的lib DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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