How to open a printer as a file handle in c sharp

A

Andrew Falanga

Hi,

I have a case where I need to write, in binary form, pre-processed/
print-ready files to a printer. I tried to open the printer by using
the share name but got this error when I did so:

FileStream was asked to open a device that was not a file. For support
for devices like 'com1:' or 'lpt1:', call CreateFile, then use the
FileStream constructors that take an OS handle as an IntPtr.

The file I need to write to the printer is print ready, i.e. I do not
have to pass the file through a print driver. How would I use the
objects referenced in the exception thrown above to do this? What is
it talking about?

Thanks,
Andy
 
P

Peter Duniho

Andrew said:
[...]
FileStream was asked to open a device that was not a file. For support
for devices like 'com1:' or 'lpt1:', call CreateFile, then use the
FileStream constructors that take an OS handle as an IntPtr.

The file I need to write to the printer is print ready, i.e. I do not
have to pass the file through a print driver. How would I use the
objects referenced in the exception thrown above to do this? What is
it talking about?

CreateFile() is a function in the Win32 unmanaged API. If you really
want to implement it this way, you need to use p/invoke to call that
function, then pass the resulting handle to the appropriate FileStream
constructor.

Pete
 
A

Andrew Falanga

Andrew said:
[...]
FileStream was asked to open a device that was not a file. For support
for devices like 'com1:' or 'lpt1:', call CreateFile, then use the
FileStream constructors that take an OS handle as an IntPtr.
The file I need to write to the printer is print ready, i.e. I do not
have to pass the file through a print driver.  How would I use the
objects referenced in the exception thrown above to do this?  What is
it talking about?

CreateFile() is a function in the Win32 unmanaged API.  If you really
want to implement it this way, you need to use p/invoke to call that
function, then pass the resulting handle to the appropriate FileStream
constructor.

Pete

Ok, thanks. Some google searches had revealed this to me. I've used
PInvoke before, it's fun.

Thanks again,
Andy
 
A

Andrew Falanga

Andrew said:
[...]
FileStream was asked to open a device that was not a file. For support
for devices like 'com1:' or 'lpt1:', call CreateFile, then use the
FileStream constructors that take an OS handle as an IntPtr.
The file I need to write to the printer is print ready, i.e. I do not
have to pass the file through a print driver.  How would I use the
objects referenced in the exception thrown above to do this?  What is
it talking about?

CreateFile() is a function in the Win32 unmanaged API.  If you really
want to implement it this way, you need to use p/invoke to call that
function, then pass the resulting handle to the appropriate FileStream
constructor.

Pete

Actually, I should have asked, how should I be doing this? Your
closing sentence makes me think there may be a better way.

Andy
 
P

Peter Duniho

Andrew said:
Actually, I should have asked, how should I be doing this? Your
closing sentence makes me think there may be a better way.

Unfortunately, I don't have specific details off the top of my head.
But my recollection is that the print spool manager has a way to submit
a print job that is simply raw data.

Barring that, I know that from the unmanaged API there is an Escape()
function that also allows transmission of raw data to the printer.

Either approach would avoid having to go down to the file handle level
of the printer, preserving all the benefits of working through the
printer driver, including spooling support and i/o device independence
(i.e. your program wouldn't need to know the port the printer is
attached to).

It's not really a .NET/C# question. You can probably get more specifics
asking for them in a forum specifically for unmanaged Windows code, or
even unmanaged Windows printing code.

Pete
 
A

Andrew Falanga

Unfortunately, I don't have specific details off the top of my head.
But my recollection is that the print spool manager has a way to submit
a print job that is simply raw data.

Barring that, I know that from the unmanaged API there is an Escape()
function that also allows transmission of raw data to the printer.

Either approach would avoid having to go down to the file handle level
of the printer, preserving all the benefits of working through the
printer driver, including spooling support and i/o device independence
(i.e. your program wouldn't need to know the port the printer is
attached to).

It's not really a .NET/C# question.  You can probably get more specifics
asking for them in a forum specifically for unmanaged Windows code, or
even unmanaged Windows printing code.

Pete

ok, thanks Pete.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top