【问题标题】:XcvData with GetConfigInfo fails带有 GetConfigInfo 的 XcvData 失败
【发布时间】:2017-10-30 03:28:44
【问题描述】:

我正在尝试使用 XcvData 读取打印机端口配置,但它失败并出现错误 87(无效参数)。添加端口或设置端口配置我没有问题。我已经看到了一些 c# 代码示例,它们完全符合我的要求,所以我不确定是什么导致了失败。任何建议将不胜感激。 谢谢。

示例代码:

PRINTER_DEFAULTS defaults = { NULL, NULL, SERVER_ACCESS_ADMINISTER };
HANDLE hPrinter;

if (::OpenPrinter(L", XcvMonitor Standard TCP/IP Port", hPrinter, &defaults))
{
    CONFIG_INFO_DATA_1 configInfoData1;
    memset(&configInfoData1, 0, sizeof(configInfoData1));
    configInfoData1.dwVersion = 1;

    PORT_DATA_1 portData1;
    // this initialization does not seem to help
    memset(&portData1, 0, sizeof(portData1));
    portData1.cbSize = sizeof(portData1);

    DWORD dwStatus = 0;
    DWORD dwNeeded = 0;

    // this always fails with dwStatus 87 (invalid parameter)
    if (XcvData(hPrinter, L"GetConfigInfo", (PBYTE)&configInfoData1, 
        sizeof(configInfoData1), (PBYTE)&portData1, sizeof(portData1), &dwNeeded, &dwStatus))
    {
        if (dwStatus != 0)
        {
            [...]
            // throw exception
        }

        _ASSERTE(dwNeeded > 0);

        [...]
    }


    // this works fine
    if (XcvData(hPrinter, L"ConfigPort", (PBYTE)&m_portData1, sizeof(m_portData1), 
        NULL, 0, &dwNeeded, &dwStatus))
    {
        [...]   
    }

    [...]
}

【问题讨论】:

  • portData1 未初始化。这很可能会脱离 API。请改用PORT_DATA_1 portData1{};
  • portData1 用于输出,所以我看不出初始化有什么帮助。但是,在发布问题之前,我确实尝试将它的 memset 设置为 0,甚至将其 cbSize 设置为正确的值。
  • 然后发布该代码,包括您的错误发现代码。许多 Windows API 调用要求输出参数仍然正确初始化(例如设置大小或版本字段)。
  • 我刚刚编辑了我的原始帖子。
  • 那不显示错误发现代码。这是许多开发人员弄错的一件事。据我们所知,错误代码在您要求时可能毫无意义。

标签: c++ winapi print-spooler-api


【解决方案1】:

` ////////////////////////////////////// /////////////////////p>

DWORD code = 0;
PRINTER_DEFAULTS Defaults1 = { NULL, NULL, SERVER_ACCESS_ADMINISTER };

HANDLE hXcv = NULL;
CString strPortName;
strPortName = _T(",XcvPort 168.118.18.156");//L",XcvMonitor Standard TCP/IP Port"
if (OpenPrinter((LPTSTR)(LPCTSTR)strPortName, &hXcv, &Defaults1))
{
    CONFIG_INFO_DATA_1 configInfoData1;
    memset(&configInfoData1, 0, sizeof(configInfoData1));
    configInfoData1.dwVersion = 1;

    PORT_DATA_1 portData1;
    // this initialization does not seem to help
    memset(&portData1, 0, sizeof(portData1));
    portData1.cbSize = sizeof(portData1);

    DWORD dwStatus = 0;
    DWORD dwNeeded = 0;

    // getconfiginfo
    if (XcvData(hXcv, L"GetConfigInfo", (PBYTE)&configInfoData1,
        sizeof(configInfoData1), (PBYTE)&portData1, sizeof(portData1), &dwNeeded, &dwStatus))
    {
    }

    _tcscpy(portData1.sztHostAddress,_T("168.118.18.148"));
    // setconfiginfo
    if (XcvData(hXcv, L"ConfigPort", (PBYTE)&portData1, sizeof(portData1),
        NULL, 0, &dwNeeded, &dwStatus))
    {
    }
    ClosePrinter(hXcv);
}
///////////////////////////

`OpenPrinter(L",XcvPort 168.118.18.156", hPrinter, &defaults);

使用特定的端口名然后 GetConfigInfo 就可以了。端口名:168.118.18.156

【讨论】:

  • 请在您的回答中添加更多解释
猜你喜欢
  • 2020-05-31
  • 2020-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2020-06-21
  • 2017-11-24
  • 1970-01-01
相关资源
最近更新 更多