簡單模擬:
#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();
具體實(shí)例:
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