Tutorial :Retrieving Device Context from .NET print API



Question:

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?


Solution:1

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).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »