主要是使用EnumPrinters函數;
#include <windows.h>
#include <Winspool.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
/* TODO: Place code here.*/
DWORD dwNeeded, dwReturned ;
PRINTER_INFO_4 * pinfo4 ;
PRINTER_INFO_5 * pinfo5 ;
if (GetVersion () & 0x80000000) // Windows 98
{
EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, NULL,
0, &dwNeeded, &dwReturned) ;
pinfo5 = malloc (dwNeeded) ;
EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, (PBYTE) pinfo5,
dwNeeded, &dwNeeded, &dwReturned) ;
MessageBox(NULL,pinfo5->pPrinterName,"列印機枚舉",MB_OK);
free (pinfo5) ;
}
else // Windows NT
{
EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, NULL,
0, &dwNeeded, &dwReturned) ;
pinfo4 = malloc (dwNeeded) ;
EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, (PBYTE) pinfo4,
dwNeeded, &dwNeeded, &dwReturned) ;
MessageBox(NULL,pinfo4->pPrinterName,"列印機枚舉",MB_OK);
free (pinfo4) ;
}
return 0;
}
建構;在CFree建構,不能連接配接,出現下列錯誤;
undefined reference to `[email protected]'
在VC6建構,可以建構成功,但是運作程式,不會顯示任何内容;
可能此函數在目前作業系統已經失效;
EnumPrinters第一個參數:
PRINTER_ENUM_LOCAL 枚舉本地列印機
PRINTER_ENUM_NAME 枚舉由name參數指定的列印機。其中的名字可以是一個供應商、域或伺服器。
PRINTER_ENUM_SHARE 枚舉共享列印機(必須同其他常數組合使用)
PRINTER_ENUM_CONNECTIONS 枚舉網絡連接配接清單中的列印機
PRINTER_ENUM_NETWORK 枚舉通過網絡連接配接的列印機。
PRINTER_ENUM_REMOTE 枚舉通過網絡連接配接的列印機和列印伺服器。
dwNeeded, dwReturned:結構體需要的位元組數,傳回的位元組數;
函數的調用方式:先調用一次EnumPrinters,獲得需要的位元組數;然後用此位元組數配置設定記憶體給pinfo5;然後再調用EnumPrinters,把傳回結果放入pinfo5;
PRINTER_INFO_4 和 PRINTER_INFO_5 結構體,用于存放枚舉結果;兩個結構體用于不同的作業系統版本;其 pPrinterName 成員是列印機名字;
PRINTER_INFO_4 包含3個成員;
pPrinterName
Pointer to a null-terminated string that specifies the name of the printer (local or remote).
pServerName
Pointer to a null-terminated string that is the name of the server.
Attributes
Specifies information about the returned data.
PRINTER_INFO_5多了幾個成員;
typedef struct _PRINTER_INFO_5 {
LPTSTR pPrinterName;
LPTSTR pPortName;
DWORD Attributes;
DWORD DeviceNotSelectedTimeout;
DWORD TransmissionRetryTimeout;
} PRINTER_INFO_5, *PPRINTER_INFO_5;
相關結構體和函數的微軟文檔連結在此;
https://docs.microsoft.com/en-us/windows/win32/printdocs/printer-info-5