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;
    }

原文地址:https://www.cnblogs.com/lanzhi/p/6469750.html