5

Short Version

When calling CreateDC/CreateIC with a redirected (i.e. remote desktop) printer, the calls fails (i.e. returns 0):

printerDC := CreateDC(nil, 'EPSON ET-2800 Series (redirected 2)', nil, nil);

Long Version

I am trying to call CreateDC/CreateIC for a printer that happens to be redirected by Remote Desktop Client:

printerDC := CreateDC(nil, 'EPSON ET-2800 Series (redirected 2)', nil, nil);

The code works in Windows 7, but fails in Windows 10 - returning NULL (i.e. zero)

I really am using a real printer (i.e. not a virtual print to file, Print to PDF virtual printer):

Screenshot of debugger inspecting printer name variable at runtime

But the same also fails for my other redirected remote printers; some "real" some "virtual":

CreateIC(…, 'Fax',                                          …) ⇒ 0xBF21704F
CreateIC(…, 'Xerox Phaser 6500N',                           …) ⇒ 0x85214A3E
CreateIC(…, 'Microsoft Print to PDF',                       …) ⇒ 0x76213A00
CreateIC(…, 'OneNote for Windows 10',                       …) ⇒ 0x162149B2
CreateIC(…, 'Brother MFC-7840W Printer',                    …) ⇒ 0xF62139F2
CreateIC(…, 'Microsoft XPS Document Writer',                …) ⇒ 0x6B214609
CreateIC(…, 'Send To OneNote 2010',                         …) ⇒ 0
CreateIC(…, 'Fax (redirected 2)',                           …) ⇒ 0
CreateIC(…, 'OneNote for Windows 10 (redirected 2)',        …) ⇒ 0
CreateIC(…, 'Colson on shielddesktop (redirected 2)',       …) ⇒ 0
CreateIC(…, 'Microsoft XPS Document Writer (redirected 2)', …) ⇒ 0
CreateIC(…, 'Microsoft Print to PDF (redirected 2)',        …) ⇒ 0
CreateIC(…, 'EPSON ET-2800 Series (redirected 2)',          …) ⇒ 0

But the call doesn't fail for "local" printers:

                                     ┊
╔════════════════════════════════════════════════╦═════════════╗
║ Printer name                       ┊           ║ Returned DC ║
╠════════════════════════════════════════════════╬═════════════╣
║ Fax                                ┊           ║ 0xBF21704F  ║
║ Fax (redirected 2)                 ┊           ║ 0           ║ 
║ Xerox Phaser 6500N                 ┊           ║ 0x85214A3E  ║
║ Send To OneNote 2010               ┊           ║ 0           ║ 
║ Microsoft Print to PDF             ┊           ║ 0x76213A00  ║
║ OneNote for Windows 10             ┊           ║ 0x162149B2  ║
║ Brother MFC-7840W Printer          ┊           ║ 0xF62139F2  ║
║ Microsoft XPS Document Writer      ┊           ║ 0x6B214609  ║
║ EPSON ET-2800 Series (redirected 2)┊           ║ 0           ║ 
║ Microsoft Print to PDF (redirected 2)          ║ 0           ║ 
║ OneNote for Windows 10 (redirected 2)          ║ 0           ║
║ Colson on shielddesktop (redirected 2)         ║ 0           ║
║ Microsoft XPS Document Writer (redirected 2)   ║ 0           ║ 
║ Microsoft XPS Document Writer (redirected 2)   ║ 0           ║ 
╚════════════════════════════════════════════════╩═════════════╝
                                     ┊

Research Effort

The MSDN documentation says that if you want to get a printer device context you must pass WINSPOOL as the driver:

How To: Retrieve a Printer Device Context

To render to a specific printer, you must specify "WINSPOOL" as the device and pass the correct name of the printer to CreateDC. You can also pass a DEVMODE structure in the call to CreateDC if you want to provide device-specific initialization data for the device driver when you create the device context.

The following example shows a call to CreateDC in which the "WINSPOOL" driver is selected and the printer name is specified by name. C++

printerDC = CreateDC( L"WINSPOOL", printerName, NULL, NULL);

But MSDN also recommends that you pass NULL for printer devices:

pwszDriver

A pointer to a null-terminated character string that specifies either DISPLAY or the name of a specific display device. For printing, we recommend that you pass NULL to lpszDriver because GDI ignores lpszDriver for printer devices.

I mention it, because there is a lot of code out there that passes:

  • empty string
  • null
  • the name of the device
  • the name of the driver
  • the literal winspool
  • the literal WINSPOOL

CRME

