使用C#Code中的C ++ DLL [英] Use C++ DLL from C# Code

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

问题描述

我有一个c ++ dll文件,我需要在c#项目中使用。

这个dll文件用于通过USB端口连接的机器。它包含一些我想要使用的函数MA_Initialize(HWND pwndDest,UINT nComMsg)它初始化MA usb通信处理程序。

1. pwndDest:处理用户窗口,其窗口过程将

从通信处理程序接收消息。

2. nComMsg:指定要接收的用户定义消息。



如何我可以创建这个函数的句柄,我该如何使用它?

I have a c++ dll file that i need to use from a c# project.
This dll file is for a machine connected through a usb port. It contains some functions that i want to use "MA_Initialize(HWND pwndDest, UINT nComMsg)" it initializes the MA usb communication handler.
1. pwndDest :Handle to user’s window whose window procedure will
receive the message from communication handler.
2. nComMsg :Specifies the user defined message to be received.

How can I create the handle to this function, and how can I use it??

推荐答案

你需要使用PInvoke。



这是一个很好的网站,你可以用作参考 www.pinvoke.net



You need to use PInvoke.

This is a good website you can use as reference www.pinvoke.net

[DllImport("YourDLL name"]
public static extern int MA_Initialize(IntPtr pwndDest, uint nComMsg);





你没有提到回报类型,所以我假设一个int。



然后你可以从表格中获取这样的句柄。





You don't mention the return type so I assumed an int.

You can then get the handle from the Form like this.

IWin32Window formHandle = this;       // this refers to the form
MA_Initialize(formHandle.Handle, 0);  // 0 is just an example value.


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

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