EDN Admin
Well-known member
Hello all,<br/>
<br/>
Im totally new in COM programming.<br/>
<br/>
I have big problem. Iâm trying to handle events from CANoe application via COM Server. Firstly I tried do it in native C++ but without results. Now Iâm trying it by using ATL. Im doing something wrong but I donât have any idea what.
When the event occurs, my client application suspends itself and CANoe. After closing client application CANoe works fine.
So I know that my client application handles Events from CANoe but it canât serve it. The commented parts of code in my source code were also used but with the same results.<br/>
<br/>
<div style="color:black; background-color:white
<pre>#import <span style="color:#a31515 "CANoe.tlb" <span style="color:green //importing CANoe type library
#include <span style="color:#a31515 "stdafx.h"
#include <atlbase.h> <span style="color:green //COM Server methods
#include <iostream>
#include <atlhost.h>
<span style="color:blue using <span style="color:blue namespace CANoe;
<span style="color:blue using <span style="color:blue namespace std;
_ATL_FUNC_INFO infoZero = { CC_STDCALL, VT_EMPTY, 0, 0};
_ATL_FUNC_INFO infoOne = { CC_STDCALL, VT_EMPTY, 1, { VT_I4 } };
<span style="color:blue class CANoeComClient :
<span style="color:green //IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(_IMeasurementEvents)>,
<span style="color:green //IDispEventSimpleImpl<2, CANoeComClient, &__uuidof(_IEnvironmentVariableEvents)>
IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(_IApplicationEvents)>
{
IApplicationPtr pApp; <span style="color:green //Pointer to the Application Object
IMeasurementPtr pMeasure; <span style="color:green //Pointer to the Measurement object
_IMeasurementEventsPtr pMEvent; <span style="color:green //Pointer to the Measurements Events
IEnvironmentPtr pEnvironment; <span style="color:green //Pointer to the Environment Object
IEnvironmentVariable2Ptr pEnvironmentVar; <span style="color:green //Pointer to the Environment Variable Object
ICAPL2Ptr pCAPL; <span style="color:green //Pointer to the CAPL Object
CLSID clsid; <span style="color:green //globally unique identifier that identifies a COM class object
HRESULT result; <span style="color:green //results of COM functions
<span style="color:blue public:
<span style="color:green //typedef IDispEventSimpleImpl<2, CANoeComClient, &__uuidof(CANoe::_IEnvironmentVariableEvents)> EnvVarEventsHandler;
<span style="color:green //typedef IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(CANoe::_IMeasurementEvents)> MeasurementEventsHandler;
<span style="color:blue typedef IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(CANoe::_IApplicationEvents)> ApplicationEventsHandler;
<span style="color:blue void __stdcall OnStart(<span style="color:blue void);
<span style="color:blue void __stdcall OnStop(<span style="color:blue void);
<span style="color:blue void __stdcall OnOpen(<span style="color:blue void);
<span style="color:blue void __stdcall OnQuit(<span style="color:blue void);
BEGIN_SINK_MAP(CANoeComClient)
<span style="color:green //SINK_ENTRY_INFO(1, __uuidof(CANoe::_IMeasurementEvents), 0x02, OnStart, &info)
<span style="color:green //SINK_ENTRY_INFO(1, __uuidof(CANoe::_IMeasurementEvents), 0x03, OnStop, &infoZero)
SINK_ENTRY_INFO(1, __uuidof(CANoe::_IApplicationEvents), 0x01, OnOpen, &infoZero)
SINK_ENTRY_INFO(1, __uuidof(CANoe::_IApplicationEvents), 0x02, OnQuit, &infoZero)
END_SINK_MAP()
<span style="color:blue void __stdcall OnChange();
<span style="color:green /*
BEGIN_SINK_MAP(CANoeComClient)
SINK_ENTRY_INFO(2, __uuidof(CANoe::_IEnvironmentVariableEvents), 0x01, OnChange, &info)
END_SINK_MAP()*/
CANoeComClient(_bstr_t configPath);
HRESULT startMeasurement();
HRESULT stopMeasurement();
};
<span style="color:blue void CANoeComClient::OnStart()
{
cout << <span style="color:#a31515 "kurka wodna 1" << endl;
}
<span style="color:blue void CANoeComClient::OnStop()
{
cout << <span style="color:#a31515 "kurka wodna 2" << endl;
}
<span style="color:blue void CANoeComClient::OnChange()
{
cout << <span style="color:#a31515 "kurka wodna 3" << endl;
}
<span style="color:blue void CANoeComClient::OnOpen()
{
cout << <span style="color:#a31515 "kurka wodna 4" << endl;
}
<span style="color:blue void CANoeComClient::OnQuit()
{
cout << <span style="color:#a31515 "kurka wodna 5" << endl;
}
CANoeComClient::CANoeComClient(_bstr_t configPath)
{
<span style="color:green /* Initialization COM library: */
<span style="color:blue if (FAILED(CoInitialize(NULL)))
{
cerr << <span style="color:#a31515 "Initialization COM Library error" << endl;
system(<span style="color:#a31515 "pause");
<span style="color:blue return;
}
<span style="color:green /* Actualization clsid variable with CANoe.Application path: */
<span style="color:blue if((result = CLSIDFromProgID(L<span style="color:#a31515 "CANoe.Application", &clsid)) != S_OK)
{
cerr << <span style="color:#a31515 "Problem with opening application" << endl;
system(<span style="color:#a31515 "pause");
<span style="color:blue return;
}
<span style="color:green /*Opening CANoe application: */
result = pApp.CreateInstance(__uuidof(CANoe::Application));
<span style="color:blue if(result != S_OK)
{
cerr << <span style="color:#a31515 "pApp fault" << endl;
<span style="color:blue return;
}
<span style="color:blue else
cout << <span style="color:#a31515 "CANoe opened succesfully" << endl;
<span style="color:green /* Opening CANoe configuration: */
result = pApp->Open(configPath, FALSE, TRUE); <span style="color:green //Opening test.cfg file
<span style="color:blue if(result != S_OK)
{
cerr << <span style="color:#a31515 "Opening configuration fault" << endl;
<span style="color:blue return;
}
<span style="color:blue else
cout << <span style="color:#a31515 "Configuration loaded succesfully" << endl;
<span style="color:green /*Definitions of all objects: */
<span style="color:green //pMeasure.CreateInstance(__uuidof(CANoe::Measurement));
pEnvironment = pApp->Environment;
pEnvironmentVar = pEnvironment->GetVariable(L<span style="color:#a31515 "env_ClientReq");
pCAPL = pApp->CAPL;
result = ApplicationEventsHandler:ispEventAdvise(pApp);
<span style="color:green // result = MeasurementEventsHandler:ispEventAdvise(pMeasure);
<span style="color:green //result = EnvVarEventsHandler:ispEventAdvise(pEnvironmentVar);
<span style="color:blue if(result != S_OK)
{
cerr << <span style="color:#a31515 "Creating connection fault" << endl;
<span style="color:blue return;
}
<span style="color:blue else
cout << <span style="color:#a31515 "Creating conenction succesfully" << endl;
}
HRESULT CANoeComClient::startMeasurement()
{
<span style="color:blue return pMeasure->Start();
}
HRESULT CANoeComClient::stopMeasurement()
{
<span style="color:blue return pMeasure->Stop();
}
<span style="color:blue int _tmain(<span style="color:blue int argc, _TCHAR* argv[])
{
<span style="color:blue int tmp = 0; <span style="color:green //temporary variable to used to get envVar values
HRESULT result; <span style="color:green //results of COM functions
CANoeComClient client(L<span style="color:#a31515 "C:\test\test.cfg");
<span style="color:blue while(1);
<span style="color:blue return 0;
}
[/code]
<br/>
<br/>
<br/>
<br/>
Here is description of Measurement Object and Event from OLE-COM object viewer:<br/>
<div style="color:black; background-color:white
<pre> [
<span style="color:blue uuid(CD866FB6-44BF-11D3-8538-00105A3E017B),
helpstring(<span style="color:#a31515 "Measurement Class")
]
coclass Measurement {
[<span style="color:blue default] <span style="color:blue interface IMeasurement2;
[<span style="color:blue default, source] dispinterface _IMeasurementEvents;
};
[
odl,
<span style="color:blue uuid(A844C1E0-F5CE-11D3-8612-00105A3E017B),
helpstring(<span style="color:#a31515 "IMeasurement2 Interface"),
dual,
oleautomation
]
<span style="color:blue interface IMeasurement2 : IMeasurement {
[id(0x0000000a), propget, helpstring(<span style="color:#a31515 "property Running")]
HRESULT Running([out, retval] VARIANT_BOOL* pVal);
};
[
<span style="color:blue uuid(A844C1E0-F5CE-11D3-8612-00105A3E017B),
helpstring(<span style="color:#a31515 "IMeasurement2 Interface"),
dual
]
dispinterface IMeasurement2 {
properties:
methods:
[id(0x00000001), propget, helpstring(<span style="color:#a31515 "property Application")]
IDispatch* Application();
[id(0x00000002), propget, helpstring(<span style="color:#a31515 "property Parent")]
IDispatch* Parent();
[id(0x00000003), helpstring(<span style="color:#a31515 "method Start")]
<span style="color:blue void Start();
[id(0x00000004), helpstring(<span style="color:#a31515 "method Stop")]
<span style="color:blue void Stop();
[id(0x00000005), helpstring(<span style="color:#a31515 "method Step")]
<span style="color:blue void Step();
[id(0x00000006), helpstring(<span style="color:#a31515 "method Animate")]
<span style="color:blue void Animate();
[id(0x00000007), helpstring(<span style="color:#a31515 "method Break")]
<span style="color:blue void Break();
[id(0x00000008), helpstring(<span style="color:#a31515 "method Reset")]
<span style="color:blue void Reset();
[id(0x00000009), propget, helpstring(<span style="color:#a31515 "property AnimationDelay")]
<span style="color:blue long AnimationDelay();
[id(0x00000009), propput, helpstring(<span style="color:#a31515 "property AnimationDelay")]
<span style="color:blue void AnimationDelay([<span style="color:blue in] <span style="color:blue long rhs);
[id(0x0000000a), propget, helpstring(<span style="color:#a31515 "property Running")]
VARIANT_BOOL Running();
};
[
<span style="color:blue uuid(CD866FB7-44BF-11D3-8538-00105A3E017B),
helpstring(<span style="color:#a31515 "_IMeasurementEvents Interface")
]
dispinterface _IMeasurementEvents {
properties:
methods:
[id(0x00000001), helpstring(<span style="color:#a31515 "method OnInit")]
HRESULT OnInit();
[id(0x00000002), helpstring(<span style="color:#a31515 "method OnStart")]
HRESULT OnStart();
[id(0x00000003), helpstring(<span style="color:#a31515 "method OnStop")]
HRESULT OnStop();
[id(0x00000004), helpstring(<span style="color:#a31515 "method OnExit")]
HRESULT OnExit();
};
[/code]
<br/>
I attach CANoe.tlb: http://www.sendspace.com/file/j2zloj<br/>
<br/>
Thank you for your attention<br/>
Damian<br/>
View the full article
<br/>
Im totally new in COM programming.<br/>
<br/>
I have big problem. Iâm trying to handle events from CANoe application via COM Server. Firstly I tried do it in native C++ but without results. Now Iâm trying it by using ATL. Im doing something wrong but I donât have any idea what.
When the event occurs, my client application suspends itself and CANoe. After closing client application CANoe works fine.
So I know that my client application handles Events from CANoe but it canât serve it. The commented parts of code in my source code were also used but with the same results.<br/>
<br/>
<div style="color:black; background-color:white
<pre>#import <span style="color:#a31515 "CANoe.tlb" <span style="color:green //importing CANoe type library
#include <span style="color:#a31515 "stdafx.h"
#include <atlbase.h> <span style="color:green //COM Server methods
#include <iostream>
#include <atlhost.h>
<span style="color:blue using <span style="color:blue namespace CANoe;
<span style="color:blue using <span style="color:blue namespace std;
_ATL_FUNC_INFO infoZero = { CC_STDCALL, VT_EMPTY, 0, 0};
_ATL_FUNC_INFO infoOne = { CC_STDCALL, VT_EMPTY, 1, { VT_I4 } };
<span style="color:blue class CANoeComClient :
<span style="color:green //IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(_IMeasurementEvents)>,
<span style="color:green //IDispEventSimpleImpl<2, CANoeComClient, &__uuidof(_IEnvironmentVariableEvents)>
IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(_IApplicationEvents)>
{
IApplicationPtr pApp; <span style="color:green //Pointer to the Application Object
IMeasurementPtr pMeasure; <span style="color:green //Pointer to the Measurement object
_IMeasurementEventsPtr pMEvent; <span style="color:green //Pointer to the Measurements Events
IEnvironmentPtr pEnvironment; <span style="color:green //Pointer to the Environment Object
IEnvironmentVariable2Ptr pEnvironmentVar; <span style="color:green //Pointer to the Environment Variable Object
ICAPL2Ptr pCAPL; <span style="color:green //Pointer to the CAPL Object
CLSID clsid; <span style="color:green //globally unique identifier that identifies a COM class object
HRESULT result; <span style="color:green //results of COM functions
<span style="color:blue public:
<span style="color:green //typedef IDispEventSimpleImpl<2, CANoeComClient, &__uuidof(CANoe::_IEnvironmentVariableEvents)> EnvVarEventsHandler;
<span style="color:green //typedef IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(CANoe::_IMeasurementEvents)> MeasurementEventsHandler;
<span style="color:blue typedef IDispEventSimpleImpl<1, CANoeComClient, &__uuidof(CANoe::_IApplicationEvents)> ApplicationEventsHandler;
<span style="color:blue void __stdcall OnStart(<span style="color:blue void);
<span style="color:blue void __stdcall OnStop(<span style="color:blue void);
<span style="color:blue void __stdcall OnOpen(<span style="color:blue void);
<span style="color:blue void __stdcall OnQuit(<span style="color:blue void);
BEGIN_SINK_MAP(CANoeComClient)
<span style="color:green //SINK_ENTRY_INFO(1, __uuidof(CANoe::_IMeasurementEvents), 0x02, OnStart, &info)
<span style="color:green //SINK_ENTRY_INFO(1, __uuidof(CANoe::_IMeasurementEvents), 0x03, OnStop, &infoZero)
SINK_ENTRY_INFO(1, __uuidof(CANoe::_IApplicationEvents), 0x01, OnOpen, &infoZero)
SINK_ENTRY_INFO(1, __uuidof(CANoe::_IApplicationEvents), 0x02, OnQuit, &infoZero)
END_SINK_MAP()
<span style="color:blue void __stdcall OnChange();
<span style="color:green /*
BEGIN_SINK_MAP(CANoeComClient)
SINK_ENTRY_INFO(2, __uuidof(CANoe::_IEnvironmentVariableEvents), 0x01, OnChange, &info)
END_SINK_MAP()*/
CANoeComClient(_bstr_t configPath);
HRESULT startMeasurement();
HRESULT stopMeasurement();
};
<span style="color:blue void CANoeComClient::OnStart()
{
cout << <span style="color:#a31515 "kurka wodna 1" << endl;
}
<span style="color:blue void CANoeComClient::OnStop()
{
cout << <span style="color:#a31515 "kurka wodna 2" << endl;
}
<span style="color:blue void CANoeComClient::OnChange()
{
cout << <span style="color:#a31515 "kurka wodna 3" << endl;
}
<span style="color:blue void CANoeComClient::OnOpen()
{
cout << <span style="color:#a31515 "kurka wodna 4" << endl;
}
<span style="color:blue void CANoeComClient::OnQuit()
{
cout << <span style="color:#a31515 "kurka wodna 5" << endl;
}
CANoeComClient::CANoeComClient(_bstr_t configPath)
{
<span style="color:green /* Initialization COM library: */
<span style="color:blue if (FAILED(CoInitialize(NULL)))
{
cerr << <span style="color:#a31515 "Initialization COM Library error" << endl;
system(<span style="color:#a31515 "pause");
<span style="color:blue return;
}
<span style="color:green /* Actualization clsid variable with CANoe.Application path: */
<span style="color:blue if((result = CLSIDFromProgID(L<span style="color:#a31515 "CANoe.Application", &clsid)) != S_OK)
{
cerr << <span style="color:#a31515 "Problem with opening application" << endl;
system(<span style="color:#a31515 "pause");
<span style="color:blue return;
}
<span style="color:green /*Opening CANoe application: */
result = pApp.CreateInstance(__uuidof(CANoe::Application));
<span style="color:blue if(result != S_OK)
{
cerr << <span style="color:#a31515 "pApp fault" << endl;
<span style="color:blue return;
}
<span style="color:blue else
cout << <span style="color:#a31515 "CANoe opened succesfully" << endl;
<span style="color:green /* Opening CANoe configuration: */
result = pApp->Open(configPath, FALSE, TRUE); <span style="color:green //Opening test.cfg file
<span style="color:blue if(result != S_OK)
{
cerr << <span style="color:#a31515 "Opening configuration fault" << endl;
<span style="color:blue return;
}
<span style="color:blue else
cout << <span style="color:#a31515 "Configuration loaded succesfully" << endl;
<span style="color:green /*Definitions of all objects: */
<span style="color:green //pMeasure.CreateInstance(__uuidof(CANoe::Measurement));
pEnvironment = pApp->Environment;
pEnvironmentVar = pEnvironment->GetVariable(L<span style="color:#a31515 "env_ClientReq");
pCAPL = pApp->CAPL;
result = ApplicationEventsHandler:ispEventAdvise(pApp);
<span style="color:green // result = MeasurementEventsHandler:ispEventAdvise(pMeasure);
<span style="color:green //result = EnvVarEventsHandler:ispEventAdvise(pEnvironmentVar);
<span style="color:blue if(result != S_OK)
{
cerr << <span style="color:#a31515 "Creating connection fault" << endl;
<span style="color:blue return;
}
<span style="color:blue else
cout << <span style="color:#a31515 "Creating conenction succesfully" << endl;
}
HRESULT CANoeComClient::startMeasurement()
{
<span style="color:blue return pMeasure->Start();
}
HRESULT CANoeComClient::stopMeasurement()
{
<span style="color:blue return pMeasure->Stop();
}
<span style="color:blue int _tmain(<span style="color:blue int argc, _TCHAR* argv[])
{
<span style="color:blue int tmp = 0; <span style="color:green //temporary variable to used to get envVar values
HRESULT result; <span style="color:green //results of COM functions
CANoeComClient client(L<span style="color:#a31515 "C:\test\test.cfg");
<span style="color:blue while(1);
<span style="color:blue return 0;
}
[/code]
<br/>
<br/>
<br/>
<br/>
Here is description of Measurement Object and Event from OLE-COM object viewer:<br/>
<div style="color:black; background-color:white
<pre> [
<span style="color:blue uuid(CD866FB6-44BF-11D3-8538-00105A3E017B),
helpstring(<span style="color:#a31515 "Measurement Class")
]
coclass Measurement {
[<span style="color:blue default] <span style="color:blue interface IMeasurement2;
[<span style="color:blue default, source] dispinterface _IMeasurementEvents;
};
[
odl,
<span style="color:blue uuid(A844C1E0-F5CE-11D3-8612-00105A3E017B),
helpstring(<span style="color:#a31515 "IMeasurement2 Interface"),
dual,
oleautomation
]
<span style="color:blue interface IMeasurement2 : IMeasurement {
[id(0x0000000a), propget, helpstring(<span style="color:#a31515 "property Running")]
HRESULT Running([out, retval] VARIANT_BOOL* pVal);
};
[
<span style="color:blue uuid(A844C1E0-F5CE-11D3-8612-00105A3E017B),
helpstring(<span style="color:#a31515 "IMeasurement2 Interface"),
dual
]
dispinterface IMeasurement2 {
properties:
methods:
[id(0x00000001), propget, helpstring(<span style="color:#a31515 "property Application")]
IDispatch* Application();
[id(0x00000002), propget, helpstring(<span style="color:#a31515 "property Parent")]
IDispatch* Parent();
[id(0x00000003), helpstring(<span style="color:#a31515 "method Start")]
<span style="color:blue void Start();
[id(0x00000004), helpstring(<span style="color:#a31515 "method Stop")]
<span style="color:blue void Stop();
[id(0x00000005), helpstring(<span style="color:#a31515 "method Step")]
<span style="color:blue void Step();
[id(0x00000006), helpstring(<span style="color:#a31515 "method Animate")]
<span style="color:blue void Animate();
[id(0x00000007), helpstring(<span style="color:#a31515 "method Break")]
<span style="color:blue void Break();
[id(0x00000008), helpstring(<span style="color:#a31515 "method Reset")]
<span style="color:blue void Reset();
[id(0x00000009), propget, helpstring(<span style="color:#a31515 "property AnimationDelay")]
<span style="color:blue long AnimationDelay();
[id(0x00000009), propput, helpstring(<span style="color:#a31515 "property AnimationDelay")]
<span style="color:blue void AnimationDelay([<span style="color:blue in] <span style="color:blue long rhs);
[id(0x0000000a), propget, helpstring(<span style="color:#a31515 "property Running")]
VARIANT_BOOL Running();
};
[
<span style="color:blue uuid(CD866FB7-44BF-11D3-8538-00105A3E017B),
helpstring(<span style="color:#a31515 "_IMeasurementEvents Interface")
]
dispinterface _IMeasurementEvents {
properties:
methods:
[id(0x00000001), helpstring(<span style="color:#a31515 "method OnInit")]
HRESULT OnInit();
[id(0x00000002), helpstring(<span style="color:#a31515 "method OnStart")]
HRESULT OnStart();
[id(0x00000003), helpstring(<span style="color:#a31515 "method OnStop")]
HRESULT OnStop();
[id(0x00000004), helpstring(<span style="color:#a31515 "method OnExit")]
HRESULT OnExit();
};
[/code]
<br/>
I attach CANoe.tlb: http://www.sendspace.com/file/j2zloj<br/>
<br/>
Thank you for your attention<br/>
Damian<br/>
View the full article