dll import problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

i want to import this dll function to c#:

BOOL WINAPI
ReadData(
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead
);

I do it so:

[DllImport("MyAPI", SetLastError=true)]
private static extern bool ReadData(
byte[] lpBuffer,
uint nNumberOfBytesToRead,
ref uint lpNumberOfBytesRead,
);

and:

byte[] PacketData=new byte[1514];
uint nBytesRead=0;

bool reValue = ReadData(
PacketData,
(uint)PacketData.Length,
ref nBytesRead);

but its'n work , what have i doing wrong ?
 
but its'n work , what have i doing wrong ?

What exactly doesn't work? In what way is it failing?



Mattias
 
Hi Mattias,

i get error code 87: // invalid parameter

int lasterror=Marshal.GetLastWin32Error();

Console.WriteLine( "lasterror:"+lasterror );

The hole thing should be:

BOOL WINAPI
ReadData(
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);

I do it so:

[DllImport("PCAImAPI", SetLastError=true)]
private static extern bool ReadData(
byte[] lpBuffer,
uint nNumberOfBytesToRead,
ref uint lpNumberOfBytesRead,
// [MarshalAs(UnmanagedType.LPStruct)]
OVERLAPPED lpOverlapped
);

[DllImport("kernel32.dll")]
private static extern IntPtr CreateEvent(
IntPtr lpEventAttributes,
bool bManualReset,
bool bInitialState,
string lpName);


[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public UIntPtr Internal;
public UIntPtr InternalHigh;
public uint Offset;
public uint OffsetHigh;
public IntPtr EventHandle;
}


and:

OVERLAPPED Ovlp=new OVERLAPPED();
Ovlp.EventHandle=IntPtr.Zero;
Ovlp.Offset=0;
Ovlp.OffsetHigh=0;
Ovlp.EventHandle= CreateEvent(IntPtr.Zero, false, false,null);

reValue = ReadData(
PacketData,
(uint)PacketData.Length,
ref nBytesRead,
Ovlp
);


I think my ovlp struct is invalid, is'n it ?
 
centrino said:
Hi Mattias,

i get error code 87: // invalid parameter

int lasterror=Marshal.GetLastWin32Error();

Console.WriteLine( "lasterror:"+lasterror );

The hole thing should be:

BOOL WINAPI
ReadData(
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);

I do it so:

[DllImport("PCAImAPI", SetLastError=true)]
private static extern bool ReadData(
byte[] lpBuffer,
uint nNumberOfBytesToRead,
ref uint lpNumberOfBytesRead,
// [MarshalAs(UnmanagedType.LPStruct)]
OVERLAPPED lpOverlapped
);

[DllImport("kernel32.dll")]
private static extern IntPtr CreateEvent(
IntPtr lpEventAttributes,
bool bManualReset,
bool bInitialState,
string lpName);


[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public UIntPtr Internal;
public UIntPtr InternalHigh;
public uint Offset;
public uint OffsetHigh;
public IntPtr EventHandle;
}


and:

OVERLAPPED Ovlp=new OVERLAPPED();
Ovlp.EventHandle=IntPtr.Zero;
Ovlp.Offset=0;
Ovlp.OffsetHigh=0;
Ovlp.EventHandle= CreateEvent(IntPtr.Zero, false, false,null);

reValue = ReadData(
PacketData,
(uint)PacketData.Length,
ref nBytesRead,
Ovlp
);


I think my ovlp struct is invalid, is'n it ?

Ovelapped struct needs to be passed by ref.

private static extern bool ReadData(
.....
ref OVERLAPPED lpOverlapped
);

// Call site
....

ref nBytesRead,
ref Ovlp
);

Willy.
 
Back
Top