我对COM Server有一个很难理解的问题。我正在尝试将客户端应用程序写入CANoe(Vector的应用程序)。他们给了CANoe.tlb,CANoe.h和CANoe_i.cpp文件,但我只使用CANoe.tlb通过#import。所有示例都在Visual Basic中,我试图在VC ++(控制台应用程序)中编写它。问题在于继承。即在他们写的帮助中,主要对象是Application,只有通过这个对象才能访问所有方法,对象事件等。 Visual Basic中的所有示例都很简单,即:
Dim gCanApp As CANalyzer.Application
Set gCanApp = New Application
gCanApp.Open ("C:\Program Files\CANalyzer\Demo_CL\motbus.cfg")
gCanApp.CAPL.Compile
gCanApp.Measurement.Start
我确定我犯了错误,但我不知道在哪里。简单地说,我无法访问子对象,他们的方法等。我只能访问应用程序的方法。例如,我想以这种方式调用方法Start from Measurement object:pApp-> Measurement-> Start()但是不可能。
我的源代码:
#import "CANoe.tlb" //importing CANoe type library
#include "stdafx.h"
#include <atlbase.h> //COM Server methods
#include <iostream>
using namespace CANoe;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
/*This part is working perfectly: */
CComPtr<IApplication> pApp = NULL;
CComPtr<IMeasurement> measure = NULL;
CComPtr<ICAPL> capl = NULL;
CLSID clsid;
IID iid;
HRESULT result;
/* Initialization COM library: */
if (FAILED(CoInitialize(NULL)))
{
cerr << "Initialization COM Library error" << endl;
system("pause");
return 1;
}
if((result = CLSIDFromProgID(L"CANoe.Application", &clsid)) != S_OK)
{
cerr << "Problem with opening application" << endl;
system("pause");
return 2;
}
result = pApp.CoCreateInstance(clsid); //Opening CANoe Application
if(result != S_OK)
cout << "pApp fault" << endl;
pApp->Open(L"C:\\test\\test.cfg", FALSE, TRUE); //Opening test.cfg file
/****************End of good part**********************/
//pApp->Measurement->Start();//I'd like to use it in this way - compiler error: error C2039: 'Start' : is not a member of 'IDispatch'
pApp->get_Measurement((IDispatch**)&measure);
measure->Start();//Unhandled exception at 0x7711d78c in canoe.exe: 0xC0000005: Access violation writing location 0x7711d78c.
CoUninitialize(); //Uninitialization COM Library
}
我附加了CANoe COM服务器文件(从免费演示版本开始是合法的):http://www.sendspace.com/file/5pgcou
P.S。使用COM Server对我来说是新的,对最终的愚蠢错误感到抱歉。我正在搜索任何有用的信息,但我没有找到任何关于使用此COM接口的信息。
答案 0 :(得分:2)
尝试更改代码:
CComQIPtr<IMeasurement> measure;
CComPtr<IDispatch> measureDisp;
pApp->get_Measurement(&measureDisp);
measure = measureDisp;
measure->Start();
另外不要忘记检查被调用方法的结果。