Reusing Printer HDC for Multiple Documents in WinUI2 Application

Shyam Butani 400 Reputation points
2025-08-18T07:46:17.33+00:00

I'm developing a WinUI2 (WinRT/C++) application, and we plan to include an option to print documents using the available printers. We want to avoid using the system's default Printer Dialog box and are instead creating our own custom solution.

Currently, I'm obtaining the printer HDC using CreateDC, and then printing the document using the sequence: StartDoc -> StartPage -> ... -> EndPage -> EndDoc. My question is whether it’s possible to use the same printer HDC to print multiple documents without calling DeleteDC in between, or should I call DeleteDC after printing each document and create a new CreateDC for each subsequent document?

The code flow would look something like this:

HDC hdc = CreateDC(...);

DOCINFO di = {};
di.cbSize = sizeof(DOCINFO);

// First document
StartDoc(hdc, &di);
StartPage(hdc); 
// draw bitmap using BitBlt... 
EndPage(hdc);
EndDoc(hdc);

// Second document
StartDoc(hdc, &di);
StartPage(hdc);
// draw bitmap using BitBlt... 
EndPage(hdc);
EndDoc(hdc);

DeleteDC(hdc);

Basically, I have a few questions:

  • Can we use the same printer HDC to print multiple documents?
  • Is it possible to hold an HDC (i.e., not calling DeleteDC) for a period of time? For example, can I print one document, perform some other tasks, and then print another document using the same HDC—possibly on a different thread?

If this is possible, my goal is to obtain the printer HDC once, store it, and reuse it for future printing tasks.

Thanks in advance for any insights, help, or suggestions!

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Harry Vo (WICLOUD CORPORATION) 1,315 Reputation points Microsoft External Staff
    2025-08-19T02:51:41.9266667+00:00

    Hi @Shyam Butani , thank you for reaching out on Microsoft Q&A!

    According to this documentation:

    When an application has finished printing, it deletes the printer DC by calling the DeleteDC function. An application must delete (rather than release) a printer DC; the ReleaseDC function fails when an application attempts to use it to release a printer DC.

    And in this document:

    When you no longer need the DC, call the DeleteDC function.

    These mean that you have to delete the printer DC after you create and finish your job.


    Because of this, the supported workflow for printing is like this way based on this document:

    1. CreateDC
    2. StartDocStartPage → [drawing] → EndPageEndDoc
    3. DeleteDC

    The documentation doesn’t say how many times you can call StartDoc and EndDoc in one workflow. However, the StartDocA function document mentions that:

    Applications should call the StartDoc function immediately before beginning a print job. Using this function ensures that multipage documents are not interspersed with other print jobs.

    Based on this doc, I believe that each print job should have its own lifecycle, starting with CreateDC and ending with DeleteDC. It’s likely the safest way to keep documents separate and avoid mixing pages between jobs.


    Overall, I don't think this is required by any official documentations. However, following this pattern is a good practice to ensure reliable printing.

    I'm sorry for the misread before, I hope this edited explanation will help you fully understand this problem. Thank you!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.