c++ - 调用 pIBidiSpl2::SendRecvXMLString 时出错

标签 c++ printing windows-8

我正在尝试使用 IBidispl2->SendRecvXML 函数,但我一直收到未处理的异常错误。

我是第一个承认我在 C++ 方面非常薄弱的​​人,但我知道如何阅读并试图找到 IBiDiSpl2 函数的示例或更好的解释,但我已经走到了死胡同。

当我尝试调试时出现此错误

Unhandled exception at 0x69D82C10 (bidispl.dll) in V4BiDiTest.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

这是我正在使用的代码:

#include "stdafx.h"
#include "BiDiSpl.h"
#include "comutil.h"

#include <iostream>
#include <vector>
#include <comdef.h>
#include <stdio.h>

using namespace std;

int main(int argc, char* argv[])
{
    // verify atleast 3 args ( prog.exe <printername> query1....)
    if(argc < 3)
    {
         cout << "ERROR: invalid usage, not enough arguments"<< endl << 
            "USAGE: V4BiDiTest.exe <printername> \"query1\" [\"query2\"] ... "     << endl <<
            "Please rerun the application";
         return 1;
    }

    // set the first arg after the exe to the printer name 
    string printer = argv[1];
    std::wstring stemp = std::wstring(printer.begin(), printer.end()); 
    LPCWSTR pPrinter = stemp.c_str();

    HRESULT hr;
    DWORD dwAccess;
    IBidiSpl2 *pIBidiSpl2 = NULL;
    dwAccess = BIDI_ACCESS_USER;

    // build the request schema with all other args after argv[1] 
    char* getSch = "<bidi:Get     xmlns:bidi=\"http://schemas.microsoft.com/windows/2005/03/printing/bidi\">";
    _bstr_t bstrt(getSch);

    for (int i = 2; i < argc; i++)
    {
        bstrt+="<Query schema=\'";
        char *argStr =argv[i];
        bstrt+=argStr;
        bstrt+="\'/>";
    } 
    bstrt+="</bidi:Get>";

    hr = CoInitializeEx (NULL, COINIT_MULTITHREADED) ;
    hr = CoCreateInstance(CLSID_BidiSpl,
            NULL, 
            CLSCTX_INPROC_SERVER,
            IID_IBidiSpl, 
            (void**)&pIBidiSpl2) ;

    if (pIBidiSpl2 == NULL)
    { 
        cerr << "CoCreateInstance failed" << endl; 
        return 1; 
    }


    hr = pIBidiSpl2->BindDevice(pPrinter,dwAccess);
    //Test hr here
    if (hr!=0){ cout << "failed on bind" <<endl; return 1;}

    BSTR responce;
    BSTR test1 = ::SysAllocString(L"<bidi:Get xmlns:bidi=\"http://schemas.microsoft.com/windows/2005/03/printing/bidi\"><Query schema='\\Printer'/></bidi:Get>");

    // I get the error when the following line executes
    hr = pIBidiSpl2->SendRecvXMLString(test1, &responce);
    //Test hr here
    if (hr!=0){cout << "failed on send" <<endl;return 1;}
    cout << responce << endl;
    ::SysFreeString(test1);
    ::SysFreeString(responce);

    hr = pIBidiSpl2->UnbindDevice();
    // test hr here
    if (hr!=0){cout << "failed on unbind" <<endl;return 1;}
    cout << "Successfully unbound device" << endl;

    return 0;
}

最佳答案

尝试改变

hr = CoCreateInstance(CLSID_BidiSpl,
        NULL, 
        CLSCTX_INPROC_SERVER,
        **IID_IBidiSpl,**
        (void**)&pIBidiSpl2) ;

hr = CoCreateInstance(CLSID_BidiSpl,
        NULL, 
        CLSCTX_INPROC_SERVER,
        **IID_IBidiSpl2,** 
        (void**)&pIBidiSpl2) ;

关于c++ - 调用 pIBidiSpl2::SendRecvXMLString 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15230303/

相关文章:

c++ - 不确定性 std::system_error: what(): 不允许操作

c++ - 访问基类型数组成员(Int-to-Type 习语)

php - Chrome 预览的预设打印设置

javascript - 在不使用 aria-haspop 的情况下模拟 Windows 表面上的触摸

c++ 11递归类模板到复杂错误

c# - 接收打印作业并将其转发到 LAN 中的打印机的软件

python - 将名称添加到 numpy 数组

windows-8 - 如何在 Windows cmd 终端中设置 cd 命令的默认目录?

windows-8 - 0x800a1391 - JavaScript 运行时错误 : 'WinJS' is undefined

c++ - OpenCV 错误 : Assertion failed -- matrix subtract