C++调用IDL程序的做法(三)



作者:朱金灿

来源:http://blog.csdn.net/clever101

 

     C++调用IDL程序的做法(二)一文中介绍了如何动态创建IDLDrawWidgetControl的做法。假如我们需要在后台中调用IDL程序,又如何动态创建IDLDrawWidget Control呢?今天介绍在一个C++函数里调用IDL函数的做法。

 

   当然在调用IDL程序之前需要以管理员身份注册IDLDrawWidgetControl 3.0。在一个C++函数里调用IDL需要进行一下步骤:

  1. 初始化COM环境,确保能创建IDLDrawWidget Control

     

  2. 动态创建IDLDrawWidgetControl

     

  3. 设置设置IDL环境

     

  4. IDL函数传递参数值

     

  5. 加载IDLpro文件

     

  6. 调用IDL函数

     

  7. 关闭com环境

     

    下面是一个示例函数代码:

    /*
    pszInputFile  --- 输入的源图像文件
    pszOutFile    --- 输入的结果图像文件
    */
    int CallIDL( const TCHAR* pszInputFile,  const TCHAR* pszOutFile )
    {
    	
    	AfxEnableControlContainer();  //确保能创建ActiveX控件
    	CoInitialize(NULL); // 初始化com环境
    
            //动态创建ActiveX控件
    	CIDLDrawX3	IDLDrawX;
    	CWnd *pFrame =CWnd::GetDesktopWindow(); // 获取屏幕窗口作为IDL控件的父窗口
    
    	UINT nID = 101;
    	RECT rt;
    	rt.left = 0;
    	rt.right = 100;
    	rt.top = 0;
    	rt.bottom = 100;
    	if (!IDLDrawX.CreateControl(IDLDrawX.GetClsid(), NULL,
    		WS_CHILD,rt,pFrame, nID))
    	{
    		return -1;
    	}
    
    	char strDLLPath[256]={0};
    	::GetModuleFileName(NULL,strDLLPath,256);
    	std::string PathName = strDLLPath;
    	PathName = PathName.substr(0,PathName.rfind('\\'));
    	PathName = PathName.substr(0,PathName.rfind('\\'));
    
            // 设置IDL环境
    	std::string strIdlDllPath = PathName+ std::string("\\IDL64\\bin\\bin.x86\\idl.dll");
    	IDLDrawX.SetIdlPath(strIdlDllPath.c_str());
    	IDLDrawX.InitIDL(NULL);
    
    	//给IDL函数传递参数值
    	 std::string InFile= pszInputFile;
    	_bstr_t bstr(InFile.c_str());
    	VARIANT vFileName;
    	VariantInit(&vFileName);
    	V_VT(&vFileName) = VT_BSTR;
    	vFileName.bstrVal = bstr;
    	IDLDrawX.SetNamedData("InputPath",vFileName);
    
    	std::string OutFile= pszOutFile;
    	_bstr_t bstrOut(OutFile.c_str());
    	VARIANT vOutFileName;
    	VariantInit(&vOutFileName);
    	V_VT(&vOutFileName) = VT_BSTR;
    	vOutFileName.bstrVal = bstrOut;
    	IDLDrawX.SetNamedData("OutputPath",vOutFileName);
    
    	// 加载IDL pro文件
    	std::string ProFile= strDLLPath;
    	ProFile=ProFile.substr(0,ProFile.rfind('\\')+1);
    	ProFile=ProFile+std::string("class_doit_isodata.pro");
            IDLLibPath = PathName + "\\IDL64\\lib\\";
    	std::string strFullpath = std::string(".compile '")+IDLLibPath+std::string("class_doit_isodata.pro'");
    	long lRet = IDLDrawX.ExecuteStr(strFullpath.c_str());
    	// 执行Class_Doit_IsoData函数,Class_Doit_IsoData函数为class_doit_isodata.pro里定义的一个函数
    	IDLDrawX.ExecuteStr("retcode = Class_Doit_IsoData(InputPath,OutputPath)");
    
            // 获取执行Class_Doit_IsoData函数的返回值
    	VARIANT vRet = IDLDrawX.GetNamedData("retcode");
    
            CoUninitialize(); // 关闭com环境
    	return 0;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clever101

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值