Click here to Skip to main content
Licence 
First Posted 18 Nov 2003
Views 264,970
Bookmarked 40 times

MATLAB Generic DLL

By | 18 Nov 2003 | Article
Call a Dynamic Linked Library from MATLAB using MATLAB Interface to Generic DLLs.

Note: This article requires the MATLAB Interface to Generic Dlls. No link is available, but if you know of one please let us know.

Sample Image - MatlabGenericDll_1.jpg

Introduction

A shared library is a collection of functions that are available for use by one or more applications running on a system. On Windows operating systems, the library is compiled into a dynamic link library (.dll) file. At run-time, the library is loaded into memory and made accessible to all applications.

The MATLAB Interface to Generic DLLs enables you to interact with functions in dynamic link libraries directly from MATLAB.

Dynamic link libraries are easily accessed by MATLAB through a command line interface. This interface gives you the ability to load an external library into MATLAB memory space and then access any of the functions exported from library. Although data types differ between the two language environments, in most cases, you can pass MATLAB types to the C functions without having to do the work of conversion. MATLAB does this for you.

This interface also supports libraries containing functions programmed in languages other than C, provided that the functions have a C interface.

To give MATLAB access to external functions in a shared library, you must first load the library into memory. Once loaded, you can request information about any of the functions in the library and call them directly from MATLAB. When the library is no longer needed, you will need to unload it from memory to conserve memory usage.

Loading the Library

To load a shared library into MATLAB, use the loadlibrary function. The syntax for loadlibrary is:

loadlibrary('your_dll', 'header_file')

where your_dll is the filename for the .dll library file, and header_file is the filename for the header file that contains the function prototypes.

Here is an example:

loadlibrary('MatlabGenericDll', 'GenericDll.h')

Unloading the Library

To unload the library and free up the memory that it occupied, use the unloadlibrary function. For example:

unloadlibrary MatlabGenericDll

Getting Information About the Library

You can use either of these two functions to get information on the functions available in a library that you have loaded:

libmethods('libname')
libmethodsview('libname')

The main difference is that libmethods displays the information in the MATLAB Command Window (and you can assign its output to a variable), and libmethodsview displays the information as a graphical display in a new window (figure 2). To see what functions are available in the MatlabGenericDll library, use libmethods, specifying the library filename as the only argument.

libmethods MatlabGenericDll

LibMethodsView Window for displaying exported functions in a Dll

Invoking Library Functions

Once a library has been loaded into MATLAB, use the calllib function to call any of the functions from that library. Specify the library name, function name, and any arguments that get passed to the function:

calllib('libname', 'funcname', arg1, ..., argN)

Here is an example (demo project) to call Format dialog from MATLAB (See figure 1):

loadlibrary('MatlabGenericDll','GenericDll.h')
calllib('MatlabGenericDll', 'FormatDrive')
unloadlibrary MatlabGenericDll

For more information and instruction to setup this interface, refer to mathworks site or click here.

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

A. Riazi



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: Calling a Matlab dll from Matlab Pinmembercat_e020:34 23 Aug '07  
Generaldll don't register in window PinmemberReinaldo Teixeira7:10 18 Dec '06  
GeneralMultiple DLLs Pinmemberhendrylibra11:26 6 Dec '06  
GeneralUsing MATLAB generated shared libraries in external C++ apps Pinmembermario627:59 15 Nov '06  
GeneralRe: Using MATLAB generated shared libraries in external C++ apps PinmemberAKG20:44 19 Dec '06  
Generalc-mex and generic dll Pinmemberburstain21:52 15 Oct '06  
Questionplz!! HELP!!! call DLL from MATLAB [modified] Pinmembervnc_cp22:49 29 Aug '06  
I have a dll. it is OK in C/C++ and LABView.
 
now i'm trying to call dll from Matlab.
loading and unloading are oke!!
 

the dll is test.dll and header is test.h
test.h's content is:
extern "C" int PASCAL EXPORT Driver(void ** hDev);
 
M-file in Matlab
 
loadlibrary('test','test.h');
libfunctions test -full
arg = libpointer('voidPtrPtr');
%calllib('test','Driver',arg);
unloadlibrary('test');
 
---
 
when i use as above. It gives the warning message:
 

------------------------------------------------------------------------
Segmentation violation detected at Wed Aug 30 15:25:54 2006
------------------------------------------------------------------------
 
