dll import problem

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 ?
 
M

Mattias Sjögren

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

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



Mattias
 
G

Guest

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 ?
 
W

Willy Denoyette [MVP]

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.
 

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