VC++ pointers with Readfile function [modified]
-
I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4: h-file ... DWORD ActualBytesRead DWORD * pActualBytesRead ... cpp-file attempt 1: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call? attempt 2: ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL) -compiles (passing by reference?) attempt 3: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile? OK, so I have a version of code that compiles but I don't really understand why attempt 1 & 3 fail?
modified on Tuesday, September 8, 2009 11:06 AM
-
I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4: h-file ... DWORD ActualBytesRead DWORD * pActualBytesRead ... cpp-file attempt 1: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call? attempt 2: ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL) -compiles (passing by reference?) attempt 3: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile? OK, so I have a version of code that compiles but I don't really understand why attempt 1 & 3 fail?
modified on Tuesday, September 8, 2009 11:06 AM
1 and 3 fails because you're passing a
DWORD**
instead ofDWORD*
. The following should compile.DWORD ActualBytesRead = 0;
BYTES ReceiverBuffer[1024];
DWORD NumberOfBytes2Read = 1024;ReadFileStatus = ReadFile(SerialPortHandle, ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL);
«_Superman_» I love work. It gives me something to do between weekends.
-
I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4: h-file ... DWORD ActualBytesRead DWORD * pActualBytesRead ... cpp-file attempt 1: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call? attempt 2: ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL) -compiles (passing by reference?) attempt 3: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile? OK, so I have a version of code that compiles but I don't really understand why attempt 1 & 3 fail?
modified on Tuesday, September 8, 2009 11:06 AM
GC104 wrote:
ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -won't compile, even though I am passing a pointer to a 'DWORD'?
This compiles for me.
GC104 wrote:
ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile?
Because argument 4 is the contents, or value, of
pActualBytesRead
."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
1 and 3 fails because you're passing a
DWORD**
instead ofDWORD*
. The following should compile.DWORD ActualBytesRead = 0;
BYTES ReceiverBuffer[1024];
DWORD NumberOfBytes2Read = 1024;ReadFileStatus = ReadFile(SerialPortHandle, ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL);
«_Superman_» I love work. It gives me something to do between weekends.
«_Superman_» wrote:
1 and 3 fails because you're passing a DWORD** instead of DWORD*.
Actually, in the first case he is passing a DWORD* (so this should compile) and in the third case he is passing a DWORD.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
GC104 wrote:
ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -won't compile, even though I am passing a pointer to a 'DWORD'?
This compiles for me.
GC104 wrote:
ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile?
Because argument 4 is the contents, or value, of
pActualBytesRead
."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Since posting the original message, I would expect attempt 1 to compile. Is this likely to be a quirk in the VS2005 VC++ compiler? Many thanks for thoughts. Geoff :)
Without seeing the actual error message, I've no idea.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Without seeing the actual error message, I've no idea.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons