views:

406

answers:

2

I am working on a .NET application to control DataCard Desktop Card printers, and I have stumbled upon a problem. In order to execute Magnetic Stripe and Chip encoding, I have to call a function in a native printer API dll to enable the printers Interactive mode.

I have managed to create the P/Invoke code to call this native function. But the problem is that this method takes the printer Device Context handle as a parameter, and have to be called BEFORE the Gdi32 StartDoc function is called.

In the .NET printing API I do have access to the hDC from the Graphics object. But as far as I can see the Graphics object is only availible AFTER the StartDoc function is invoked.

So my question is if anyone knows a way to retrieve the context before StartDoc is called?

+1  A: 

You can do a OpenPrinter to retrieve a printer handle (HANDLE) and then call CreateDC by passing in this handle to get a printer DC anytime (before StartDoc).

dirkgently
I assume you mean the "winspool.drv" and "gdi32.dll" functions? I am aware that this is not difficult to do using the native C apis. But my application is a .NET application, so I would hope I could use the .NET api as far as possible.
Johnny Egeland
You are already using P/Invoke which is why I ventured this reply. Mention your needs clearly in the question.
dirkgently
I think I clearly mentioned that my problem was with the .NET printing API. However writing my own p/invoke wrapper for Gdi32 seems to be the only way to achieve this.
Johnny Egeland
A: 

It seems dirkgently's suggestion is the only possible solution to this. I disassembled the .NET Printing features, and the creation of the DC is well hidden away in internal methods. Also StartDoc is called immediately after the DC creation, so there is no way to achieve this.

So I'm off to writing my own version of yet another insufficient .NET API feature :-/

Johnny Egeland