VC调用exe文件
来源:互联网 发布:dnf梦幻装备源码 编辑:程序博客网 时间:2024/05/13 13:15
第1步: 新建工程,选择Empty Project,Finish
第2步: 新建cpp文件,并输入代码
/* * 使用matlab生成的exe做加减运算(每次调用exe都会启动matlab引擎,比较花时间) */#include <iostream>using namespace std;void main(){ FILE *fp; int a,b,aaddb,aminusb; char status[20]; cout<<"输入加数与被加数:"<<endl; cin>>a>>b; if ((fp=fopen("MatlabIn.txt","wt+")) == NULL){ cout<<"打开文件MatlabIn.txt失败!"<<endl; return; } fprintf(fp,"a:%d\n",a); // hu 输入参数 fprintf(fp,"b:%d\n",b); fclose(fp); if (fp=fopen("MatlabOut.txt","rt")){ fclose(fp); remove("MatlabOut.txt"); } system("addandminus.exe"); // hu 启动exe并等待其执行完退出 if ((fp=fopen("MatlabOut.txt","rt")) == NULL){ cout<<"打开文件MatlabOut.txt失败!"<<endl; return; } fscanf(fp,"status:%s\n",status); if (strcmp(status,"成功")){ fclose(fp); cout<<status<<endl; return; } fscanf(fp,"a+b:%d\n",&aaddb); // hu 获得结果 fscanf(fp,"a-b:%d\n",&aminusb); fclose(fp); cout<<"aaddb:"<<aaddb<<"\naminusb:"<<aminusb<<endl; system("pause");}
第3步: 启动matlab2013,新建Function文件,输入代码,并生成exe(Windows Standalone Application)
% %% 对MatlabIn.txt中的参数进行处理并输出到MatlabOut.txt% %function addandminus()MatlabInfid = fopen('MatlabIn.txt','rt');if MatlabInfid<0 MatlabOutfid = fopen('MatlabOut.txt','wt+'); fprintf(MatlabOutfid,'%s\n','status:打开MatlabIn.txt失败'); fprintf(MatlabOutfid,'%s\n','a+b:'); fprintf(MatlabOutfid,'%s\n','a-b:'); fclose(MatlabOutfid); return;enda = fscanf(MatlabInfid,'a:%d\n');b = fscanf(MatlabInfid,'b:%d\n');fclose(MatlabInfid);MatlabOutfid = fopen('MatlabOut.txt','wt+');fprintf(MatlabOutfid,'%s\n','status:成功');fprintf(MatlabOutfid,'a+b:%d\n',a+b);fprintf(MatlabOutfid,'a-b:%d\n',a-b);fclose(MatlabOutfid);end
第4步: 将生成的exe文件复制到上面新建的vc工程目录下,运行vc程序成功!
改进: 上面的方法虽然能运行,但每次调用exe都要启动matlab引擎,比较花时间,可以将第2步和第3步代码加以改进
/* * 使用matlab生成的exe做加减运算(exe一直处于运行状态,直到该程序发出退出命令) */#include <iostream>#include <windows.h>#include <io.h>using namespace std;void main(){ FILE *fp; int a,b,aaddb,aminusb; char status[20]; WinExec("addandminus.exe",SW_HIDE); // hu 启动matlab后直接执行下一步程序 for (int ii=0;ii<5;ii++){ cout<<"输入加数与被加数:"<<endl; cin>>a>>b; if ((fp=fopen("MatlabIn.txt","wt+")) == NULL){ cout<<"打开文件MatlabIn.txt失败!"<<endl; return; } fprintf(fp,"a:%d\n",a); // hu 输入参数 fprintf(fp,"b:%d\n",b); fclose(fp); fclose(fopen("MatlabRun.tmp","a")); // hu 执行matlab while(!access("MatlabRun.tmp",0)); // hu 该文件不存在说明matlab执行完成 if ((fp=fopen("MatlabOut.txt","rt")) == NULL){ cout<<"打开文件MatlabOut.txt失败!"<<endl; return; } fscanf(fp,"status:%s\n",status); if (strcmp(status,"成功")){ fclose(fp); cout<<status<<endl; return; } fscanf(fp,"a+b:%d\n",&aaddb); fscanf(fp,"a-b:%d\n",&aminusb); fclose(fp); cout<<"aaddb:"<<aaddb<<"\naminusb:"<<aminusb<<endl; } fclose(fopen("MatlabStop.tmp","a")); // hu 退出matlab system("pause");}
% %% 每隔0.1s判断一次指令文件状态% MatlabRun.tmp 对MatlabIn.txt中的参数进行处理并输出到MatlabOut.txt% MatlabStop.tmp 退出程序% %function addandminus()while 1 pause(0.1); if exist('MatlabRun.tmp','file') % hu 若该文件存在,执行程序 MatlabInfid = fopen('MatlabIn.txt','rt'); if MatlabInfid<0 MatlabOutfid = fopen('MatlabOut.txt','wt+'); fprintf(MatlabOutfid,'%s\n','status:打开MatlabIn.txt失败'); fprintf(MatlabOutfid,'%s\n','a+b:'); fprintf(MatlabOutfid,'%s\n','a-b:'); fclose(MatlabOutfid); delete('MatlabRun.tmp'); while exist('MatlabRun.tmp','file') delete('MatlabRun.tmp'); end continue; end a = fscanf(MatlabInfid,'a:%d\n'); % hu 获得输入参数 b = fscanf(MatlabInfid,'b:%d\n'); fclose(MatlabInfid); MatlabOutfid = fopen('MatlabOut.txt','wt+'); fprintf(MatlabOutfid,'%s\n','status:成功'); fprintf(MatlabOutfid,'a+b:%d\n',a+b); % hu 输出结果 fprintf(MatlabOutfid,'a-b:%d\n',a-b); fclose(MatlabOutfid); delete('MatlabRun.tmp'); % hu 删除该文件表示程序运行完成 while exist('MatlabRun.tmp','file') delete('MatlabRun.tmp'); end elseif exist('MatlabStop.tmp','file') % hu 若该文件存在,退出程序 delete('MatlabStop.tmp'); break; endendend
阅读全文
0 0
- VC调用exe文件
- vc中调用exe文件
- 用VC++调用exe文件
- 关于vc++调用 exe文件的问题
- vc调用外部exe文件操作方法
- VC中调用exe
- VC中调用exe
- VC++调用外部exe
- VC中调用exe
- VC中调用exe
- vc调用EXE文件,并且等待其执行完毕.
- 在vc中怎么调用exe文件并传递参数
- vc中调用exe文件并传递参数
- 在VC中调用exe文件或者批处理文件方法总结
- vc中调用exe文件并传递参数
- VC++调用EXE文件的方法 (WinExc,ShellExcute,CreateProcess)
- 利用VC中函数调用一个.exe文件
- VC++调用EXE文件的方法 (WinExc,ShellExcute,CreateProcess)
- 层次遍历构造二叉树 java
- ==和equals()的关系
- 电脑是怎样执行编程语言的
- Android视频播放和横竖屏切换
- LinkedList的实现原理
- VC调用exe文件
- filegetname方法+filegetname(desc_bfile,directory,filename)+使用filegetname方法
- React--事件处理
- Spring Cloud简介以及版本选择
- ubantu16.04 配置samba
- PHP科普
- 解决unity导入模型时出现多层UV的情况
- 使用React的static方法实现同构以及同构的常见问题
- 《C和指针》6.18.1匹配字符