from: http://peng-jun.blog.163.com/blog/static/21562814200931585216511/
一、安装VC6.0
其他的就不多说了,就是下一步下一步的。我这里,只说下需要注意的地方。
在这里,我们选择“是”。
在这里,我们一定要注册环境变量,也就是将上面的“Register Environment Variables”选项,不然在VC6里面使用MATLAM COM builder Components的时候会出现:Error running MIDL.exe : 2 的错误。切记。
确定就OK。
最后就安装完成了。
安装完成之后,查看系统的环境变量就会发现多了include,lib,MSDevDir这三个环境变量。另外,path中也肯定有VC的路径了。如果没有的话,你可以自己建立。具体的可参看这篇文章:http://hi.baidu.com/pengjun/blog/item/97f9f7247a6d483b8744f9d4.html。如下图:
二、MATLAB的安装
我选择是MATLAB2008A版本,下面只选择几个安装界面:
这个序列号对应一个破解的激活文件,可以在我的博客下载到。
下载网址:http://pengtian85.googlepages.com/license_standalone.dat
这个激活文件,会在MATLAB安装结束的时候,需要提供。
三、编写M文件
打开MATLAB2008A,在命令窗口运行deploytool命令。会打开Deployment Tool,如下:
新建一个Deployment project。
一定要注意,project的保存路径不能含有空格。也就是上面的Location:后面的路径不能含有空格,否则在编译的时候会出现错误。
然后再MATLAB中新建一个M文件,写上简单的测试代码,如下:
然后,将该M文件,添加到我们的PlotSin工程中。
然后,点击Tools->Build。会出现下面的提示:
说明,我们还没有安装编译器。那么在MATLAB的命令窗口中,运行mbuild –setup命令就行了,整个过程如下:
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n? >> y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in C:/PROGRA~1/MATLAB/R2008a/sys/lcc
[2] Microsoft Visual C++ 6.0 in C:/Program Files/Microsoft Visual Studio
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C++ 6.0
Location: C:/Program Files/Microsoft Visual Studio
Are these correct [y]/n? y
Trying to update options file: C:/Documents and Settings/Tim/Application Data/MathWorks/MATLAB/R2008a/compopts.bat
From template: C:/PROGRA~1/MATLAB/R2008a/bin/win32/mbuildopts/msvc60compp.bat
Done . . .
之后再次点击Tools->Build来编译该COM组件。会出现如下的错误:
Build output( 2009-03-29 21:34:30).
mcc -F 'C:/COM/PlotSin/PlotSin.prj'
Compiler version: 4.8 (R2008a)
mcc -W 'com:PlotSin,PlotSinclass,1.0' -d 'C:/COM/PlotSin/PlotSin/src' -T 'link:lib' -v -C 'class{PlotSinclass:C:/COM/PlotSin/plotsin.m}'
Processing C:/Program Files/MATLAB/R2008a/toolbox/matlab/mcc.enc
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file C:/COM/PlotSin/PlotSin/src/mccExcludedFiles.log contains a list of functions excluded from the CTF archive.
1 item(s) added.
Generating MATLAB path for the compiled application...
Created 38 path items.
Begin validation of MEX files: Sun Mar 29 21:35:27 2009
Validating 'C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/deploywhich.mexw32'.
No conflicting M-file found.
Validating 'C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/readline.mexw32'.
No conflicting M-file found.
End validation of MEX files: Sun Mar 29 21:35:27 2009
Deleting 2 temporary MEX authorization files.
Removing: 'C:/DOCUME~1/Tim/LOCALS~1/Temp/mathworks_tmp_2756_3627_2756.auth'.
Removing: 'C:/DOCUME~1/Tim/LOCALS~1/Temp/mathworks_tmp_2756_28864_2756.auth'.
Parsing file "C:/COM/PlotSin/plotsin.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/deployprint.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/printdlg.m"
(Referenced from: "Compiler Command Line").
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin_idl.idl".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.hpp".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.cpp".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin_dll.cpp".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin.def".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin.rc".
Generating file "C:/COM/PlotSin/PlotSin/src/readme.txt".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin_mcc_component_data.c".
Executing command: mbuild -O -v -output "PlotSin_1_0" -I"C:/COM/PlotSin/PlotSin/src" "mwcomtypes.idl" "C:/COM/PlotSin/PlotSin/src/PlotSin_idl.idl" "mclcommain.lib" "C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.cpp" "C:/COM/PlotSin/PlotSin/src/PlotSin_dll.cpp" "C:/COM/PlotSin/PlotSin/src/PlotSin.def" "C:/COM/PlotSin/PlotSin/src/PlotSin.rc" "C:/COM/PlotSin/PlotSin/src/PlotSin_mcc_component_data.c" -link dll -outdir "C:/COM/PlotSin/PlotSin/src" -regsvr
'mbuild' 2?ê??ú2??òía2??üá?£?ò22?ê??é??DDμ?3ìDò
?ò?ú′|àí???t?£
Error: An error occurred while shelling out to mbuild (error code = 1).
Unable to build executable.
??? Error using ==> mcc
Error executing mcc, return status = 1.
There were errors during compilation process.
这是因为我们没有MATLAB的bin目录添加系统的环境变量path中,所以我们要将C:/Program Files/MATLAB/R2008a/bin/win32这个目录添加到系统的环境变量path中,再次编译。会有下面的成功提示:
Build output( 2009-03-29 21:45:02).
mcc -F 'C:/COM/PlotSin/PlotSin.prj'
Compiler version: 4.8 (R2008a)
mcc -W 'com:PlotSin,PlotSinclass,1.0' -d 'C:/COM/PlotSin/PlotSin/src' -T 'link:lib' -v -C 'class{PlotSinclass:C:/COM/PlotSin/plotsin.m}'
Processing C:/Program Files/MATLAB/R2008a/toolbox/matlab/mcc.enc
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file C:/COM/PlotSin/PlotSin/src/mccExcludedFiles.log contains a list of functions excluded from the CTF archive.
1 item(s) added.
Generating MATLAB path for the compiled application...
Created 38 path items.
Begin validation of MEX files: Sun Mar 29 21:46:01 2009
Validating 'C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/deploywhich.mexw32'.
No conflicting M-file found.
Validating 'C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/readline.mexw32'.
No conflicting M-file found.
End validation of MEX files: Sun Mar 29 21:46:01 2009
Deleting 2 temporary MEX authorization files.
Removing: 'C:/DOCUME~1/Tim/LOCALS~1/Temp/mathworks_tmp_3892_9976_3892.auth'.
Removing: 'C:/DOCUME~1/Tim/LOCALS~1/Temp/mathworks_tmp_3892_11626_3892.auth'.
Parsing file "C:/COM/PlotSin/plotsin.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/deployprint.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/printdlg.m"
(Referenced from: "Compiler Command Line").
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin_idl.idl".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.hpp".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.cpp".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin_dll.cpp".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin.def".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin.rc".
Generating file "C:/COM/PlotSin/PlotSin/src/readme.txt".
Generating file "C:/COM/PlotSin/PlotSin/src/PlotSin_mcc_component_data.c".
Executing command: mbuild -O -v -output "PlotSin_1_0" -I"C:/COM/PlotSin/PlotSin/src" "mwcomtypes.idl" "C:/COM/PlotSin/PlotSin/src/PlotSin_idl.idl" "mclcommain.lib" "C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.cpp" "C:/COM/PlotSin/PlotSin/src/PlotSin_dll.cpp" "C:/COM/PlotSin/PlotSin/src/PlotSin.def" "C:/COM/PlotSin/PlotSin/src/PlotSin.rc" "C:/COM/PlotSin/PlotSin/src/PlotSin_mcc_component_data.c" -link dll -outdir "C:/COM/PlotSin/PlotSin/src" -regsvr
This is mbuild Copyright 1984-2006 The MathWorks, Inc.
-> Default options filename found in C:/Documents and Settings/Tim/Application Data/MathWorks/MATLAB/R2008a
----------------------------------------------------------------
-> Options file = C:/Documents and Settings/Tim/Application Data/MathWorks/MATLAB/R2008a/compopts.bat
-> COMPILER = cl
-> Compiler flags:
COMPFLAGS = -c -Zp8 -G5 -GX -W3 -nologo -I"C:/PROGRA~1/MATLAB/R2008a/extern/include/win32" -DMSVC -DIBMPC
OPTIMFLAGS = -O2 -DNDEBUG
DEBUGFLAGS = -Z7
arguments = -IC:/COM/PlotSin/PlotSin/src
Name switch = /Fo
-> Pre-linking commands =
-> LINKER = link
-> Link directives:
LINKFLAGS = kernel32.lib user32.lib gdi32.lib advapi32.lib oleaut32.lib ole32.lib /LIBPATH:"C:/PROGRA~1/MATLAB/R2008a/extern/lib/win32/microsoft" /nologo mclmcrrt.lib /dll /implib:"C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.lib" /def:C:/COM/PlotSin/PlotSin/src/PlotSin.def
LINKFLAGSPOST =
Name directive = "/out:C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.dll"
File link directive =
Lib. link directive =
Rsp file indicator = @
-> Resource Compiler = rc /fo "C:/COM/PlotSin/PlotSin/src/PlotSin.res"
-> Resource Linker =
----------------------------------------------------------------
--> copy C:/PROGRA~1/MATLAB/R2008a/extern/include/win32/mwcomutil.tlb .
ò??′?? 1 ?????t?£
--> midl /nologo /win32 /I "C:/PROGRA~1/MATLAB/R2008a/extern/include" /D "NDEBUG" /out "C:/COM/PlotSin/PlotSin/src" "mwcomtypes.idl"
mwcomtypes.idl
oaidl.idl
objidl.idl
unknwn.idl
wtypes.idl
ocidl.idl
oleidl.idl
servprov.idl
urlmon.idl
msxml.idl
Processing C:/PROGRA~1/MATLAB/R2008a/extern/include/mwcomtypes.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/oaidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/objidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/unknwn.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/wtypes.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/ocidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/oleidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/servprov.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/urlmon.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/msxml.idl
--> midl /nologo /win32 /I "C:/PROGRA~1/MATLAB/R2008a/extern/include" /D "NDEBUG" /out "C:/COM/PlotSin/PlotSin/src" "C:/COM/PlotSin/PlotSin/src/PlotSin_idl.idl"
PlotSin_idl.idl
oaidl.idl
objidl.idl
unknwn.idl
wtypes.idl
ocidl.idl
oleidl.idl
servprov.idl
urlmon.idl
msxml.idl
mwcomtypes.idl
Processing C:/COM/PlotSin/PlotSin/src/PlotSin_idl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/oaidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/objidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/unknwn.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/wtypes.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/ocidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/oleidl.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/servprov.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/urlmon.idl
Processing C:/Program Files/Microsoft Visual Studio/VC98/INCLUDE/msxml.idl
Processing C:/PROGRA~1/MATLAB/R2008a/extern/include/mwcomtypes.idl
--> del mwcomutil.tlb
--> cl -IC:/COM/PlotSin/PlotSin/src -c -Zp8 -G5 -GX -W3 -nologo -I"C:/PROGRA~1/MATLAB/R2008a/extern/include/win32" -DMSVC -DIBMPC /FoC:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/PlotSinclass_com.obj -IC:/PROGRA~1/MATLAB/R2008a/extern/include -IC:/PROGRA~1/MATLAB/R2008a/simulink/include -O2 -DNDEBUG C:/COM/PlotSin/PlotSin/src/PlotSinclass_com.cpp
PlotSinclass_com.cpp
--> cl -IC:/COM/PlotSin/PlotSin/src -c -Zp8 -G5 -GX -W3 -nologo -I"C:/PROGRA~1/MATLAB/R2008a/extern/include/win32" -DMSVC -DIBMPC /FoC:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/PlotSin_dll.obj -IC:/PROGRA~1/MATLAB/R2008a/extern/include -IC:/PROGRA~1/MATLAB/R2008a/simulink/include -O2 -DNDEBUG C:/COM/PlotSin/PlotSin/src/PlotSin_dll.cpp
PlotSin_dll.cpp
--> cl -IC:/COM/PlotSin/PlotSin/src -c -Zp8 -G5 -GX -W3 -nologo -I"C:/PROGRA~1/MATLAB/R2008a/extern/include/win32" -DMSVC -DIBMPC /FoC:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/PlotSin_mcc_component_data.obj -IC:/PROGRA~1/MATLAB/R2008a/extern/include -IC:/PROGRA~1/MATLAB/R2008a/simulink/include -O2 -DNDEBUG C:/COM/PlotSin/PlotSin/src/PlotSin_mcc_component_data.c
PlotSin_mcc_component_data.c
--> rc /fo "C:/COM/PlotSin/PlotSin/src/PlotSin.res" C:/COM/PlotSin/PlotSin/src/PlotSin.rc
Contents of C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/mbuild_tmp.rsp:
mclcommain.lib C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/PlotSinclass_com.obj C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/PlotSin_dll.obj C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/PlotSin_mcc_component_data.obj C:/COM/PlotSin/PlotSin/src/PlotSin.res
--> link "/out:C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.dll" kernel32.lib user32.lib gdi32.lib advapi32.lib oleaut32.lib ole32.lib /LIBPATH:"C:/PROGRA~1/MATLAB/R2008a/extern/lib/win32/microsoft" /nologo mclmcrrt.lib /dll /implib:"C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.lib" /def:C:/COM/PlotSin/PlotSin/src/PlotSin.def @C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/mbuild_tmp.rsp
Creating library C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.lib and object C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.exp
--> if exist C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/templib.def del C:/DOCUME~1/Tim/LOCALS~1/Temp/mbuild_gPvSTO/templib.def
--> mwregsvr C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.dll
DllRegisterServer in C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.dll succeeded
Copying: C:/COM/PlotSin/PlotSin/src/PlotSin_1_0.dll -> C:/COM/PlotSin/PlotSin/distrib
Copying: C:/COM/PlotSin/PlotSin/src/PlotSin.ctf -> C:/COM/PlotSin/PlotSin/distrib
Copying: C:/COM/PlotSin/PlotSin/src/readme.txt -> C:/COM/PlotSin/PlotSin/distrib
Compilation completed succesfully. The output is located in C:/COM/PlotSin/PlotSin/distrib.
You can package the component by clicking on the "Package" icon in the Deployment Tool toolbar, or by clicking the Tools->Package menu when the Deployment Tool panel is selected. To include additional files in the package, click Project->Settings).
然后,点击Tools->Package来进行打包。但是,此时不打包,已经在本机的COM组件中,已经可以看到该COM组件了。之所以,打包是你可以在其他的电脑上安装这个COM组件。那么,我们就不打包了。
打包时,会有如下的输出:
Packaging output( 2009-03-29 22:11:08).
Files to package:
_install.bat
C:/Program Files/MATLAB/R2008a/toolbox/compiler/deploy/win32/MCRRegCOMComponent.exe
PlotSin_1_0.dll
PlotSin.ctf
readme.txt
PlotSin_pkg.exe created in the directory C:/COM/PlotSin/PlotSin/distrib
The size of the package is: 271.007 KB
然后到工程的distrib目录下,运行_install.bat,成功之后会出现如下对话框:
四、在VC6.0中调用MATLAB COM Builder Components下面的COM组件
在VC6.0中新建一个基于对话框的MFC应用程序,假设取工程名为VCPlotSin。并在工程中做如下的设置:
再选择Tools->OLE/COM Object Viewer,如下:
然后,在PlotSinclass上面单击右键,选择“View Type Information”,打开“ItypeLib Viewer”,如下:
点击ItypeLib Viewer中的保存,将其保存到Deployment Tools工程PlotSin所在的src目录,保存的时候要注意,保存的是.h和.c文件。然后将src目录下的PlotSin_1_0.h和PlotSin_1_0.c和mwcomtypes.h拷贝到VC6.0的工程VCPlotSin的目录下面。
然后将这三个文件,添加到VC6.0的工程VCPlotSin中。
选择Tools->options->directorys,在Show Directory for下选择include files.
将C:/Program Files/MATLAB/R2008a/extern/include/win32添加到下面的路径中。因为我们要用到这个目录下面的mwcomutil.h这个文件。
在VCPlotSinDlg.h中,添加如下程序:
#include "PlotSin_1_0.h"
#include "mwcomutil.h"
然后再类中声明一个该COM组件的指针:IPlotSinclass *pcom;
然后编译的时候会出现下面的错误:
c:/com/vcplotsin/plotsin_1_0.h(79) : error C2061: syntax error : identifier 'IMWFlags'
c:/com/vcplotsin/plotsin_1_0.h(82) : error C2061: syntax error : identifier 'IMWFlags'
c:/com/vcplotsin/plotsin_1_0.h(207) : error C2061: syntax error : identifier 'IMWFlags'
c:/com/vcplotsin/plotsin_1_0.h(219) : error C2061: syntax error : identifier 'IMWFlags'
那是因为我们的位置放错了,将两个include文件交换一下位置,如下:
#include "mwcomutil.h"
#include "PlotSin_1_0.h"
然后,再次编译就没有什么问题了。下面是VCPlotSinDlg.h中的前面的部分程序内容:
// VCPlotSinDlg.h : header file
//
#if !defined(AFX_VCPLOTSINDLG_H__4538699E_152D_42A0_8E05_9AF60105EE6C__INCLUDED_)
#define AFX_VCPLOTSINDLG_H__4538699E_152D_42A0_8E05_9AF60105EE6C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "mwcomutil.h"
#include "PlotSin_1_0.h"
/
// CVCPlotSinDlg dialog
class CVCPlotSinDlg : public CDialog
{
// Construction
public:
CVCPlotSinDlg(CWnd* pParent = NULL); // standard constructor
IPlotSinclass *pcom;
然后,为对话框添加一个按钮。在按钮的响应函数下面写上如下程序:
void CVCPlotSinDlg::OnBtnPlotsin()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
HRESULT hcom=CoCreateInstance(CLSID_PlotSinclass,NULL,CLSCTX_ALL,IID_IPlotSinclass,(void**)&pcom);
if(FAILED(hcom))
{
AfxMessageBox("创建COM组件失败");
return;
}
pcom->PlotSin();
}
并为CVCPlotSinDlg类添加一个析构函数,如下:
CVCPlotSinDlg::~CVCPlotSinDlg()
{
if(pcom!=NULL)
{
pcom->Release();
pcom=NULL;
}
CoUninitialize();
}
然后运行程序,点击按钮即可画出一个从-pi到pi的正弦曲线,如下:
五、带输入参数的COM组件
至于详细的步骤,和前面的差不多。在这里,我就给出M文件和VC的程序,至于配置步骤和前面一样。
M文件:plotcom.m
function []=plotcos(x)
t=0:0.1:(x*pi);
y=cos(t);
plot(t,y);
VC中的部分调用程序:
void CVCPlotCosDlg::OnBtnPlotcos()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
HRESULT hcom=CoCreateInstance(CLSID_PlotCosclass,NULL,CLSCTX_ALL,IID_IPlotCosclass,(void**)&pcom);
if(FAILED(hcom))
{
AfxMessageBox("创建COM组件失败");
return;
}
COleVariant x=(double)2;
pcom->PlotCos((VARIANT)x);
}
运行界面如下:
运行结果如下:
当将输入参数改为3的时候,运行结果如下: