Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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??
Posted

1 solution

You need to use PInvoke.

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

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


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

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

C#
IWin32Window formHandle = this;       // this refers to the form
MA_Initialize(formHandle.Handle, 0);  // 0 is just an example value.
 
Share this answer
 
v2
Comments
Member 10866132 30-Jun-14 4:37am    
The return type is boolean. I know this Import code, what I need to know is about "pwndDest", how to create it and then call MA_Initialize?
Member 10866132 30-Jun-14 4:46am    
Can I create a simple handle and use it instead of formHandle.Handle? If it is possible could you make a small example about creating and using this handle.
Thank you
George Jonsson 30-Jun-14 5:13am    
Why doesn't the solution work?
Don't you have a Windows Form in your application? (I got that impression)
Member 10866132 30-Jun-14 6:20am    
It works, I just want to do some calculation when something happens.
How can I put a code in the handler function?
George Jonsson 30-Jun-14 7:24am    
Not sure what you want to achieve. What do you mean by handler function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900