"ERROR_INVALID_PARAMETER": ReadFile() and WriteFile()

I am writing a program that connects to a serial port and writes data.  I 
follow the guides from MSDN and I use ReadFile() and WriteFile() to do data 
I/O.  The port is opened in non-overlapped I/O mode.  But whenever I call 
ReadFile() or WriteFile(), it returns error number 87, and when I look that 
up, it is the ERROR_INVALID_PARAMETER.  

So I made sure that everything was okay.  The buffer I pass to those 
functions is actually allocated and okay, and the variables for how many 
bytes to read/write, and the lpdword for how bytes were actually read/written 
are okay as well.  And the overlapped I/O option is of course set to NULL.  I 
can only figure that my file handle to the serial port is not okay.  

But it does not make sense because I can open and close that file handle, 
and when I use the PortMon serial port sniffer tool, I can see the events 
happening properly (see log below).

Anyone have any ideas why this might be happening?  I am really lost on this 
one.  Thanks for taking the time to read this.


Port Sniffer Output (Port opened, and then set for 9600,N,8,1):
----------------------------------------------------------------------
0.00004855	NMEAInterface.e	IRP_MJ_CREATE	Serial0	SUCCESS	Options: Open 	
0.00000279	NMEAInterface.e	IOCTL_SERIAL_GET_BAUD_RATE	Serial0	SUCCESS		
0.00000099	NMEAInterface.e	IOCTL_SERIAL_GET_LINE_CONTROL	Serial0	SUCCESS		
0.00000080	NMEAInterface.e	IOCTL_SERIAL_GET_CHARS	Serial0	SUCCESS		
0.00000079	NMEAInterface.e	IOCTL_SERIAL_GET_HANDFLOW	Serial0	SUCCESS		
0.00000074	NMEAInterface.e	IOCTL_SERIAL_GET_BAUD_RATE	Serial0	SUCCESS		
0.00000077	NMEAInterface.e	IOCTL_SERIAL_GET_LINE_CONTROL	Serial0	SUCCESS		
0.00000073	NMEAInterface.e	IOCTL_SERIAL_GET_CHARS	Serial0	SUCCESS		
0.00000073	NMEAInterface.e	IOCTL_SERIAL_GET_HANDFLOW	Serial0	SUCCESS		
0.00000876	NMEAInterface.e	IOCTL_SERIAL_SET_BAUD_RATE	Serial0	SUCCESS	Rate: 
9600	
0.00000394	NMEAInterface.e	IOCTL_SERIAL_SET_RTS	Serial0	SUCCESS		
0.00000387	NMEAInterface.e	IOCTL_SERIAL_SET_DTR	Serial0	SUCCESS		
0.00000238	NMEAInterface.e	IOCTL_SERIAL_SET_LINE_CONTROL	Serial0	SUCCESS	StopBits: 1 Parity: NONE WordLength: 8	
0.00000100	NMEAInterface.e	IOCTL_SERIAL_SET_CHAR	Serial0	SUCCESS	EOF:0 ERR:0 
BRK:0 EVT:0 XON:11 XOFF:13	
0.00000262	NMEAInterface.e	IOCTL_SERIAL_SET_HANDFLOW	Serial0	SUCCESS	Shake:1 
Replace:40 XonLimit:2048 XoffLimit:512	
0.00000269	NMEAInterface.e	IOCTL_SERIAL_SET_TIMEOUTS	Serial0	SUCCESS	RI:1000 
RM:1000 RC:1000 WM:1000 WC:1000	



The open function:
--------------------------------------------------
BOOL CSerialPort::Open(CString strPortNum)
{
	m_hSerialPort = CreateFile(strPortNum,					 // File Name
							   GENERIC_READ | GENERIC_WRITE, // Access Settings
							   0,							 // Sharing
							   0,							 // Security Settings
							   OPEN_EXISTING,				 // How to Open the File
							   FILE_FLAG_OVERLAPPED,		 // Flags and Attributes
							   0);							 // Template File

	if ( m_hSerialPort == INVALID_HANDLE_VALUE )
	{
		int errorNum = GetLastError();
		TRACE("CNDKSerialPort::Open -- Could not open the port, error #%d\n", 
errorNum);
		return FALSE;
	}	
	else
		return TRUE;
}





