调用pIBidiSpl2 :: SendRecvXMLString时出错

时间:2013-03-05 17:28:51

标签: c++ printing windows-8

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

我是第一个承认自己在C ++方面非常弱的人,但我知道如何阅读并试图找到IBiDiSpl2功能的例子或更好的解释并且走到了死胡同。

我尝试调试此错误时收到此错误

  

V4BiDiTest.exe中0x69D82C10(bidispl.dll)的未处理异常:   0xC0000005:访问冲突读取位置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;
}

1 个答案:

答案 0 :(得分: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) ;