Passing buffer to unmanaged dll.

C

Claire

After giving up on passing nested structs to an unmanaged DLL, I planned
that I'd pass a simple buffer of bytes (the same size as the struct) to the
dll, convert to a memory stream and read in the fields from that.

I've 2 possible DLls to use, one is emulated and the other one is the "real"
one
The emulated one (that I have to use on a desktop) has a function declared
as follows in win32 delphi. The Delphi version of the code works fine for
both dlls.

The recRDetails record is 60880 bytes in size. It's fields are filled by the
dll.
{$IFDEF EMULATOR}
TdFunction = function(var recRDetails: PRDetails): integer; stdcall;
{$ELSE}
{ This is the release DLL Line }
TdFunction = function(recRDetails: PRDetails): integer; cdecl;
{$ENDIF}

I've made a dll declaration as follows. I don't know if Ive done this
correctly. Ive tried both with and without "ref" and I get a SEHexception
with both.
[DllImport(DllName, CallingConvention=CallingConvention.StdCall)]
public static extern int d86NetOpen(byte[] buffer);

I attempt to call the dll function as follows
byte[] buff = new byte[60880];
DDll.dNetOpen( buff);

Ive been fiddling with this for so long I no longer know what the hell Im
actually doing. If someone can tell me what I should be doing for both cdecl
and stdcall versions Id be very happy. (I used to understand variable types
until I came to dotnet)

thanks.
 
W

Willy Denoyette [MVP]

Claire said:
After giving up on passing nested structs to an unmanaged DLL, I planned
that I'd pass a simple buffer of bytes (the same size as the struct) to
the dll, convert to a memory stream and read in the fields from that.

I've 2 possible DLls to use, one is emulated and the other one is the
"real" one
The emulated one (that I have to use on a desktop) has a function declared
as follows in win32 delphi. The Delphi version of the code works fine for
both dlls.

The recRDetails record is 60880 bytes in size. It's fields are filled by
the dll.
{$IFDEF EMULATOR}
TdFunction = function(var recRDetails: PRDetails): integer; stdcall;
{$ELSE}
{ This is the release DLL Line }
TdFunction = function(recRDetails: PRDetails): integer; cdecl;
{$ENDIF}

I've made a dll declaration as follows. I don't know if Ive done this
correctly. Ive tried both with and without "ref" and I get a SEHexception
with both.
[DllImport(DllName, CallingConvention=CallingConvention.StdCall)]
public static extern int d86NetOpen(byte[] buffer);

I attempt to call the dll function as follows
byte[] buff = new byte[60880];
DDll.dNetOpen( buff);

Ive been fiddling with this for so long I no longer know what the hell Im
actually doing. If someone can tell me what I should be doing for both
cdecl and stdcall versions Id be very happy. (I used to understand
variable types until I came to dotnet)

thanks.

When exactly is this SEHexception thrown and what does it hold?
When passing a byte array are sure the individual elements are correctly
aligned as expected by the callee?

Willy.
 

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

Top