Here is the Read function:
--------------------------------------------------
DWORD CNDKSerialPort::ReadData(unsigned char buffer[], DWORD dwNumBytesToRead)
{
	DWORD dwNumBytesRead = 0;
	BOOL readStatus = ReadFile(m_hSerialPort, buffer, dwNumBytesToRead, 
&dwNumBytesRead, NULL);
	if (readStatus == FALSE)
	{
		TCHAR szBuf[80]; 
		LPVOID lpMsgBuf;

		DWORD errorNum = GetLastError();
		TRACE("CSerialPort::ReadData -- Could not read successfully, error #%d\n", 
errorNum);
		FormatMessage(  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
						NULL,
						errorNum,
						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
						(LPTSTR) &lpMsgBuf,
						0, NULL );

		wsprintf(szBuf, "Failed with error %d: %s", errorNum, lpMsgBuf); 
		AfxMessageBox(szBuf);

	}
	return dwNumBytesRead;
}






Here is the Write function:
--------------------------------------------------
DWORD CSerialPort::WriteData(unsigned char buffer[], DWORD dwNumBytesToWrite)
{
	DWORD dwNumBytesWritten = 0;
	BOOL writeStatus = WriteFile(m_hSerialPort, buffer, dwNumBytesToWrite, 
&dwNumBytesWritten, NULL);
	if (writeStatus == FALSE)
	{
		TCHAR szBuf[80]; 
		LPVOID lpMsgBuf;
		DWORD errorNum = GetLastError();
		TRACE("CSerialPort::WriteData -- Could not write successfully, error 
#%d\n", errorNum);
		FormatMessage(  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
						NULL,
						errorNum,
						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
						(LPTSTR) &lpMsgBuf,
						0, NULL );

		wsprintf(szBuf, "Failed with error %d: %s", errorNum, lpMsgBuf); 
		AfxMessageBox(szBuf);
	}
	return dwNumBytesWritten;
}

0
Kazuhiro (5)
8/18/2005 2:45:01 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
1932 Views

Similar Articles

[PageSpeed] 0

Kazuhiro wrote:
> I am writing a program that connects to a serial port and writes data.  I 
> follow the guides from MSDN and I use ReadFile() and WriteFile() to do data 
> I/O.  The port is opened in non-overlapped I/O mode.  But whenever I call 
> ReadFile() or WriteFile(), it returns error number 87, and when I look that 
> up, it is the ERROR_INVALID_PARAMETER.  

Code typo or newsgroup typo: ?

You saved the CreateFile handle in CSerialPort::m_hSerialPort
But in Write File you use the handle CNDKSerialPort::m_hSerialPort

-- 
Scott McPhillips [VC++ MVP]

0
Scott
8/18/2005 5:03:49 AM
Sorry, that was a typo of my own.  I copied it from a backup, but I changed 
the name later and forgot about that.  Sorry for that.  It is the same handle 
though.  I have checked, and the file handle does not change values during 
the execution of the program after opening it.

"Scott McPhillips [MVP]" wrote:

> Kazuhiro wrote:
> > I am writing a program that connects to a serial port and writes data.  I 
> > follow the guides from MSDN and I use ReadFile() and WriteFile() to do data 
> > I/O.  The port is opened in non-overlapped I/O mode.  But whenever I call 
> > ReadFile() or WriteFile(), it returns error number 87, and when I look that 
> > up, it is the ERROR_INVALID_PARAMETER.  
> 
> Code typo or newsgroup typo: ?
> 
> You saved the CreateFile handle in CSerialPort::m_hSerialPort
> But in Write File you use the handle CNDKSerialPort::m_hSerialPort
> 
> -- 
> Scott McPhillips [VC++ MVP]
> 
> 
0
Kazuhiro (5)
8/18/2005 6:17:02 AM
> I/O.  The port is opened in non-overlapped I/O mode.  But whenever I call

> m_hSerialPort = CreateFile(strPortNum, // File Name
>    GENERIC_READ | GENERIC_WRITE, // Access Settings
>    0, // Sharing
>    0, // Security Settings
>    OPEN_EXISTING, // How to Open the File
>    FILE_FLAG_OVERLAPPED, // Flags and Attributes
>    0); // Template File

obviously it is opened in overlapped mode ....


0
Cow7412 (14)
8/18/2005 11:36:46 AM
Wow, it was right there all along, feeling quite foolish right now.  Might I 
convince you that it was because I was tired...?  :-)  No?  Okay just thought 
I would try.