Configuration:
MATLAB Version: 7.0.1.24704 (R14) Service Pack 1
MATLAB License: 253616
Operating System: Microsoft Windows XP
Window System: Version 5.1 (Build 2600)
Processor ID: x86 Family 6 Model 6 Stepping 5, GenuineIntel
Virtual Machine: Java 1.4.2_04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM
(mixed mode)
Default Charset: ibm-5348_P100-1997
 
Register State:
EAX = ffffffff EBX = 108dbfb0
ECX = ffffffff EDX = 79b32664
ESI = 0f72e508 EDI = 00cddc0c
EBP = 00cddbe4 ESP = 00cddbe0
EIP = 788dc2d2 FLG = 00010286
 
Stack Trace:
[0] udd.dll:public: class UDInterface * __thiscall UDInterface::getReference(void)(0xffffffff, 0x108db6d0 "test", 0x00cddfa0, 0x108db830 "Driver") + 2 bytes
[1] libmwcli.dll:void __cdecl CallLibFunction(char *,char *,int,struct mxArray_tag * * const,int,struct mxArray_tag const * * const)(0x108db6d0 "test", 0x108db830 "Driver", 1, 0x00cddf38) + 572 bytes
[2] libmwcli.dll:_mfCallLib(0, 0x00cddf38, 4, 0x108db6d0 "test") + 142 bytes
[3] m_dispatcher.dll:public: virtual void __thiscall Mfh_builtin::dispatch_mf(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cddf38, 4, 0x00cddf98) + 55 bytes
[4] m_dispatcher.dll:public: virtual void __thiscall Mfh_MATLAB_fn::dispatch_fh(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cddf38, 4, 0x00cddf98) + 200 bytes
[5] m_interpreter.dll:int __cdecl mdDispatch(int,char const *,int,struct mxArray_tag * *,int,struct mxArray_tag * *,class Mfh_MATLAB_fn * *)(136, 0x014a0545 "calllib", 0, 0x00cddf38) + 88 bytes
[6] m_interpreter.dll:_inDispatchFromStack(136, 0x014a0545 "calllib", 0, 4) + 801 bytes
[7] m_interpreter.dll:_inCallFcnFromReference(0, 0x108dd070, 0x789b6a50, 0xcccccccd) + 176 bytes
[8] m_interpreter.dll:int __cdecl inInterp(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag volatile *)(1, 0, 9, 0) + 4160 bytes
[9] m_interpreter.dll:int __cdecl inInterPcodeSJ(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag *)(1, 0, 1, 0) + 272 bytes
[10] m_interpreter.dll:int __cdecl inExecuteMFunctionOrScript(class Mfh_mp *,bool)(0x0f6a39a0, 1, 0, 0x7893a5a0) + 778 bytes
[11] m_interpreter.dll:_inExecCompScript(0, 0x00cde6ac, 0x0f6a39a0, 0xffffffff) + 327 bytes
[12] m_interpreter.dll:public: void __thiscall Mfh_mp::inRunMP(int,struct mxArray_tag * *,int,struct mxArray_tag * *,struct inWorkSpace_tag *)(0, 0x00cde6ac, 0, 0x00cde70c) + 122 bytes
[13] m_interpreter.dll:public: virtual void __thiscall Mfh_mp::dispatch_file(struct _mdUnknown_workspace *,int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0, 0x00cde6ac, 0) + 28 bytes
[14] m_interpreter.dll:public: virtual void __thiscall Mfh_mp::dispatch_file(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cde6ac, 0, 0x00cde70c) + 26 bytes
[15] m_dispatcher.dll:public: virtual void __thiscall Mfh_file::dispatch_fh(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cde6ac, 0, 0x00cde70c) + 273 bytes
[16] m_interpreter.dll:int __cdecl mdDispatch(int,char const *,int,struct mxArray_tag * *,int,struct mxArray_tag * *,class Mfh_MATLAB_fn * *)(480, 0x012d66c4 "Untitled", 0, 0x00cde6ac) + 88 bytes
[17] m_interpreter.dll:_inDispatchFromStack(480, 0x012d66c4 "Untitled", 0, 0) + 801 bytes
[18] m_interpreter.dll:enum opcodes __cdecl inDispatchCall(char const *,int,int,int,int *,int *)(0x012d66c4 "Untitled", 480, 0, 0) + 138 bytes
[19] m_interpreter.dll:int __cdecl inInterp(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag volatile *)(2, 0, 0, 0) + 2359 bytes
[20] m_interpreter.dll:int __cdecl inInterPcodeSJ(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag *)(2, 0, 0, 0) + 272 bytes
[21] m_interpreter.dll:_inInterPcode(2, 0x78773d54, 0, 0) + 69 bytes
[22] m_interpreter.dll:enum inExecutionStatus __cdecl in_local_call_eval_function(int *,struct _pcodeheader *,int *,struct mxArray_tag * * const,enum inDebugCheck)(0x00cdf2c8, 0x00cdf3b4, 2, 1) + 162 bytes
[23] m_interpreter.dll:$L73181(0x78773d54, 0x0f71e010 "Untitled\n", 0, 0) + 196 bytes
[24] m_interpreter.dll:enum inExecutionStatus __cdecl inEvalCmdWithLocalReturnandtype(char const *,int *,enum inDebugCheck)(0x0f71e010 "Untitled\n", 0, 2, 0x00cdf444 "ôôÍ") + 69 bytes
[25] m_interpreter.dll:_inEvalCmdNoEnd(0x0f71e010 "Untitled\n", 0x00cdf4e4, 0x00cdf49c, 0x014a7f38) + 16 bytes
[26] bridge.dll:_mnParser(0x77e79f93, 0x014a7f38, 0, 0) + 431 bytes
[27] mcr.dll:public: void __thiscall mcrInstance::mnParser(void)(271244, 0x4d5c3a43, 0x414c5441, 0x31303742) + 87 bytes
[28] MATLAB.exe:0x00401d2f(4194304, 0, 271244, 0x014a7f38)
[29] MATLAB.exe:0x00403e45(0, 1, 0x7ffdf000, 0xfb76ecf4)
[30] kernel32.dll:0x77e7eb69(0x00403cc0 "jth8U@", 0, 0, 0)
 
