How to import function from C++ dll and use them in c# code ?

  • Thread starter Thread starter MichalSody
  • Start date Start date
M

MichalSody

How cann I import & use these function from an old C++ dll ?

Only this, I've got are these two signatures of function I wannt use in
my C# app.

DllExport int DLLAPI drawImage( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
HANDLE &imageHandle, int &imageBufferSize,
char *imageFormat) ;

DllExport int DLLAPI drawImageFile( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
char *fileName,
char *imageFormat);
 
Hi,

You have to use a feature named P/invoke, you can find a lot of examples in
pinvoke.net
 
MichalSody,

You won't be able to use the first function, as you can not access
referenced parameters (&) in .NET through the P/Invoke layer. You have to
create another version of the function (or a wrapper in unmanaged code if
you cant change the original) which uses pointers.

Assuming you have made the changes to the first function, your
declarations would be

[DllImport("<dllname>")]
static extern int drawImage(
[MarshalAs(UnmanagedType.LPStr)]
string chemBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string queryBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string chemFormat,
int showMapps,
int showRkz,
int showSter,
int showNumb,
int showInvRet,
int queryDisplay
int showResidue,
int canvasWidth,
int canvasHeight,
IntPtr imageHandle,
ref int imageBufferSize,
[MarshalAs(UnmanagedType.LPStr)]
string imageFormat);

[DllImport("<dllname>")]
static extern int drawImageFile(
[MarshalAs(UnmanagedType.LPStr)]
string chemBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string queryBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string chemFormat,
int showMapps,
int showRkz,
int showSter,
int showNumb,
int showInvRet,
int queryDisplay
int showResidue,
int canvasWidth,
int canvasHeight,
[MarshalAs(UnmanagedType.LPStr)]
string fileName,
[MarshalAs(UnmanagedType.LPStr)]
string imageFormat);

These declarations assume that you are not modifying the contents of the
string parameters at all. If you are, then you need to change the
declarations to a StringBuilder instance and make sure to initialize it to
have enough capacity to be written to by the unmanaged function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top