Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a library that is published by a governmental, non-profit organization on how to generate models from data. It supplies a library file (coilmodel.dll) and a header (coilmodel.h).

From the header, I see there are only four (4) exported methods that our applications will need.

If I were to use P/Invoke, does the DLL need to be placed in a certain folder on the PC? I have not seen any examples of how to point to a DLL file in another location. Could someone give me an example?

If I were to write a "managed code" wrapper for the DLL, could this be done in C#, or would I need to write the wrapper in C++ as well? I have not used C++ since C# came out, so I would like to see how to do this as well.

In my header file, the four (4) exported methods are shown below:

#ifndef coilmodel_h
#define coilmodel_h

#ifndef EXPORT
#define EXPORT
#endif

EXPORT int steadyFor(double Par[], double Inlet[], double Outlet[]);

EXPORT int dynamicFor(double time[], double Par[], double Inlet[], double Outlet[]);

EXPORT int steadyInv(double Par[], double Coeff[], double Inlet[], double Outlet[]);

EXPORT int dynamicInv(double time[], double Par[], double Coeff[], double Cap[], double Inlet[], double Outlet[]);

#endif


Thanks for your help!
~Joe
Posted

1 solution

As long as the dlls are in a location that your application can find it's easy to import them to C#. One such place is in the same folder as your application (so just copy them to the bin\Debug folder.

Since all the methods exported in the example above uses primitives all you have to do is import the methods using the DllImport attribute. You can create a C# wrapper around this, and I would recommend that you do as this will expose the C++ methods in a more C#-like way.

This article Baboon Framework : A Revolutionary WPF Framework that will Save You Time and Effort[^], shows (amongst other things) how to interact with a C++ dll from C#, scroll down a bit to find it.

(Note that the article was written as a April fools joke which is why it goes on and on about Baboons, but the techniques and methods described in it are correct).

Hope this helps,
Fredrik
 
Share this answer
 
Comments
jp2code 10-Jun-10 10:58am    
Mr. Bornander,

This is exactly the answer I was looking for. (...even though the Baboon example makes it difficult for me to focus on the idea!)

Thank you,
Joe
Fredrik Bornander 10-Jun-10 11:06am    
Glad someone found the article useful :)

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