Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ MFC
I have an application that needs to read some ascii commands from a text file and then output them one line at a time to a virtual printer which is a USB parallel port. The device is named "Generic / Text Only" and it is accessed thru a port named "USB001" using a driver named "USB Printing Support". I can use CreateDC with the above info and then use the dc to draw text etc but it adds a whole bunch of overhead. Is there any plain text method that I can use? ( remember Print & LPrint??) Just in case you're interested, I have to write one line at a time because the virtual device has a very limited buffer.
Any help will be appreciated!
OK, Maybe too much information clouded the question. Let me restate it!
I have a MSVC++ MFC application that needs to write null terminated C-style strings to a virtual printer attached via a USB to Parallel port device. The device name is "Generic / text only" using a driver named "USB Printing Support" on a port named "USB001". What is the best way to get the strings to the printer?
Thanks again for looking!
Posted 21-May-13 4:11am
Edited 22-May-13 0:18am
Sergey Alexandrovich Kryukov at 21-May-13 10:08am
Why? commands, in a text file? Does not look like a reasonable design.
What CreateDC, if this is text-only? Makes no sense at all.
Richard MacCutchan at 22-May-13 5:26am
What is the best way to get strings to a printer?
Just write them to the Printer DC (assuming that is how you access the device). Your question seems aimed more at the writers of the printer driver, maybe you should talk to them.
CPallini at 22-May-13 5:28am
Cannot you open the device using CreateFile and then send text via WriteFile?
Ron Harshbarger at 22-May-13 5:49am
I tried to use the same technique that I use all the time to write to a com port ( CreateFile, writefile ) but I get an invalid handle. I can create a DC using the above info ( I was chastised here for suggesting that I had done that - see above ) and I can draw the text using textout etc but that creates a lot of extra overhead when all I really need is to write simple text strings..
Ron H.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can try to use CreateFile() to open the device and write to it:
HANDLE hDevice = ::CreateFile(_T("\\\\.\\USB001"), 
    LPCSTR lpszTest = "Test";
    DWORD dwWritten = 0;
    ::WriteFile(hDevice, lpszTest, strlen(lpszTest), &dwWritten, NULL);
Note that it may be necessary to change the device name to something else (_T("\\Device\\<name>") or the the long version like 'usb#vid...&pid..&...&CLSID'). If you got the correct name, this should work.
Ron Harshbarger at 22-May-13 6:58am
I have used essentially that code with every possible name that I could think of but just get "INVALID_HANDLE". To test the connection, I open Notepad and type a few lines then print to the virtual printer and it works fine if you don't mind all the extra header,footer, white space... etc. I've tried the "\\Device\\USB001" and even the long name with the same results.
Jochen Arndt at 22-May-13 7:13am
Then you have to go the hard way to find the correct name by enumerating the USB devices. This MSDN article shows how to do the enumeration and get the device names:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 370
2 Maciej Los 285
3 ProgramFOX 275
4 CHill60 260
0 OriginalGriff 360
1 ProgramFOX 265
2 Maciej Los 235
3 CHill60 200
4 RyanDev 170

Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100