关于COM Server,我有一个大而难以理解的问题。我正在尝试编写客户端应用程序独木舟(应用程序的矢量)。他们给了独木舟。tlb, CANoe.h和canoe_i.p文件,但我只使用独木舟。TLB通过#import。所有的例子都在Visual Basic和我试图写它在vc++(控制台应用程序)。问题在于继承。例如,在他们编写的帮助中,主对象是Application,并且只有通过该对象才能访问所有方法、对象事件等。Visual Basic中的所有示例也都很简单,例如:
Dim gCanApp As CANalyzer.Application
Set gCanApp = New Application
gCanApp.Open ("C:Program FilesCANalyzerDemo_CLmotbus.cfg")
gCanApp.CAPL.Compile
gCanApp.Measurement.Start
我肯定我犯了错误,但我不知道在哪里。简单地说,我无法访问子对象及其方法等。我只能访问Application的方法。例如,我想以这种方式从测量对象调用方法Start: 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
}
我附加独木舟COM服务器文件(它是合法的从免费演示版本):http://www.sendspace.com/file/5pgcou
注:使用COM服务器对我来说是新的,所以很抱歉最终愚蠢的错误。我正在寻找任何有用的信息,但我没有找到任何关于使用这个COM接口。
尝试修改代码:
CComQIPtr<IMeasurement> measure;
CComPtr<IDispatch> measureDisp;
pApp->get_Measurement(&measureDisp);
measure = measureDisp;
measure->Start();
也不要忘记检查调用的方法的结果