473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dll import problem

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 ?

Nov 17 '05 #1
3 1362
>but its'n work , what have i doing wrong ?

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
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 ?


"Mattias Sjögren" wrote:
but its'n work , what have i doing wrong ?


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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #3

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
5
by: Stef Mientki | last post by:
hello, The import statement "import sqlite3" gives the error given below. In simple programs, the import statement (sometimes) succeed, and I can indeed access the database. So I guess there is...
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.