Please follow these steps in reporting this problem to The MathWorks so
that we have the best chance of correcting it:
 
1. Send this crash report to segv@mathworks.com for automated analysis.
For your convenience, this information has been recorded in:
E:\DOCUME~1\NGUYEN~1\LOCALS~1\Temp\matlab_crash_dump.956
 
2. Also, if the problem is reproducible, send the crash report to
support@mathworks.com along with:
- A specific list of steps that will reproduce the problem
- Any M, MEX, MDL or other files required to reproduce the problem
- Any error messages displayed to the command window
A technical support engineer will contact you with further information.
 
Thank you for your assistance. Please save your workspace and restart
MATLAB before continuing your work.
 
Error in ==> Untitled at 5
calllib('test','Driver',arg)
 

----
Where is my error???
 
----
Help please!
 
Thanks
 

 
-- modified at 21:52 Wednesday 30th August, 2006
QuestionRe: plz!! HELP!!! call DLL from MATLAB PinmemberRadDeveloper6:23 26 Feb '07  
QuestionCan I take "Ogre" into MATLAB? Pinmemberkbigdelysh4:03 12 Aug '06  
AnswerRe: Can I take "Ogre" into MATLAB? PinmemberA. Riazi5:52 12 Aug '06  
GeneralLCC preprocessor error [modified] PinmemberA.S.Nandagopalan11:21 12 Jul '06  
QuestionI am confused :( [modified] Pinmemberareddy20065:20 7 Jul '06  
GeneralTo Mr Riazi, thanks a lot PinmemberSylvianne2:56 30 Jun '06  
AnswerRe: To Mr Riazi, thanks a lot PinmemberA. Riazi3:35 30 Jun '06  
QuestionCan i use these header files with loadlibrary? PinmemberSylvianne2:31 23 Jun '06  
Generalmbuild -setup PinmemberSylvianne21:31 20 Jun '06  
GeneralRe: mbuild -setup PinmemberSylvianne21:33 20 Jun '06  
AnswerRe: mbuild -setup PinmemberA. Riazi0:20 23 Jun '06  
GeneralRe: mbuild -setup PinmemberSylvianne2:33 23 Jun '06  
GeneralWrapper DLL to load DLL in Matlab PinmemberSylvianne1:35 19 Jun '06  
AnswerRe: Wrapper DLL to load DLL in Matlab PinmemberA. Riazi21:45 19 Jun '06  
GeneralRe: Wrapper DLL to load DLL in Matlab PinmemberSylvianne21:52 19 Jun '06  
AnswerRe: Wrapper DLL to load DLL in Matlab PinmemberA. Riazi23:41 19 Jun '06  
GeneralRe: Wrapper DLL to load DLL in Matlab PinmemberSylvianne4:14 20 Jun '06  
GeneralRe: Wrapper DLL to load DLL in Matlab PinmemberSylvianne21:57 19 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 19 Nov 2003
Article Copyright 2003 by A. Riazi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid