2011-11-26 224 views
2

我有一個很大而難以理解的COM服務器問題。我正在嘗試將客戶端應用程序寫入CANoe(Vector的應用程序)。他們給了CANoe.tlb,CANoe.h和CANoe_i.cpp文件,但我只通過#import使用了CANoe.tlb。所有的例子都在Visual Basic中,我試圖用VC++(控制檯應用程序)編寫它。問題在於繼承。即在他們寫的幫助中,主要對象是應用程序,只有通過此對象才能訪問所有方法,對象事件等。 Visual Basic中的所有例子也很簡單,例如:如何訪問CANoe COM服務器接口中的子對象

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 

我確定我犯了錯誤,但我不知道在哪裏。簡而言之,我無法訪問子對象,他們的方法等。我只能訪問應用程序的方法。例如,我想以這種方式調用從測量對象開始的方法: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

附:使用COM服務器對我來說是新的,所以對於最終的愚蠢錯誤感到抱歉。我正在尋找任何有用的信息,但我沒有找到任何有關使用此COM接口的信息。

+0

我不相信沒有人可以幫助我:( – meler

+0

我不能編譯PAPP->測量 - 您的樣品> start()方法,因爲測量retrun它沒有啓動方法 – Victor

+0

你是對的IDisptach對象。我在第一篇文章中改變了代碼,現在它是可編譯的,所以也許你可以提出如何使用它的方法使用Measurement對象,我用OLE-COM Object Viewer打開了CANoe.tlb,但是這些信息對我來說並不容易理解。請參閱使用其他對象的解決方案?也許我以錯誤的方式使用pApp-> GetMeasurement(IDispatch **)。我附上有關使用CANoe COM Server的幫助文件:http://www.sendspace.com/file/7pqxyv – meler

回答

1

試圖改變你的代碼:

CComQIPtr<IMeasurement> measure; 
CComPtr<IDispatch> measureDisp; 

pApp->get_Measurement(&measureDisp); 
measure = measureDisp; 
measure->Start(); 

也不要忘記檢查的調用的方法結果。

+0

它完美的作品:)謝謝你很多維克多!我沒有考慮將CComQIPtr與CComPtr混合:) – meler

+0

CComQIPtr只是調用QueryIterface並將IDispatch轉換爲IMeasurement對象。在COM中,您不能使用static_cast進行強制轉換,而應該使用QueryInterface。 – Victor