Here are the Complete Reproduciable Minimal Example for every possible combination of arguments i can think of:

  1. Passing recommended NULL as the driver:

    // MSDN: "For printing, we recommend that you pass NULL to lpszDriver because GDI ignores lpszDriver for printer devices."
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdca
    CreateDC(nil,        'Xerox Phaser 6500N',                           nil, nil) ==> 0x85214A3E
    CreateDC(nil,        'Send To OneNote 2010',                         nil, nil) ==> 0
    CreateDC(nil,        'OneNote for Windows 10',                       nil, nil) ==> 0x162149B2
    CreateDC(nil,        'Colson on shielddesktop (redirected 2)',       nil, nil) ==> 0
    CreateDC(nil,        'OneNote for Windows 10 (redirected 2)',        nil, nil) ==> 0
    CreateDC(nil,        'Microsoft XPS Document Writer (redirected 2)', nil, nil) ==> 0
    CreateDC(nil,        'Microsoft XPS Document Writer',                nil, nil) ==> 0x6B214609
    CreateDC(nil,        'Microsoft Print to PDF (redirected 2)',        nil, nil) ==> 0
    CreateDC(nil,        'Microsoft Print to PDF',                       nil, nil) ==> 0x76213A00
    CreateDC(nil,        'Fax (redirected 2)',                           nil, nil) ==> 0
    CreateDC(nil,        'Fax',                                          nil, nil) ==> 0xBF21704F
    CreateDC(nil,        'EPSON ET-2800 Series (redirected 2)',          nil, nil) ==> 0
    CreateDC(nil,        'Brother MFC-7840W Printer',                    nil, nil) ==> 0xF62139F2
    
  2. MSDN, and some other people have said pass WINSPOOL as the Driver Name:

    // Another MSDN page says use "WINSPOOL"
    // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162931%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
    CreateDC('WINSPOOL', 'Xerox Phaser 6500N',                           nil, nil) ==> 0x29214466
    CreateDC('WINSPOOL', 'Send To OneNote 2010',                         nil, nil) ==> 0
    CreateDC('WINSPOOL', 'OneNote for Windows 10',                       nil, nil) ==> 0x0C2140A2
    CreateDC('WINSPOOL', 'Colson on shielddesktop (redirected 2)',       nil, nil) ==> 0
    CreateDC('WINSPOOL', 'OneNote for Windows 10 (redirected 2)',        nil, nil) ==> 0
    CreateDC('WINSPOOL', 'Microsoft XPS Document Writer (redirected 2)', nil, nil) ==> 0
    CreateDC('WINSPOOL', 'Microsoft XPS Document Writer',                nil, nil) ==> 0xB52134E4
    CreateDC('WINSPOOL', 'Microsoft Print to PDF (redirected 2)',        nil, nil) ==> 0
    CreateDC('WINSPOOL', 'Microsoft Print to PDF',                       nil, nil) ==> 0x8B213BA3
    CreateDC('WINSPOOL', 'Fax (redirected 2)',                           nil, nil) ==> 0
    CreateDC('WINSPOOL', 'Fax',                                          nil, nil) ==> 0xD62137F0
    CreateDC('WINSPOOL', 'EPSON ET-2800 Series (redirected 2)',          nil, nil) ==> 0
    CreateDC('WINSPOOL', 'Brother MFC-7840W Printer',                    nil, nil) ==> 0x3D2132F3
    
  3. Try passing an empty string.

    // Try empty string as the driver :shrug:
    CreateDC('',         'Xerox Phaser 6500N',                           nil, nil) ==> 0x15213FFF
    CreateDC('',         'Send To OneNote 2010',                         nil, nil) ==> 0
    CreateDC('',         'OneNote for Windows 10',                       nil, nil) ==> 0x28212DA3
    CreateDC('',         'Colson on shielddesktop (redirected 2)',       nil, nil) ==> 0
    CreateDC('',         'OneNote for Windows 10 (redirected 2)',        nil, nil) ==> 0
    CreateDC('',         'Microsoft XPS Document Writer (redirected 2)', nil, nil) ==> 0
    CreateDC('',         'Microsoft XPS Document Writer',                nil, nil) ==> 0xAD212B04
    CreateDC('',         'Microsoft Print to PDF (redirected 2)',        nil, nil) ==> 0
    CreateDC('',         'Microsoft Print to PDF',                       nil, nil) ==> 0x17211B7E
    CreateDC('',         'Fax (redirected 2)',                           nil, nil) ==> 0
    CreateDC('',         'Fax',                                          nil, nil) ==> 0xE72129DE
    CreateDC('',         'EPSON ET-2800 Series (redirected 2)',          nil, nil) ==> 0
    CreateDC('',         'Brother MFC-7840W Printer',                    nil, nil) ==> 0x8B212758
    
  4. Some people have suggested duplicating the device name into the driver name:

    CreateDC('Xerox Phaser 6500N',                              'Xerox Phaser 6500N',                           nil, nil) ==> 0x67211FDA
    CreateDC('Send To OneNote 2010',                            'Send To OneNote 2010',                         nil, nil) ==> 0
    CreateDC('OneNote for Windows 10',                          'OneNote for Windows 10',                       nil, nil) ==> 0xD52160FA
    CreateDC('Colson on shielddesktop (redirected 2)',          'Colson on shielddesktop (redirected 2)',       nil, nil) ==> 0
    CreateDC('OneNote for Windows 10 (redirected 2)',           'OneNote for Windows 10 (redirected 2)',        nil, nil) ==> 0
    CreateDC('Microsoft XPS Document Writer (redirected 2)',    'Microsoft XPS Document Writer (redirected 2)', nil, nil) ==> 0
    CreateDC('Microsoft XPS Document Writer',                   'Microsoft XPS Document Writer',                nil, nil) ==> 0xBE211038
    CreateDC('Microsoft Print to PDF (redirected 2)',           'Microsoft Print to PDF (redirected 2)',        nil, nil) ==> 0
    CreateDC('Microsoft Print to PDF',                          'Microsoft Print to PDF',                       nil, nil) ==> 0xC0211291
    CreateDC('Fax (redirected 2)',                              'Fax (redirected 2)',                           nil, nil) ==> 0
    CreateDC('Fax',                                             'Fax',                                          nil, nil) ==> 0xBF2176BF
    CreateDC('EPSON ET-2800 Series (redirected 2)',             'EPSON ET-2800 Series (redirected 2)',          nil, nil) ==> 0
    CreateDC('Brother MFC-7840W Printer',                       'Brother MFC-7840W Printer',                    nil, nil) ==> 0
    

