ReadFileEx callback doesn't work - simple question

C

christian

Hi all,

After having checked some of these threads out I realized that you
guys here on the forum probably will solve my problem in no-time,
while it is a tough problem for me.

In your answers - please be explainatory and tell the obvious as I am
a hardware guy trying to make a sw driver to a com-port. That's
another way of saying "I kinda suck at programming" :)

Ok - here goes:

I first used ReadFile and it worked nicely. Then I exchange it with
ReadFileEx and try and interface it like this: (I've rearranged the
code in a try to make it simple to interpret)

//First I have a callback declared as:
private callback my_callback;
//And it looks like this:
private delegate void callback(int nErrorCode,
int nNumberOfBytesTransfered,
ref OVERLAPPED lpOverlapped);
//When I create my file I also create the callback function:
my_callback = new callback(this.this_happens_after_callback);
//And the input to "my_callback" is:
private void this_happens_after_callback(int nErrorCode,
int nNumberOfBytesTransfered,
ref OVERLAPPED lpOverlapped)
{
}
//After I have started the program I make a new lpOverlapped, and call
ReadFileEx:
OVERLAPPED ovlCommPort = new OVERLAPPED();
ReadFileEx(hComm,BufBytes,NumBytes,ref ovlCommPort, ref
my_callback);

//Some additional information:

//I create the file like this:
hComm = CreateFile("COM" + PortNum ,GENERIC_READ |
GENERIC_WRITE,0, 0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);

//My OVERLAPPED looks like this:
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED {
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
}
//And this is what the import looks like:
[DllImport("kernel32.dll")]
private static extern bool ReadFileEx(
int hFile, // filehandle
byte[] lpBuffer, // data buffer
int nNumberOfBytesToRead, // number of bytes to read
ref OVERLAPPED lpOverlapped, // overlapped buffer
ref callback lpCompletionRoutine // pointer to the completion
routine... I think.
);

Now for the (to me) strange part: I never enter the callback-function!
Why is this? That is why I don't have anything written in
"this_happens_after_callback" - it is useless as I never enter the
procedure...

I would be most grateful for help in this matter, as I've been trying
and trying for a whole day to make it work - without success...

Regards,
Christian
 

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