Click here to Skip to main content
12,996,841 members (134,751 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 3:11am
Updated 21-May-13 23:18pm
Sergey Alexandrovich Kryukov 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 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 22-May-13 5:28am
Cannot you open the device using CreateFile and then send text via WriteFile?
Ron Harshbarger 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 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2017
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