Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm dealing with MOF here and the code is written in C++ which is used to extract the instances from the MOF class file ...and i've completed the Coding part

Now here comes the problem i want to create a "Form" which will consist of One "label","textbox" and a "button" and when i click on button event should trigger the C++ code for MOF and whatever results are transferred back to the "Form"

plz help
Posted

1 solution

This is done via P/Invoke. You need to make a DLL using C++ code, exports functions you need. You cannot and need not to add this DLL to your C# project. Instead, you should create some class with static declaration of the functions exported from your DLL and apply System.Runtime.InteropServices.DllImportAttribute. If you do it correctly, you DLL will be referenced in you managed code. See for help and code samples: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^].

This can be a bit tricky due to different types. You should use how to learn System.Runtime.InteropServices.MarshalAsAttribute, sometimes InAttribute, OutAttribute or OptionalAttribute, see http://msdn.microsoft.com/en-us/library/9esea608.aspx[^] and the references.

Alternative way is using mixed-mode (managed+unmanaged) C++ project, where you can mix C++ and C++/CLI code. This is the way to wrap C++ declarations into C++/CLI managed type and make them accessible for any .NET assembly. In this way, you can reference this DLL as a normal .NET assembly.

[EDIT]

After changes in Microsoft documentation, most adequate link to P/Invoke and C++/CLI implicit P/Invoke is this:
https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 9-Apr-15 15:02pm    
Sergey, why do you made some changes to this answer?
Sergey Alexandrovich Kryukov 9-Apr-15 15:09pm    
Because the link I added is very important; I want to help people looking on this topic.
Wikipedia article on P/Invoke presently has bad link.
I don't want to leave my past answer in bad misleading state.
—SA
Maciej Los 9-Apr-15 15:10pm    
Do you know that might cause downvotes?
Sergey Alexandrovich Kryukov 9-Apr-15 15:14pm    
I don't care. I already get enough down-votes on different moves.
It's more important to do right things.

Why would some decide to down-vote? Some stupid rules? Yes, I heard certain opinions on this matter, but why should I even care of something I consider completely wrong? Everything which can be fixed deserves to be fixed. And you cannot say that P/Invoke in not actual anymore.

—SA
Maciej Los 9-Apr-15 15:22pm    
I really do like this attitude! This makes you exceptional!
5ed!

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