Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » How to Retrieve Printer List from Windows System?
How to Retrieve Printer List from Windows System? [message #61856] Wed, 19 November 2025 11:46 Go to next message
fzx374cn is currently offline  fzx374cn
Messages: 6
Registered: August 2025
Promising Member
U++,How to Retrieve Printer List from Windows System?Sometimes, I need to specify a default printer.In Qt,I can do it easy.
The problem has been resolved [message #61861 is a reply to message #61856] Thu, 20 November 2025 05:38 Go to previous message
fzx374cn is currently offline  fzx374cn
Messages: 6
Registered: August 2025
Promising Member
The problem has been resolved:

#include <CtrlLib/CtrlLib.h>
#include <windows.h>
#include <winspool.h>

using namespace Upp;

// 动态加载winspool.drv方式
Vector<String> EnumeratePrinters() {
Vector<String> printers;

HMODULE hWinSpool = LoadLibrary("winspool.drv"); //Upp系统没有winspool.drv文件,在\Winddows\System32 处有
if (!hWinSpool) {
Cout() << "加载winspool.drv失败,错误代码:" << GetLastError() << "\n";
return printers;
}

// 获取函数指针
typedef BOOL (WINAPI *EnumPrintersFunc)(DWORD, LPTSTR, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD);
EnumPrintersFunc pEnumPrinters = (EnumPrintersFunc)GetProcAddress(hWinSpool, "EnumPrintersA");

if (!pEnumPrinters) {
Cout() << "获取EnumPrintersA函数指针失败\n";
FreeLibrary(hWinSpool);
return printers;
}

DWORD needed = 0;
DWORD returned = 0;

// 第一次调用获取所需缓冲区大小
if (!pEnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, NULL, 0, &needed, &returned)) {
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
Cout() << "第一次调用失败,错误代码:" << GetLastError() << "\n";
FreeLibrary(hWinSpool);
return printers;
}
}

// 分配缓冲区
Buffer<BYTE> buffer(needed);

// 第二次调用获取打印机信息
if (pEnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, buffer, needed, &needed, &returned)) {
PRINTER_INFO_4* printerInfo = (PRINTER_INFO_4*)~buffer; //注意:Ai示例中往往漏掉~字符

for (DWORD i = 0; i < returned; i++) {
printers.Add(printerInfo[i].pPrinterName);
}
}

FreeLibrary(hWinSpool);
return printers;
}

GUI_APP_MAIN {
Vector<String> printers = EnumeratePrinters();
DUMP(printers);

for (int i = 0; i < printers.GetCount(); i++) {
Cout() << "打印机 " << i << ": " << printers[i] << "\n";
}
}
Previous Topic: About Get Printer list from windows
Next Topic: Add compilable testcases for nontrivial problems!
Goto Forum:
  


Current Time: Thu Nov 20 06:50:45 CET 2025

Total time taken to generate the page: 0.53534 seconds