IJW - How exactly?

  • Thread starter Thread starter Austin Ehlers
  • Start date Start date
A

Austin Ehlers

(I apologize if this is a fairly trivial question, but I mostly use
C#, not MC++.)

Problem: I've got an unmanaged DLL (and the LIB file), that exports a
bunch of static functions. I'd like to be able to wrap this with a
__gc class, and then access it from C#. All of the examples I've seen
are based upon wrapping unmanaged code (H and CPP files), not existing
DLLs. I'm sure I'm missing something.

Thanks,
Austin Ehlers
 
Austin Ehlers said:
(I apologize if this is a fairly trivial question, but I mostly use
C#, not MC++.)

We won't hold that against you. :-)
Problem: I've got an unmanaged DLL (and the LIB file), that exports a
bunch of static functions. I'd like to be able to wrap this with a
__gc class, and then access it from C#. All of the examples I've seen
are based upon wrapping unmanaged code (H and CPP files), not existing
DLLs. I'm sure I'm missing something.

Well, I'll explain a couple of ways to attack the problem in the way you
suggest in a moment. But if all you want to do is to call unmanaged code in
a DLL from C# then there may be an easier way. Take a look at this:

http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx

Back to what you asked:

Of the MS .Net compilers, MC++ stands alone in its ability to compile both
managed and unmanaged code segments in the _same_ module. You delimit those
segments with

#pragma managed

and

#pragma unmanaged

In an unmanaged segment you call DLL exported functions in the usual way.
IJW handles the transitions between managed and unmanaged code invisibly.
So, managed code in a method of a _gc class can call into an unmanaged code
segment which calls a DLL function.

Alternatively, you can use the [DllImport] attribute to declare an external,
static function that resides in a DLL. In most cases, the compiler will
marshall the arguments and make the call without extra work on your part.

Regards,
Will
 
We won't hold that against you. :-)

Thanks :)
In an unmanaged segment you call DLL exported functions in the usual way.
IJW handles the transitions between managed and unmanaged code invisibly.
So, managed code in a method of a _gc class can call into an unmanaged code
segment which calls a DLL function.

There's my problem. I don't know how to call the DLL functions. Take
this definition:

my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql);
and the C# definition of it is:

[ DllImport( "libmySQL.dll", EntryPoint="mysql_affected_rows" )]
unsafe public static extern ulong mysql_affected_rows(IntPtr mysql);

which works. I'm thinking something like:

extern "C"
{
UInt64 mysql_affected_rows(IntPtr mysql);
}
but how do I make it reference the DLL?

Thanks,
Austin Ehlers
 
Austin said:
There's my problem. I don't know how to call the DLL functions. Take
this definition:

my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql);
and the C# definition of it is:

[ DllImport( "libmySQL.dll", EntryPoint="mysql_affected_rows" )]
unsafe public static extern ulong mysql_affected_rows(IntPtr mysql);

which works. I'm thinking something like:

extern "C"
{
UInt64 mysql_affected_rows(IntPtr mysql);
}
but how do I make it reference the DLL?

Just add the import library (libmySQL.lib) to your project. It defines
stub functions for the DLL that you can link into your project to
satisfy the extern declaration.

Jesse
 
Back
Top