Win32 C++ PRINTER_INFO_2 attributes

Win32 C++ PRINTER_INFO_2 attributes

本文关键字:attributes INFO Win32 PRINTER C++      更新时间:2023-10-16

我试图访问PRINTER_INFO_2结构的友好名称属性。但是我不知道怎么用c++来写。

所以下面的代码以十六进制形式返回名称…

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwNeeded = 0, dwPrintersR = 0, Level = 2;
    PRINTER_INFO_2* prninfo=NULL;
    int retValue = 0;
    //Find required buffer size
    EnumPrinters( PRINTER_ENUM_NAME, NULL, Level, NULL, 0, &dwNeeded, &dwPrintersR );

    prninfo = (PRINTER_INFO_2*) GlobalAlloc(GPTR,dwNeeded);
    EnumPrinters( PRINTER_ENUM_NAME, NULL, Level, (LPBYTE) prninfo, dwNeeded, &dwNeeded, &dwPrintersR );
    cout << "# of printers:" << dwPrintersR << "n";
    for(int i = 0; i<dwPrintersR; i++){
        cout << "Printer Name: " << prninfo[i].pPrinterName << "n";
    }
    _getch();
    return 0;
}

我想使用PRINTER_ATTRIBUTE_FRIENDLY_NAME,但我不知道怎么做。

我是c++和编译语言的新手。非常感谢。

代码正常工作,除了它实际上试图将名称打印到控制台窗口。

看起来你正在编译Unicode,所以打印机名称是一个宽字符串(whcar_t *)。std::ostream::operator<<(wchar_t*)没有重载,所以你最终得到的只是指针的值,而不是字符串的值。

您需要将宽字符串转换为"ANSI"字符串,编译器为ANSI而不是Unicode,或者使用处理宽字符串的函数输出名称。例如,可以将cout行替换为:

MessageBox(NULL, prninfo[i].pPrinterName, TEXT("Printer Name"), MB_OK);

或者直接替换

cout & lt; & lt;# of printers:" <<dwPrintersR & lt; & lt;" n";

wcout & lt; & lt;L"打印机名称:"<<prninfo[我]。pPrinterName & lt; & lt;L" n";

测试。它工作。