Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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
Updated 21-May-13 23:18pm
v2
Comments
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.
—SA
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?
[no name] 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

You can try to use CreateFile() to open the device and write to it:
HANDLE hDevice = ::CreateFile(_T("\\\\.\\USB001"), 
    GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0);
if (INVALID_HANDLE_VALUE != hDevice)
{
    LPCSTR lpszTest = "Test";
    DWORD dwWritten = 0;
    ::WriteFile(hDevice, lpszTest, strlen(lpszTest), &dwWritten, NULL);
    ::CloseHandle(hDevice);
}

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.
 
Share this answer
 
Comments
[no name] 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: http://msdn.microsoft.com/en-us/library/windows/hardware/ff540174%28v=vs.85%29.aspx.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900