Click to See Complete Forum and Search --> : WritefileEx


Jokhan
March 21st, 2006, 09:41 AM
Hi All,

I have problem in printing data on usb port. The code I am using is like this:

m_hPrinter = CreateFile (deviceInterfaceDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE, // Only read access
0, // FILE_SHARE_READ | FILE_SHARE_WRITE
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
FILE_FLAG_OVERLAPPED, // No special attributes
NULL); // No template file

And then using writefileex to write the data on above file:

WriteFileEx(m_hPrinter, pcInput, uLength, O_Olap,FileIOCompletionRoutine);

pcInput is unsigned char* and uLength is the length of that string. pcInput I am getting from some other function. I want to modify pcInput and want to append some characters.... for that shake I do like this:

char check[150];
int count2;
for ( count2 =0; count2<uLength; count2++)
check[count2] = pcInput[count2];
check[count2] = '\n';
count2++;
check[count2] = '\0';

pcInput = (unsigned char*)check;
Now when I am passing pcInput to WritefileEx... printer is printing garbage... When I am not modifying pcInput.. printer prints properly.. but without changing lines..............

Though it works fine with lpt1 port properly.

Please give me suggestion to modify that string to get proper print..on USB port..

Thanks,
Jokhan

Brenton S.
March 21st, 2006, 02:36 PM
How are you trying to modify pcInput? Do you want a newline between each character?

Jokhan
March 22nd, 2006, 12:41 AM
Hi Brenton,
I am trying to modify pcInput by copying each character of it into an array and then adding at the end a new line.. and then sending it to WritefileEx.... as I have mentioned in previous details.

char check[150];
int count2;
for ( count2 =0; count2<uLength; count2++)
check[count2] = pcInput[count2];
check[count2] = '\n';
count2++;
check[count2] = '\0';

pcInput = (unsigned char*)check

Thanks,
Jokhan

philkr
March 22nd, 2006, 05:35 AM
It is just a guess, but maybe you have to add "\r\n" instead of only adding "\n".

Brenton S.
March 22nd, 2006, 07:07 AM
I agree with philkr but I also suggest you use lstrcat to append "\r\n" to the end of pcInput.

ex:

lstrcat(pcInput, "\r\n");

instead of:

char check[150];
int count2;
for ( count2 =0; count2<uLength; count2++)
check[count2] = pcInput[count2];
check[count2] = '\n';
count2++;
check[count2] = '\0';

pcInput = (unsigned char*)check;

Jokhan
March 24th, 2006, 01:14 AM
Thanks A lot!!!
My New line problem is solved.
But With write fileEx : buffer is getting overlapped? How to resolve this? If I am using waitforsingleobj, for which even should i wait for?
If i put small delay in deleting buffer it works fine!! but how to do without putting delay?

WriteFileEx(m_hPrinter, pcInput, uLength, O_Olap,FileIOCompletionRoutine);


Thanks & Regards,
Jokhan