Reputation: 459
I have this program that is supposed to print a rectangle on the printer. It uses standard Win32 API calls.
HANDLE hdl;
DEVMODE* devmode;
OpenPrinter(L"HP Deskjet F4400 series", &hdl, NULL);
int size = DocumentProperties(NULL, hdl, L"HP Deskjet F4400 series", NULL, NULL, 0);
devmode = (DEVMODE*)malloc(size);
DocumentProperties(NULL, hdl, L"HP Deskjet F4400 series", devmode, NULL, DM_OUT_BUFFER);
HDC printerDC = CreateDC(L"WINSPOOL", devmode->dmDeviceName, NULL, devmode);
DOCINFO info;
memset(&info, 0, sizeof(info));
info.cbSize = sizeof(info);
StartDoc(printerDC, &info);
StartPage(printerDC);
Rectangle(printerDC, 100, 100, 200, 200);
EndPage(printerDC);
EndDoc(printerDC);
DeleteDC(printerDC);
All the API calls succeed, but no printing happens. What did I do wrong?
Upvotes: 5
Views: 6509
Reputation: 13932
There's a couple of issues here:
ClosePrinter
OpenPrinter
and CreateDC
are incorrect - they need to be an actual printer name, not a printer class. (When I tried your code, the APIs failed.)I tweaked the GDI print sample to print to the default printer, and it works; I modified your sample appropriately:
HANDLE hdl;
DEVMODE* devmode;
wchar_t szPrinter[MAX_PATH];
DWORD cchPrinter(ARRAYSIZE(szPrinter));
GetDefaultPrinter(szPrinter, &cchPrinter);
OpenPrinter(szPrinter, &hdl, NULL);
int size = DocumentProperties(NULL, hdl, szPrinter, NULL, NULL, 0);
devmode = (DEVMODE*)malloc(size);
DocumentProperties(NULL, hdl, szPrinter, devmode, NULL, DM_OUT_BUFFER);
HDC printerDC = CreateDC(L"WINSPOOL", szPrinter, NULL, devmode);
DOCINFO info;
memset(&info, 0, sizeof(info));
info.cbSize = sizeof(info);
StartDoc(printerDC, &info);
StartPage(printerDC);
Rectangle(printerDC, 100, 100, 200, 200);
EndPage(printerDC);
EndDoc(printerDC);
DeleteDC(printerDC);
ClosePrinter(hdl);
Upvotes: 3