1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > matlab生成的dll初始化失败 VC调用Matlab编译的DLL 老初始化失败

matlab生成的dll初始化失败 VC调用Matlab编译的DLL 老初始化失败

时间:2023-05-29 08:58:53

相关推荐

matlab生成的dll初始化失败 VC调用Matlab编译的DLL 老初始化失败

我用的是VC6与MatlabA

装饰Matlab代码编译成DLL文件老失败,搞了好久没找到原因,请教各位!

以下是网上的算例,照着做了,就是不能初始化DLL。

发表于: -10-18 17:35:31 我的开发环境是:Matlab 7.0 和 VC++6.0(mbuild环境已配置好)

编程步骤:

1、在Matlab中编写一个Draw.m文件,文件内容为:

%实现输入x1与x2,然后绘制sin曲线,然后生成两个随机数并输出

function [X1,Y1]=Draw(x1,x2)

x=x1:0.1:x2;

y=sin(x);

X1=rand(1);

Y1=rand(1);

plot(x,y,X1,Y1,'*');

2、利用命令 mcc -W cpplib:Draw -T link:lib Draw.m 生成DLL;

3、新建一个VC++工程(基于对话框的)

4、拷贝draw.h、Draw.dll、Draw.lib及Draw.ctf到VC++工程目录中,并将Draw.h 加入到当前工程中;

5、设置环境:Project->Setting->Link->Category :Input->Object/library modules:mclmcr.lib Draw.lib;

6、在对话框的头文件中加入 #include "draw.h" 与 #include "mclmcr.h";

7、在对话框的OnInitDialog()事件中进行 MATLAB 库文件的初始化,在对话框的OnDestroy()事件中进行 MATLAB 库文件资源的释放,代码如下:

BOOL CMatlabDrawInputXYDlg::OnInitDialog()

{

CDialog::OnInitDialog();

……………

// TODO: Add extra initialization here

/* Call the mclInitializeApplication routine. Make sure that the application

* was initialized properly by checking the return status. This initialization

* has to be done before calling any MATLAB API's or MATLAB Compiler generated

* shared library functions.*/

if( !mclInitializeApplication(NULL,0) )

{

AfxMessageBox( "Could not initialize the application.");

exit(1);

}

/* Call the library intialization routine and make sure that the

* library was initialized properly. */

if (!DrawInitialize())

{

AfxMessageBox("Could not initialize the library.");

exit(1);

}

return TRUE;// return TRUEunless you set the focus to a control

}

void CMatlabDrawInputXYDlg::OnDestroy()

{

CDialog::OnDestroy();

/* Call the library termination routine */

DrawTerminate();

mclTerminateApplication();

}

8、在窗体界面上放置两个编辑框(用于输入起始值和终止值,并设置变量m_X1和m_X2,均为double类型)和一个按钮;

9、编辑按钮单击事件代码,如下:

void CMatlabDrawInputXYDlg::OnBUTTONDraw()

{

// TODO: Add your control notification handler code here

double Data_X1[1],Data_X2[1];

double Data_Out1[1],Data_Out2[1];

CString strPromt;

mwArray m_InX1(1,1,mxDOUBLE_CLASS);

mwArray m_InX2(1,1,mxDOUBLE_CLASS);

mwArray m_OutX1(1,1,mxDOUBLE_CLASS);

mwArray m_OutX2(1,1,mxDOUBLE_CLASS);

UpdateData(TRUE);

Data_X1[0]=m_X1;

Data_X2[0]=m_X2;

m_InX1.SetData(Data_X1,1);

m_InX2.SetData(Data_X2,1);

draw(2,m_OutX1,m_OutX2,m_InX1,m_InX2);

//extern void mlxDraw(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

//extern void draw(int nargout, mwArray& X1, mwArray& Y1, const mwArray& x1, const mwArray& x2);

m_OutX1.GetData(Data_Out1,1);

m_OutX2.GetData(Data_Out2,1);

strPromt.Format("X1= %.6f",Data_Out1[0]);

AfxMessageBox(strPromt);

strPromt.Format("Y1= %.6f",Data_Out2[0]);

AfxMessageBox(strPromt);

}

10、编译通过,运行正常

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。