C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

c++中使用com的方法

简单模拟:

#include  " ClassDll.h "
#include 
" ClassDll_i.c "


if  ( FAILED( CoInitialize(NULL) ))
{
return ;
}


IClass
*  pIClass;

hr 
=  CoCreateInstance(CLSID_Class,
NULL,
CLSCTX_INPROC_SERVER,
IID_IClass,
(
void ** & pIClass);

if  ( SUCCEEDED(hr) )
{
// hr = pIClass->Method();

if  ( SUCCEEDED(hr) )
{
}

else
{
}


pIClass
-> Release();
}


CoUninitialize();


具体实例:
LOCAL COM Server:

import  " oaidl.idl " ;

//  define IStats interface
[ object , uuid(FE78387F - D150 - 4089 - 832C - BBF02402C872),
 oleautomation, helpstring(
" Get the status information about this car " )]
interface  IStats : IUnknown
{
   HRESULT DisplayStats();
   HRESULT GetPetName([
out ,retval] BSTR *  petName);
}
;

//  define the IEngine interface
[ object , uuid(E27972D8 - 717F - 4516 - A82D - B688DC70170C),
 oleautomation, helpstring(
" Rev your car and slow it down " )]
interface  IEngine : IUnknown
{
   HRESULT SpeedUp();
   HRESULT GetMaxSpeed([
out ,retval]  int *  maxSpeed);
   HRESULT GetCurSpeed([
out ,retval]  int *  curSpeed);
}
;

//  define the ICreateMyCar interface
[ object , uuid(5DD52389 - B1A4 - 4fe7 - B131 - 0F8EF73DD175),
 oleautomation, helpstring(
" This lets you create a car object " )]
interface  ICreateMyCar : IUnknown
{
   HRESULT SetPetName([
in ]BSTR petName);
   HRESULT SetMaxSpeed([
in int  maxSp);
}
;

//  library statement
[uuid(957BF83F - EE5A - 42eb - 8CE5 - 6267011F0EF9), version( 1.0 ),
 helpstring(
" Car server with typeLib " )]
library CarLocalServerLib
{
   importlib(
" stdole32.tlb " );
   [uuid(1D66CBA8
- CCE2 - 4439 - 8596 - 82B47AA44E43)]
   coclass MyCar
   
{
      [
default interface  ICreateMyCar;
      
interface  IStats;
      
interface  IEngine;
   }
;
}
;


LOCAL COM Client:


#include "../CarLocalServer/CarLocalServerTypeInfo.h"    // use your own path here
#include "../CarLocalServer/CarLocalServerTypeInfo_i.c"  // use your own path here
#include "iostream.h"

// for showing possible mistakes
void ShowErrorMessage(LPCTSTR,HRESULT);

int main()
{
   
// initialize the COM runtime
   cout << "Initialize the COM runtime";
   CoInitialize(NULL);
   cout 
<< "success." << endl;

   
// declare variables
   HRESULT hr;
   IClassFactory
* pICF = NULL;
   ICreateMyCar
*  pICreateMyCar = NULL;
   IEngine
*       pIEngine = NULL;
   IStats
*        pIStats = NULL;

   cout 
<< endl << "Get the class factory interface for the Car class";
   hr 
= CoGetClassObject(CLSID_MyCar,CLSCTX_LOCAL_SERVER, 
                         NULL,IID_IClassFactory,(
void**)&pICF);
   
if ( FAILED(hr) )
   
{
      ShowErrorMessage(
"CoGetClassObject()",hr);
      exit(
1);
   }

   
else cout << "success." << endl;

   cout 
<< "Create the Car object and get back the ICreateMyCar interface";
   hr 
= pICF->CreateInstance(NULL,IID_ICreateMyCar,(void**)&pICreateMyCar);
   
if ( FAILED(hr) )
   
{
      ShowErrorMessage(
"CoCreateInstance()",hr);
      exit(
1);
   }

   
else cout << "success." << endl;

   
// set parameters on the car
   cout << endl << "Set different parameters on the car";
   pICreateMyCar
->SetMaxSpeed(30);
   BSTR carName 
= SysAllocString(OLESTR("COMCar?!"));
   pICreateMyCar
->SetPetName(carName);
   SysFreeString(carName);
   cout 
<< "success." << endl;

   cout 
<< endl << "Query the IStats interface";
   pICreateMyCar
->QueryInterface(IID_IStats,(void**)&pIStats);
   cout 
<< "success." << endl;

   cout 
<< endl << "Use the IStats interface to 
                    display the status of the car:" << endl;
   pIStats->DisplayStats();

   cout 
<< endl << "Query the IEngine interface";
   pICreateMyCar
->QueryInterface(IID_IEngine,(void**)&pIEngine);
   cout 
<< "success." << endl;

   cout 
<< endl << "Start to use the engine" << endl;
   
int curSp = 0;
   
int maxSp = 0;
   pIEngine
->GetMaxSpeed(&maxSp);
   
do
   
{
      pIEngine
->SpeedUp();
      pIEngine
->GetCurSpeed(&curSp);
      cout 
<< "current speed is: " << curSp << endl;
   }
 while (curSp <= maxSp);

   
if ( pICF )         pICF->Release();
   
if ( pICreateMyCar) pICreateMyCar->Release();
   
if ( pIStats )      pIStats->Release();
   
if ( pIEngine )     pIEngine->Release();

   cout 
<< endl << "Close the COM runtime";
   CoUninitialize();
   cout 
<< "success." << endl;
   
return 0;
}


void ShowErrorMessage(LPCTSTR header, HRESULT hr)
{
   
void* pMsg;

   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER 
| 
                 FORMAT_MESSAGE_FROM_SYSTEM,NULL,hr,
                 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), 
                 (LPTSTR)
&pMsg,0,NULL);

   cout 
<< header << ": Error(" << hex << hr << "): " 
        
<< (LPTSTR)pMsg << endl;

   LocalFree(pMsg);
}

参考: http://www.codeproject.com/com/LocalCOMServerClient.asp

posted on 2005-12-24 14:53 梦在天涯 阅读(3127) 评论(0)  编辑 收藏 引用 所属分类: CPlusPlusCOM/ATL


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1784891
  • 排名 - 5

最新评论

阅读排行榜