By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,333 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,333 IT Pros & Developers. It's quick & easy.

ReadFileEx callback doesn't work - simple question

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: 1
egl
You need to call SleepEx API and set Alertable to 1 or True right after your call to ReadFileEx. This will get you into that callback. regards - egl
Feb 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.