C++调用IDL导出的COM文件

DYQ老师在esri培训教材上有C#调用IDL导出dll组件的方法

在这里使用演示C++调用IDLEXBR_ASSISTANT导出的COM组件的方法

同样以helloworldex__define.pro为例看导出为dll并进行数据传递

 

;helloworldex__define.pro

FUNCTION helloworldex::HelloFrom, who
      IF (N_ELEMENTS(who) NE 0) THEN BEGIN
      
      MESSAGE = "Hello World from " + who
      tmp = dialog_message(MESSAGE,/infor)
      RETURN, message
   ENDIF ELSE BEGIN
      MESSAGE = 'Hello World'
      tmp = dialog_message(MESSAGE,/infor)
      RETURN, message
   ENDELSE
END

; -----------------------------------------------------------
; Init returns object reference on successful
; initialization.
FUNCTION helloworldex::INIT

   RETURN, 1

END

; -----------------------------------------------------------
; Object definition.
PRO helloworldex__define
  struct = {helloworldex, $
     who: '' , $
     message: ' ' $
  }
END

(1) IDL命令行输入IDLEXBR_ASSISTANT新建 COM工程

image

(2) 设置 HelloFrom的输出参数

image

生成com文件(dll文件和tlb文件)

(3) 发布后注册组件

regsvr32 “helloworldex.dll”

image
(4) 调用组件

#include "stdafx.h"
#include  <atlbase.h>
#import "helloworldex.dll" no_namespace

int _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize(NULL);
	{	
		CComPtr<Ihelloworldex> spFoo;
		spFoo.CoCreateInstance(__uuidof(helloworldex));
		CComVariant vtNULL(0);  
		HRESULT hr = spFoo->CreateObject(0,vtNULL,vtNULL);	
		_bstr_t returnstr;
		returnstr=spFoo->HELLOFROM(L"ESRIChina-BJ");//,returnstr);
		wprintf(L"%s", (WCHAR *)returnstr);
	}
	CoUninitialize();
	return 0;
}

输出Hello World from ESRIChina-BJ

posted on 2012-02-09 12:40  lartely  阅读(1828)  评论(0编辑  收藏  举报

导航