Click here to Skip to main content
15,918,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a c++ .exe file.
1- How can i decompile the .exe file ? OR
2- How can i access methods, functions and classes of c++.exe file in a .Net project ?
Posted

PInvoke to the rescue!

Read tutorial, it is easy to understand.
 
Share this answer
 
You are confusing Platform/Invoke with COM interop. These are two separate interop techniques. As far as which one you should use I would lean toward PInvoke given your newness to Windows programming. PInvoke meets most needs and is simple to use.

For PInvoke all you need is the C signature for a method to call in a DLL. If you want to write your code in C++ then you must: a) create a static or global function in C++ (no class methods), and b) you must mark the function with extern "C" in order to change the namemangling from C++ to C. You can not access C++ classes or class members through PInvoke. Theoretically you can but honestly it is simply too difficult and not worth the effort.

Here is a function you might write in C++:

//In DLL: MyDll.dll
extern "C" double SquareRoot ( double value )
{
//Do work
}

In C# you would do this:

public class MyClass
{
public double SqrRoot ( double value )
{
return SquareRoot(value);
}

[DllImport("MyDll.dll")]
private static extern double SquareRoot ( double value );
}

The general rule of thumb is to make these functions private and put a .NET wrapper around it like in the example. DllImport accepts a lot of optional parameter to control character sets, error handling, etc. Furthermore each parameter can be attributed to control marshaling and whatnot but the above example gives you the basics.

If you want to expose an entire class and its methods then you are forced to use COM interop. This requires that you create a COM interface and class using ATL or something and then register the COM object. You then have to import the COM object into a .NET project (or use tlbimp) to generate the COM proxy that .NET will use. You can then use the auto-generated proxy to talk with the COM object. However I'd avoid this if at all possible since it complicates issues and adds to deployment requirements. For more information use MSDN to look up Platform/Invoke and COM Interop.
 
Share this answer
 

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