01 June 2006

Using C++ Libraries in C#

Using C++ Libraries in C#
-----------------------------
I am told it’s quite a task! Considering Microsoft claims, "Power of C++ and flexibility of Visual Basic!" I thought there is
something Kala in Daal (fihsy). Now it turns out lot of wrapping that this stuff. Well I would put how I managed to do that
after a few days. Yeas, will let you know if I scrapped it as well. What a day :)

I finally came up with this incomlete stuff:

(This only tells how to call member functions, if you plan using Types or classes, you are in a wrong place)

Using VC++ Dlls in C#
---------------------------
Once you have chosen C# scratch your head before you start. You must if you are a beginner like me. Last thing you should do is to look in to that sacred Microsoft bible: MSDN. Search in Google groups instead.

Assume you have a dll VCDll.dll. Copy the dll to your project folder or to C:\windows\system32, or any other folder tell your .NET IDE that it is in X folder using build->Options.

Once the dreaded dll is in place, suppose that you would like to call a function,
bool KillGates()

You may use it as follows;

[DllImport("VCDll.dll", EntryPoint="?KillGates@CPDIdev@@QAEHXZ")]
//Note you have to use appropriate entry point above is just an example.
static extern bool KillGates(); // Returns TRUE if run successfully

If you wonder where is this "?KillGates@CPDIdev@@QAEHXZ" coming from, that directly comes from Bill Gates' godown. The one you see is an example for the mangled name of the function. You may get the mangled names as follows;

1 Type dumpbin.exe in the command prompt.
2 You will get an error! If you do, not go to step 4.
3 Copy dumpbin.exe in Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin to
C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE
4 Type;
dumpbin.exe /EXPORTS > FunctionNames.txt
5 Use FunctionNames.txt to identify EntryPoint and use it in the DllImport section explained above.

Hold on. Not everything is over. REBUILD you application. Otherwise you will get an error depending on what you have coded!
For e.g., if you specify no or wrong Entrypoint you will get EntryPoinNotFound failed to find KillGates(); in VCDll.dll.

Now you are done. Note VCDll.dll is an example replace the name of your dll there when you write your program.

Not successful?
You ought to be. I could not get it to work! :) What about types? You can not use the types like Class etc directly. I gave up and started using Java ! I am not good in both so I had no roblem in switching.
It is insane guys. Do not try this stuff. If you have source with you recompile it with some changes to make it Managed code. Otherwise hire Osama to kill your dll vender.

Send me an email, I took an oath this dooms day! I will kill anyone who asks me about this problem again.

A friend of mine did a nice work around. He created a dilogbox that displays data using C++ and MFC. He uses the data in his C# program to get the job done. THis is useful if you are using your program for an experiment. Even otherwise you may hide the dilog box and get the job done.

Saskatoon
06/06/06
~rAGU

No comments:

Post a Comment

Please leave a note about what you think about this write up. Thanks.

ಯಕ್ಷಮಿತ್ರದಿಂದ ಹೊಸ್ತೋಟ ಭಾಗವತರ ಯಕ್ಷಗಾನ ಲಕ್ಷಣ ಗ್ರಂಥ ಪ್ರಕಟಣೆ

                      ಪುಸ್ತಕಕ್ಕಾಗಿ ಸಂಪರ್ಕಿಸಿ ಅಮೇರಿಕಾಸ್: +16473283934                        ಭಾರತ — ರವೀಂದ್ರ ಮುದ್ರಣಾಲಯ ಸಾಗರ, ಮೋ: +9194495872...