CreateIC also fails

CreateIC is just a "lightweight" form of CreateDC (you can use it to get information about a device, but you can't draw GDI with it). It also means that CreateIC also fails on Windows 10.

Bonus Reading

Note: I have repurposed this old unrelated question of mine to ask this new question here see here before you. If the question here gets an answer, i'll copy it to the question's permanent home, and revert this question back to the unrelated question it started as.

Printer Name Length

@TylerH had a good insight, possible issue with printer name length:

It's possible. If i sort the printer names by length, you can see it nearly works; except for one that makes that theory breakdown:

╔════════════════════════════════════════════════╦═════════════╗
║ Printer name (sorted by name length)           ║ Returned DC ║
╠════════════════════════════════════════════════╬═════════════╣
║ Fax                                ┊           ║ 0xBF21704F  ║
║ Fax (redirected 2)                 ┊           ║ 0           ║ 
║ Xerox Phaser 6500N                 ┊           ║ 0x85214A3E  ║
║ Send To OneNote 2010               ┊           ║ 0           ║ 
║ Microsoft Print to PDF             ┊           ║ 0x76213A00  ║
║ OneNote for Windows 10             ┊           ║ 0x162149B2  ║
║ Brother MFC-7840W Printer          ┊           ║ 0xF62139F2  ║
║ Microsoft XPS Document Writer      ┊           ║ 0x6B214609  ║
║ EPSON ET-2800 Series (redirected 2)┊           ║ 0           ║ 
║ Microsoft Print to PDF (redirected 2)          ║ 0           ║ 
║ OneNote for Windows 10 (redirected 2)          ║ 0           ║
║ Colson on shielddesktop (redirected 2)         ║ 0           ║
║ Microsoft XPS Document Writer (redirected 2)   ║ 0           ║ 
║ Microsoft XPS Document Writer (redirected 2)   ║ 0           ║ 
╚════════════════════════════════════════════════╩═════════════╝

I think this is correlation implying causation: remote printer names are longer because of the (redirected 2) suffix.

It also cannot be the cause, because in that linked question, the issue is with filling the DEVMODE structure; which we are not (and do not need) to do.

Thirdly, a redirected printer with a name shorter than 35 characters still fails.

And finally the linked post was about the user trying to retrieve the name of the printer, and finding that DEVMODE truncates the name to 35 characters:

Note that this name may be truncated to fit in the dmDeviceName array.

so the other questioner is not getting the entire name he wants. When he specifies the "full" printer name, it works.

So, no; not a related problem.

14
  • 1
    The ERROR_NOT_SUPPORTED may not be accurate, as the documentation (as you state) only reports that the function returns NULL on failure. However, it may well be that the XPS document writer does not support returning an information context on Win10.. Did you try setting your HP printer as the default (or setting it as the active printer with PrinterIndex) to see if that also fails?
    – Ken White
    Oct 4, 2015 at 3:28
  • FWIW this seems like a pure winapi question Oct 4, 2015 at 9:26
  • @DavidHeffernan I thought about that but a) i didn't want to scare off people with talk of VCL framework code (i.e. not my code) and b) i wanted to pre-empt anyone who might ask why are you doing that?, and i would have no idea how to answer them - since i wasn't the one doing it!
    – Ian Boyd
    Oct 4, 2015 at 12:57
  • 1
    I suspect though, that the winapi experts may steer clear of a Delphi question Oct 4, 2015 at 13:10
  • 1
    If you use EnumPrinters do you get the name you expect for the printer? (Use Level 2 to get the printer name and driver name)
    – theB
    Oct 4, 2015 at 13:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.