Matlab 版 (精华区)
发信人: billsun (为了明天), 信区: Matlab
标 题: m文件作成可被vc或vb调用的dll的教程
发信站: 哈工大紫丁香 (2002年10月27日22:04:57 星期天), 站内信件
我从mathworks网站上找的,觉着奖得比较通俗易懂,大家共享一下
Problem Description
How do I call a Compiler generated stand-alone DLL from my Visual Basic appl
ication in MATLAB 5.3.x?
I would like to call a DLL generated by the MATLAB Compiler and linked to th
e C/C++ Math Libraries from my Visual Basic application.
Solution:
PLEASE NOTE: If you are using MATLAB 6.0 (R12) please refer to the following
URL:
http://www.mathworks.com/support/solutions/data/28621.shtml
If you are using MATLAB 5.3.x:
This solution will provide you with a complete example that will take you fr
om an M-file (created in MATLAB 5.3.x) to a Visual Basic application that ca
lls a Compiler generated DLL. This example is intended to show you the steps
to take you from start to finish and does not emphasize the functionality o
f the DLL or Visual Basic application.
Please Note: There are many ways which this example can be changed to suit a
particular functionality. If you have any questions about this example, ple
ase refer to the documentation. We have provided examples in the manuals and
online references to assist you with modifying the code. For instance, many
questions that arise are about manipulating mxArrays (i.e. creating, extrac
ting data from, mulit-dimensional, etc.). To answer these questions, you can
refer to Chapter 3 of the Application Program Interface Guide or refer to t
he MATLAB Helpdesk. To access this part of the Helpdesk:
1. Type "Helpdesk" at the MATLAB prompt. This will open the MATLAB Helpdesk
in your web browser.
2. Click on the "Application Program Interface Guide" link.
3. Click on the function of interest for a detailed reference.
Other common questions are related to the functionality of the routines in t
he Math Library. Please refer to the C/C++ Math Library User's Guide and als
o the online reference in the Helpdesk. To access this part of the Helpdesk,
replace step 2 above with
2. Click on the "MATLAB C Math Library 2.0 Ref." link.
We are willing to investigate any problem or question that you may have. How
ever, in order to resolve your issue, you must narrow down the problem and c
reate an example that does not require the Visual Basic or Visual C/C++ envi
ronment.
The following example was created using Microsoft Visual C/C++ 5.0 and Visua
l Basic 6.0. Instructions for other C compilers and/or versions of Visual Ba
sic are not available because it is not practical for us to offer complete t
echnical support on the details of using any specific one of the large numbe
r of IDE environments our customers use. If you need detailed assistance wit
h the particular settings needed to get your IDE environment to generate cod
e that successfully compiles and runs with our products, we suggest you cont
act the manufacturer of your IDE to get the necessary information or expert
technical assistance in using it.
NOTE: These instructions have been used successfully by many customers. Almo
st every problem reported to us has been the result of a typographical error
, skipped steps, or mistakes in compiler settings. If you encounter problems
with this example, please retrace or repeat these steps before contacting u
s.
In MATLAB,
1. Write a function named foo that contains the following code,
function y=foo(x)
y=x+1;
2. Use the MATLAB Compiler to compile foo.m into C code that can be included
in a C shared library.
mcc -W lib:foolib -L C -t foo.m
This will generate the following files.
foo.c
foolib.c
foolib.h
foolib.exports
In Microsoft Visual C/C++ 5.0
1. Start up the Microsoft Visual C/C++ 5.0 IDE
2. Go to FILE and NEW. Click on Projects Tab. Select Win32 Dynamic-Link Libr
ary. In the Project Name field type: test. Create new workspace should be fi
lled in. In the Platforms field, Win32 should also be checked. Click OK.
3. Go to the MAIN menu and click on Project, Add to Project, New. Click on P
rojects Tab. Select Win32 Static Library. In the Project Name field type in
libmx. Add to current workspace should be filled in. Dependency of should be
checked. In the listbox select test. In the Platforms field, Win32 should b
e checked. Click OK.
4. Highlight Workspace 'test': 2 project(s) and then right click. Select Add
new project to workspace. Click on Projects Tab. Select Win32 Static Librar
y. In the Project Name field type in libmcc. Add to current workspace should
be filled in. Dependency of should be checked. In the listbox select test.
In the Platforms field Win32 should be checked. Click OK.
5. Do step 4 for libmmfile and libmatlb.
6. Highlight test files. Right click and select Set as Active Project.
7. Highlight test files. Right click and select New Folder. In the Name of t
he new folder field type in Sources. Click OK.
8. Do step 7 for libmatlb files, libmx files, libmcc files, and libmmfile fi
les. Replace test with the appropriate names.
9. Double click on test files. Highlight Sources and then right click. Selec
t Add files to folder. Find and choose the foo.c file (created above).
10. Double click on test files. Highlight Sources and then right click. Sele
ct Add files to folder. Find and choose the foolib.c file (created above).
11. Highlight test files. Choose from the menu, Project, Add to Project, New
. Under the Files tab choose C++ Source file. Name the file foowrap.c. Make
sure Add to Project is checked. Make sure test appears in the listbox. Pleas
e Note: Be sure to use the .c extension for the name of the file.
12. Enter the following code in the foowrap.c file.
#include "foolib.h"
#include "matlab.h"
double __stdcall foowrap(double x)
{
mxArray *x_ptr;
mxArray *y_ptr;
double *y;
double ret;
/* Create an mxArray to input into mlfFoo */
x_ptr = mlfScalar(x);
y_ptr = mlfFoo(x_ptr);
/* The return value from mlfFoo is an mxArray so we must extract the data f
rom it */
y = mxGetPr(y_ptr);
ret = *y;
/* Return a double precision number to Visual Basic */
return(ret);
}
13. Highlight test files. Choose from the menu, Project, Add to Project, New
. Under the Files tab choose Text file. Name the file test.def. Make sure Ad
d to Project is checked. And make sure test appears in the listbox. Note, be
sure to use the .def extension for the name of the file.
14. Enter the following code into the test.def file.
LIBRARY test.dll
EXPORTS
foowrap
15. Double click on libmx files. Highlight Sources and then right click. Sel
ect Add files to folder. Go to $MATLAB/extern/include and select libmx.def.
16. Do step 15 for libmatlb files, libmcc files, and libmmfile files. Replac
e libmx with the appropriate names.
17. Double click on libmatlb files and double click on Sources. Right click
on libmatlb.def and select Settings. This brings up a Project Settings windo
w. Click on Custom Build Tab. In the Build Command(s) field enter:
lib /def:"c:\matlab\extern\include\libmatlb.def" /machine:ix86 /OUT:libmatlb
.lib
c:\matlab represents the root directory where MATLAB is installed on your sy
stem.
In the Output file(s) field type: libmatlb.lib.
Click OK.
18. Do step 17 for libmx files, libmmfile files, and libmcc files. Replace l
ibmatlb with the appropriate names.
19. Highlight test files and right click. Select Settings. Click on the C/C+
+ Tab. In the Category listbox select Code Generation. In the Use Runtime li
brary listbox select Multithreaded DLL. Change the Category listbox to Prepr
ocessor. Add to the Preprocessor definitions MSVC, MSWIND, IBMPC so:
WIN32,_DEBUG,_CONSOLE,_MBCS
changes to:
WIN32,_DEBUG,_CONSOLE,_MBCS,MSVC,MSWIND,IBMPC
Add to the Additional include directories field:
c:\matlab\extern\include\cpp;c:\matlab\extern\include;
Also add to the Additional include directories field the directory in which
foolib.h is located.
20. Go to Build and Rebuild All.
21. You should now have built test.dll.
In Visual Basic
1. Launch Visual Basic and choose a New Standard EXE Project.
2. Drag a Push Button into the Project.
3. Double click on the Push Button in Form 1. This will open a new window na
med Project1-Form1 (Code).
4. Choose General from the left listbox.
5. Enter the following code. You may need to enter the exact path to test.dl
l for the VB program to find this file.
Dim y As Double
Private Declare Function foowrap Lib "test.dll" (ByVal x As Double) As Doubl
e
6. Choose Command1 from the left listbox.
7. Enter the following code
y = foowrap(1.1)
MsgBox y
The complete code should now look like:
Dim y As Double
Private Declare Function foowrap Lib "H:\Cases\vbdll\test\Debug\test.dll" (B
yVal x As Double) As Double
Private Sub Command1_Click()
y = foowrap(1.1)
MsgBox y
End Sub
8. Choose Run, Start
9. Push the button. A message box should now appear with the value 2.1.
--
我是谁??
我的特点:一不浪漫;二不痞;三不帅;四没钱;五怕死;六爱玩;七贪吃; 八九不离十
;是个大坏蛋。是吗?
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.247.147]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.124毫秒