1

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

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

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

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;
}
4

1 回答 1

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) ;
于 2013-03-13T17:31:12.787 回答