1
我正在尝试使用IBidispl2->SendRecvXML
函数,并且我不断收到未处理的异常错误。当我打电话给pIBidiSpl2 :: SendRecvXMLString时发生错误
我是第一个承认我在C++方面非常薄弱的人,但我知道如何阅读并试图找到对IBiDiSpl2函数的例子或更好的解释,并且已经走到死胡同。 0000005:访问冲突读取位置0xCCCCCCD0
当我尝试在0x69D82C10(bidispl.dll)在V4BiDiTest.exe调试这个
未处理的异常,我得到这个错误。
这里是我一起工作的代码:
#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;
}
你能标记出你得到错误的行。在使用它之前,你还没有检查'pIBidiSpl2'的有效性(是'!= NULL')。我怀疑这是获得这种例外的最可能原因。 – 2013-03-05 17:36:43
我在上面的代码中添加了错误,我还在一个快速if语句中添加了以确保pIBiDiSpl2不为null并仍然出现错误。感谢您的帮助,我希望:) – user2136748 2013-03-05 17:45:33
我想你的意思是你的快速检查失败后的stmt:'pIBidiSpl2-> SendRecvXMLString(test1,&responce);'。对不起,1日看起来一切都很好(根据'SendRecvXMLString'的参数说明)。看起来像'bidispl.dll'模块中发生的错误,可能是由于'Test1'变量的意外或无效输入引起的。您是否检查了所传递的XML与所提到的模式? – 2013-03-05 18:32:31