我总结的几种简单的调用Com组件的方法

1.假如我们创建的Com组件的工程名叫TestCom,在其中添加了一个接口叫MyCom,接口名会自动命名为IMyCom,在其中添加两个方法,分别为:

HRESULT Add([in] long n1,[in] long n2,[out,retval]long * pVal);
HRESULT Cat([in] BSTR s1,[in] BSTR s2,[out,retval] BSTR * pVal);

 
编绎成功后,会生成TestCom.dll
注意此时我们生成的Com组件层次即为:TestCom.IMycom.方法;

2.下面总结出调用该Com的几种方法:

#include "stdafx.h"
#include <atlbase.h>
#import "G:\\workspace\\VC_WorkSpace\\TestCom\\Debug\\TestCom.dll" no_namespace

 

/*
注意:在导入了com的dll后,会在工程的exe目录下生成两个文件:TestCom.tlh与TestCom.tli,一个为定义文件,一个为实现文件,打开TestCom.tlh定义文件,会发现有类似这样几句:

_COM_SMARTPTR_TYPEDEF(IMyCom, __uuidof(IMyCom)); //该宏定义了指向接口IMyCom的指针IMyComPtr,以后的程序中可以直接用IMyComPtr这个指针类型。

struct __declspec(uuid("92e83a37-399a-47b1-bf6c-0ab052650cfe"))
MyCom; //该句定义了CLSID为"{92e83a37-399a-47b1-bf6c-0ab052650cfe}",通过它我们可以通过CLSIDFromString获取clsid;

struct __declspec(uuid("103b4c6e-3d3e-4e38-ab50-b9348465fc13"))
IMyCom : IDispatch //该句定义了接口ID:IID为"{103b4c6e-3d3e-4e38-ab50-b9348465fc13}",通过它我们可以通过IIDFromString获取接口iid;

CoCreateInstance函数就用到了上面两个做为参数来创建Com对象.
*/


第一种方法:

int impl0()
{
CoInitialize(NULL);
CLSID clsid;
IID iid;
HRESULT hr = CLSIDFromString(OLESTR("{92e83a37-399a-47b1-bf6c-0ab052650cfe}"),&clsid);
if(NOERROR != hr){
MessageBox(NULL,"ErrCLSIDFromString","ErrCLSIDFromString",MB_OK);
return -1;
}

HRESULT hriid = IIDFromString(OLESTR("{103b4c6e-3d3e-4e38-ab50-b9348465fc13}"),&iid);
if(S_OK != hriid){
MessageBox(NULL,"ErrIIDFromString","ErrIIDFromString",MB_OK);
return -1;
}

{
CComPtr<imycom> pMyCom;
pMyCom.CoCreateInstance(clsid);
long ret;
ret = pMyCom->Add(1,2);
}
CoUninitialize();
return 0;
}

 

 

第二种方法:

int impl1()
{
CoInitialize(NULL);
CLSID clsid;
CLSIDFromProgID(OLESTR("TestCom.MyCom"),&clsid); //"TestCom.MyCom"即为Dll名+Com对象名
{
CComPtr<imycom> pMyCom;
pMyCom.CoCreateInstance(clsid);
long ret;
ret = pMyCom->Add(1,2);
}
CoUninitialize();
return 0;
}

 

 

第三种方法:

int impl2()
{
CoInitialize(NULL);
IMyComPtr Pt ;
if (!SUCCEEDED(Pt.CreateInstance(__uuidof(MyCom))))
{
DWORD err;
err = GetLastError();
char Msg[128]={0};
sprintf(Msg,"%d",err);
MessageBox(NULL,Msg,"Err",MB_OK);
return -1;
}
long ret = Pt->Add(1,2);
CoUninitialize();
return 0;
}

 

 

第四种方法:

int impl3()
{
CoInitialize(NULL);
IMyComPtr Pt(__uuidof(MyCom)) ;
long ret = Pt->Add(1,2);
CoUninitialize();
return 0;
}

 

 

第五种方法:

int impl4()
{
  CoInitialize(NULL);
  IMyCom * pMyCom=NULL;

  CLSID clsid;
  IID iid;
  HRESULT hr = CLSIDFromString(OLESTR("{92e83a37-399a-47b1-bf6c-0ab052650cfe}"),&clsid);
  if(NOERROR != hr){
  MessageBox    (NULL,"ErrCLSIDFromString","ErrCLSIDFromString",MB_OK);
return -1;
}

HRESULT hriid = IIDFromString(OLESTR("{103b4c6e-3d3e-4e38-ab50-b9348465fc13}"),&iid);
if(S_OK != hriid){
  MessageBox(NULL,"ErrIIDFromString","ErrIIDFromString",MB_OK);
  return -1;
}

if (!SUCCEEDED(::CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,iid,(void**)&pMyCom)))
{
  DWORD err;
  err = GetLastError();
  char Msg[128]={0};
  sprintf(Msg,"%d",err);
  MessageBox(NULL,Msg,"Err",MB_OK);
  return -1;
}

long ret;
ret = pMyCom->Add(1,2);

CoUninitialize();
return 0;
}

 

 

 

int main(int argc, char* argv[])
{
return impl0();
}

 

你可能感兴趣的:(vc++)