Using the CreateFile, WriteFile, PurgeComm, and CloseHandle functions in Windows 7 64-bit. Are there

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have recently ported my C++ application to Windows 7 after running it successfully on XP for years. It uses CreateFile, WriteFile, PurgeComm, and CloseHandle Windows API functions to send data directly to a printer connected through a serial port. After
trying to run it in Windows 7 I am convinced that theres a memory leak somewhere in the code because the printer sometimes does not print or prints garbage characters. Is there a size problem I need to be aware when moving from the 32-bit environment of Windows
XP to the 64-bit environment of Windows 7? My WriteFile calls are similar to the following:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // print one line of data to the serial printer
<span style="color:Blue; bool SerialPrinter::PrintData(<span style="color:Blue; const <span style="color:Blue; char* <span style="color:Blue; const str)
{
LPCVOID lpBuffer = str;
DWORD dwNumberOfBytesToWrite = (DWORD)strlen(str);
DWORD dwNumberOfBytesWritten = 0;
LPDWORD lpNumberOfBytesWritten = &dwNumberOfBytesWritten;

BOOL write_file_result = WriteFile(<span style="color:Blue; this->hCOM,
lpBuffer,
dwNumberOfBytesToWrite,
lpNumberOfBytesWritten,
NULL);

<span style="color:Blue; if (write_file_result == FALSE)
{
<span style="color:Green; /* error checking code omitted */

<span style="color:Blue; return <span style="color:Blue; false;
}

<span style="color:Blue; return <span style="color:Blue; true;
}
[/code]
<br/>
Even if the function returns TRUE, could there still be a problem with the call? Are we certain that in a 64-bit environment that assignment of dwNumberOfBytesToWrite and dwNumberOfBytesWritten are correct?


View the full article
 
Back
Top