Thanks for catching that though, I didn't even realize it.  Thanks again.
0
Kazuhiro (5)
8/19/2005 12:30:07 AM
Just a followup.  Remeber, as soon as you write wsprintf you have made a coding error. Why
are you doing this?  CString::Format would have worked just fine, and not had the
potential for buffer overflow (apparently you think 80 is a sensible length for an error
message string; there's nothing I'm aware of that causes this to make sense). 

I also note that you do not free the buffer returned by FormatMessage.  You have to free
it or you have a memory leak (and it won't be found by the C/C++ runtime, but it is there
nonetheless)
				joe

On Wed, 17 Aug 2005 19:45:01 -0700, "Kazuhiro" <Kazuhiro@discussions.microsoft.com> wrote:

>I am writing a program that connects to a serial port and writes data.  I 
>follow the guides from MSDN and I use ReadFile() and WriteFile() to do data 
>I/O.  The port is opened in non-overlapped I/O mode.  But whenever I call 
>ReadFile() or WriteFile(), it returns error number 87, and when I look that 
>up, it is the ERROR_INVALID_PARAMETER.  
>
>So I made sure that everything was okay.  The buffer I pass to those 
>functions is actually allocated and okay, and the variables for how many 
>bytes to read/write, and the lpdword for how bytes were actually read/written 
>are okay as well.  And the overlapped I/O option is of course set to NULL.  I 
>can only figure that my file handle to the serial port is not okay.  
>
>But it does not make sense because I can open and close that file handle, 
>and when I use the PortMon serial port sniffer tool, I can see the events 
>happening properly (see log below).
>
>Anyone have any ideas why this might be happening?  I am really lost on this 
>one.  Thanks for taking the time to read this.
>
>
>Port Sniffer Output (Port opened, and then set for 9600,N,8,1):
>----------------------------------------------------------------------
>0.00004855	NMEAInterface.e	IRP_MJ_CREATE	Serial0	SUCCESS	Options: Open 	
>0.00000279	NMEAInterface.e	IOCTL_SERIAL_GET_BAUD_RATE	Serial0	SUCCESS		
>0.00000099	NMEAInterface.e	IOCTL_SERIAL_GET_LINE_CONTROL	Serial0	SUCCESS		
>0.00000080	NMEAInterface.e	IOCTL_SERIAL_GET_CHARS	Serial0	SUCCESS		
>0.00000079	NMEAInterface.e	IOCTL_SERIAL_GET_HANDFLOW	Serial0	SUCCESS		
>0.00000074	NMEAInterface.e	IOCTL_SERIAL_GET_BAUD_RATE	Serial0	SUCCESS		
>0.00000077	NMEAInterface.e	IOCTL_SERIAL_GET_LINE_CONTROL	Serial0	SUCCESS		
>0.00000073	NMEAInterface.e	IOCTL_SERIAL_GET_CHARS	Serial0	SUCCESS		
>0.00000073	NMEAInterface.e	IOCTL_SERIAL_GET_HANDFLOW	Serial0	SUCCESS		
>0.00000876	NMEAInterface.e	IOCTL_SERIAL_SET_BAUD_RATE	Serial0	SUCCESS	Rate: 
>9600	
>0.00000394	NMEAInterface.e	IOCTL_SERIAL_SET_RTS	Serial0	SUCCESS		
>0.00000387	NMEAInterface.e	IOCTL_SERIAL_SET_DTR	Serial0	SUCCESS		
>0.00000238	NMEAInterface.e	IOCTL_SERIAL_SET_LINE_CONTROL	Serial0	SUCCESS	StopBits: 1 Parity: NONE WordLength: 8	
>0.00000100	NMEAInterface.e	IOCTL_SERIAL_SET_CHAR	Serial0	SUCCESS	EOF:0 ERR:0 
>BRK:0 EVT:0 XON:11 XOFF:13	
>0.00000262	NMEAInterface.e	IOCTL_SERIAL_SET_HANDFLOW	Serial0	SUCCESS	Shake:1 
>Replace:40 XonLimit:2048 XoffLimit:512	
>0.00000269	NMEAInterface.e	IOCTL_SERIAL_SET_TIMEOUTS	Serial0	SUCCESS	RI:1000 
>RM:1000 RC:1000 WM:1000 WC:1000	
>
>
>
>The open function:
>--------------------------------------------------
>BOOL CSerialPort::Open(CString strPortNum)
>{
>	m_hSerialPort = CreateFile(strPortNum,					 // File Name
>							   GENERIC_READ | GENERIC_WRITE, // Access Settings
>							   0,							 // Sharing
>							   0,							 // Security Settings
>							   OPEN_EXISTING,				 // How to Open the File
>							   FILE_FLAG_OVERLAPPED,		 // Flags and Attributes
>							   0);							 // Template File
>
>	if ( m_hSerialPort == INVALID_HANDLE_VALUE )
>	{
>		int errorNum = GetLastError();
>		TRACE("CNDKSerialPort::Open -- Could not open the port, error #%d\n", 
>errorNum);
>		return FALSE;
>	}	
>	else
>		return TRUE;
>}
>
>
>
>
>
>Here is the Read function:
>--------------------------------------------------
>DWORD CNDKSerialPort::ReadData(unsigned char buffer[], DWORD dwNumBytesToRead)
>{
>	DWORD dwNumBytesRead = 0;
>	BOOL readStatus = ReadFile(m_hSerialPort, buffer, dwNumBytesToRead, 
>&dwNumBytesRead, NULL);
>	if (readStatus == FALSE)
>	{
>		TCHAR szBuf[80]; 
>		LPVOID lpMsgBuf;
>
>		DWORD errorNum = GetLastError();
>		TRACE("CSerialPort::ReadData -- Could not read successfully, error #%d\n", 
>errorNum);
>		FormatMessage(  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
>						NULL,
>						errorNum,
>						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
>						(LPTSTR) &lpMsgBuf,
>						0, NULL );
>
>		wsprintf(szBuf, "Failed with error %d: %s", errorNum, lpMsgBuf); 
>		AfxMessageBox(szBuf);
>
>	}
>	return dwNumBytesRead;
>}
>
>
>
>
>
>
>Here is the Write function:
>--------------------------------------------------
>DWORD CSerialPort::WriteData(unsigned char buffer[], DWORD dwNumBytesToWrite)
>{
>	DWORD dwNumBytesWritten = 0;
>	BOOL writeStatus = WriteFile(m_hSerialPort, buffer, dwNumBytesToWrite, 
>&dwNumBytesWritten, NULL);
>	if (writeStatus == FALSE)
>	{
>		TCHAR szBuf[80]; 
>		LPVOID lpMsgBuf;
>		DWORD errorNum = GetLastError();
>		TRACE("CSerialPort::WriteData -- Could not write successfully, error 
>#%d\n", errorNum);
>		FormatMessage(  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
>						NULL,
>						errorNum,
>						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
>						(LPTSTR) &lpMsgBuf,
>						0, NULL );
>
>		wsprintf(szBuf, "Failed with error %d: %s", errorNum, lpMsgBuf); 
>		AfxMessageBox(szBuf);
>	}
>	return dwNumBytesWritten;
>}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
8/20/2005 10:07:12 PM
Reply:

Similar Artilces:

CreateProcessAsUserW always returns ERROR_INVALID_PARAMETER
Hi all, I have the following function that I execute from a service running on win2003 terminal server. This code creates a process in a client's terminal session. I got this to work on win 2000 server. But when I run in 2003 I always keep getting ERROR_INVALID_PARAMETER from the function CreateProcessAsUserW . I verified the code and every thing seems right. I am trying to create some thing as simple as c:\w2k3\notepad.exe and it still gives this error. Any body with ideas please help me out int createTSProcess(char* userName,char* domain,char* passwd,char* commandStr,char* processDir,lo...

"ERROR_INVALID_PARAMETER": ReadFile() and WriteFile()
I am writing a program that connects to a serial port and writes data. I follow the guides from MSDN and I use ReadFile() and WriteFile() to do data I/O. The port is opened in non-overlapped I/O mode. But whenever I call ReadFile() or WriteFile(), it returns error number 87, and when I look that up, it is the ERROR_INVALID_PARAMETER. So I made sure that everything was okay. The buffer I pass to those functions is actually allocated and okay, and the variables for how many bytes to read/write, and the lpdword for how bytes were actually read/written are okay as well. And the over...