This blog is only to remind myself of what I've learned (from others or by myself) and only for knowledge sharing. External sources will be clearly stated in [Reference] of each article. I hope this blog won't infringe any copyrights and that it can be useful for interested blog readers.

2008年4月8日 星期二

COM client using COM server's interface

Goals:

(1) how to get instance of COM service

(2) how to get access of COM service APIs

(3) Usage


Sample code:

#include <iostream.h>

//!!NOTES: this part is to get CLSID and IID info

#import "..\Test_COM_service/Test_COM_service.tlb" named_guids raw_interfaces_only
using namespace TEST_COM_SERVICELib; // refer to Test_COM_service_i.c

#include <stdio.h>


void main(void)
{
// Declare and HRESULT and a pointer to
// the Simple_ATL interface
HRESULT hr;
IZTEDll *pZTEDll = NULL;

// Now we will intilize COM
hr = CoInitialize(0);

// Use the SUCCEEDED macro and see if
// we can get a pointer
// to the interface
if(SUCCEEDED(hr))
{
//NOTES: don't know why CLSCTX_INPROC_SERVER won't work

hr = CoCreateInstance(CLSID_ZTEDll, NULL, CLSCTX_ALL, IID_IZTEDll, (void**) &pZTEDll) ;

if(SUCCEEDED(hr))
{
UTC_time_info timeInfo;
timeInfo.year = 2008;
timeInfo.month = 4;
timeInfo.date = 8;
timeInfo.hour = 14;
timeInfo.minute = 0;
timeInfo.second = 0;

HRESULT res = S_OK;
res = pZTEDll->initializeKda(&timeInfo);
cout << "after initializeKda: " << res << endl;
cout << "year: " << timeInfo.year << endl;
cout << "month: " << timeInfo.month << endl;
cout << "date: " << timeInfo.date << endl;
cout << "hour: " << timeInfo.hour << endl;
cout << "minute: " << timeInfo.minute << endl;
cout << "second: " << timeInfo.second << endl;

res = pZTEDll->terminateKda();
cout << "after terminateKda: " << res << endl;

cout << "press any key to continue... " << endl;
getchar();

pZTEDll->Release();
}
else
{
cout << "CoCreateInstance Failed." << endl;
}
}
// Uninitialize COM
CoUninitialize();
}


Usage:

(1) open a "command console"
(2) Go to the directory where "Test_COM_client.exe" is located
(3) execuate Test_COM_service.exe
(4) check the result

沒有留言: