WEB开发网
开发学院软件开发VC 基于ole的VC和matlab混合编程方法 阅读

基于ole的VC和matlab混合编程方法

 2007-03-17 21:25:13 来源:WEB开发网   
核心提示: 7. 打开VC++编译器,选择文件->新建->工程->MFC(exe)->命名(newoletest)-对话框->完成(曾经做个几个测试程序,没成功,名字可以自己写),基于ole的VC和matlab混合编程方法(3),删除确定和取消按钮,新建一个按钮,这是本文

7. 打开VC++编译器,选择文件->新建->工程->MFC(exe)->命名(newoletest)-对话框->完成(曾经做个几个测试程序,没成功,名字可以自己写)。删除确定和取消按钮,新建一个按钮,我直接使用了ok按钮,删除了取消,把ok按钮中原来的程序删掉了。

8. 打开类向导,在 Add Class选项内,选中From a typed library,进入d:MATLAB6p5workMyarraytestsrc文件夹,选中myarraytest_idl.tlb文件,点击打开,OK。这时一个COM类便加入进来了,查看一下为IMyarraytest类。

9. 下面就是OLE调用的基本方法了。首先在 CNewoletestApp类的InitInstance()里添加初始化OLE代码。

BOOL suc=AfxOleInit(); // 初始化OLE
if (suc==FALSE)
 {
  ::AfxMessageBox("初始化OLE失败!");
 }
  其次,在 CNewoletestDlg里包含plot_idl.h头文件;并从d:MATLAB6p5workMyarraytestsrcmyarraytest_idl_i.c中拷贝类的GUID并复制到PlotView.h文件类定义的上面。#include "myarraytest_idl.h"
const CLSID CLSID_Myarraytest = {0x9C4328EF,0xEA57,0x4D84,{0x9C,0x97,0xDE,0x4B,0xAE,0x02,0x21,0x5F}};
class CNewoletestDlg : public CDialog
{
// Construction
public:
IMyarraytest pResponse;
.....
}//double a[7][6];是我做其它测试程序用的。
然后,Onok()函数里添加获得COM指针的函数,代码如下:

plot.CreateDispatch(CLSID_plotclass,NULL); //创立接口
COleDispatchDriver(); //连接(此句可以不写)
pResponse.CreateDispatch(CLSID_Myarraytest,NULL); //创立接口
COleDispatchDriver(); //连接(此句可以不写)
VARIANT x;
VARIANT y;
 VariantInit(&x); //初始化
 VariantInit(&y);
 
 x.vt=VT_ARRAY|VT_R8; //类型(数组,双精度型)
 SAFEARRAYBOUND rgsabound[2];
 rgsabound[0].cElements=2; //数组所含元素数
 rgsabound[0].lLbound=0;  //数组上界
 rgsabound[1].cElements=2; //数组所含元素数
 rgsabound[1].lLbound=0;  //数组上界
//创立数组
 x.parray=SafeArrayCreate(VT_R8,2,rgsabound); //创立二维数组
 double b[2][2];
 b[0][0]=20;
 b[0][1]=24;
 b[1][0]=12;
 b[1][1]=15;
 //锁定数组
 SafeArrayLock(x.parray);
//数组传递数据
 x.parray->pvData=b;
 
//调用方法
 pResponse.arraytest(0,&y,x);
//解锁
 SafeArrayUnlock(x.parray);
/*
The most generic MATLAB M-function is function [Y1, Y2, ..., varargout] = foo(X1, X2, ..., varargin)
This function maps directly to the following IDL signature.
HRESULT foo([in] long nargout,
      [in,out] VARIANT* Y1,
      [in,out] VARIANT* Y2,
      .
      [in,out] VARIANT* varargout,
      [in] VARIANT X1,
      [in] VARIANT X2,
     ..
      [in] VARIANT varargin);
This IDL function definition is generated by producing a function with the same name as the original M-function and
an argument list containing all inputs and outputs of the original plus one additional parameter, nargout. (nargout
is not produced if you compile an M-function containing no outputs.) When present,
the nargout parameter is an [in] parameter of type long.It is always the first argument in the list.
This parameter allows correct passage of the MATLAB nargout parameter to the compiled M-code.
Following the nargout parameter, the outputs are listed in the order they appear
on the left side of the MATLAB function, and are tagged as [in,out], meaning that they are passed in both directions. The
function inputs are listed next, appearing in the same order as they do on the right side of the original function. All
inputs are tagged as [in] parameters. When present, the optional varargin/varargout parameters are always listed as the
last input parameters and the last output parameters. All parameters other than nargout are passed as COM VARIANT types.
Data Conversion Rules lists the rules for conversion between MATLAB arrays and COM VARIANTs.
*/
// ::AfxMessageBox("调用结束!");
pResponse.DetachDispatch();
pResponse.ReleaseDispatch();
10. 测试。

本文并没有给出函数调用的返回参数y的后处理,这是本文的一个缺点,我会继续努力,在数据返回这一方面多做些工作。

上一页  1 2 3 

Tags:基于 ole VC

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接