calling functions in existing dll files from stand-alone MATLAB programs

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Thu, 20 Jan 2005 10:47:36


Hello,

I have a dll file produced by a 3rd party called s232.dll - and I can
call functions within it from the MATLAB command line without any
problem.

What do I need to do to be able to call functions within it from
stand-alone programs created through matlab? I have created a program
with a number of M-files, and it runs fine until it encounters a
function call to the s232.dll, then it just spits out errors saying
contact vendor if its a matlab dll, or check the mex source code if
it is not.

Does anyone have any ideas?

thanks heaps:)

Justin.
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by Peter Web » Fri, 21 Jan 2005 05:54:04

The most reliable way to do this is to write a MEX function that links
against s232.dll and provides a MEX interface to the functions in that DLL.
MATLAB Compiler cannot fully support MATLAB's generic DLL interface, because
in calling the DLL functions MATLAB generates M-files, and these unencrypted
M-files cannot be executed by the MATLAB Compiler generated application.

However, if you're willing to execute a manual step before compilation, you
can use the generic DLL interface with compiled programs. Assume you have
GenericDLL.dll which exports a function fnGenericDLL. Then, you'd do this:

'GenericDLLAPI.m');

(This generates the M file GenericDLL.m, which the compiled application can
use. ExportedCDLL.h must have C declarations (not extern "C") for the
exported functions in GenericDLL.dll.)


where:

function CallGeneric
if ~libisloaded('GenericDLL')
loadlibrary('GenericDLL.dll', @'GenericDLLAPI);
end
x = calllib('GenericDLL', 'fnGenericDLL');
disp(['The Answer: ' num2str(x)]);

This will ONLY work on Windows. The generic DLL interface is NOT supported
on any other platform.

 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by Todd Flana » Fri, 21 Jan 2005 05:58:20

A couple of questions:
MATLAB Version 7?
Are you calling mex functions or using loadlibrary?
Is the dll in the yourexecutablenamehere_mcr directory?
Could you post the mcc -mv ... output?
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Fri, 21 Jan 2005 07:08:12

odd Flanagan wrote:

MATLAB Version 7 SP1


huh? Sorry, no idea. I have not been able to understand the help
files enough to know. I have a s232.dll file, and can call functions
within it using:

S232('function_name',function_inputs);


no, its in a seperate directory (c:\tdt\s232\MATLAB\s232.dll)



To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

Compiler version: 4.1 (R14SP1)
Parsing file "c:\matlab701\work\new scripts3\untitled.m"
(Referenced from: "Compiler Command Line").
Parsing file "c:\matlab701\toolbox\compiler\deploy\matlabrc.m"
(Referenced from: "Compiler Command Line").
Parsing file "c:\matlab701\toolbox\compiler\dirname.m"
(Referenced from: "Compiler Command Line").
Parsing file "c:\matlab701\toolbox\compiler\deploy\deployprint.m"
(Referenced from: "Compiler Command Line").
Parsing file "c:\matlab701\toolbox\matlab\uitools\gui_mainfcn.m"
(Referenced from: "c:\matlab701\work\new scripts3\untitled.m").
Parsing file "c:\matlab701\toolbox\matlab\uitools\guidata.m"
(Referenced from: "c:\matlab701\work\new scripts3\untitled.m").
Parsing file "c:\matlab701\toolbox\compiler\deploy\hgrc.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\matlabrc.m").
Parsing file "c:\matlab701\toolbox\matlab\strfun\str2double.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\matlabrc.m").
Parsing file "c:\matlab701\toolbox\matlab\general\usejava.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\matlabrc.m").
Parsing file "c:\matlab701\toolbox\matlab\iofun\fileparts.m"
(Referenced from: "c:\matlab701\toolbox\compiler\dirname.m").
Parsing file "c:\matlab701\toolbox\matlab\graphics\gcbf.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\deployprint.m").
Parsing file "c:\matlab701\toolbox\matlab\general\ispc.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\deployprint.m").
Parsing file "c:\matlab701\toolbox\matlab\graphics\print.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\deployprint.m").
Parsing file "c:\matlab701\toolbox\matlab\uitools\printdlg.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\deployprint.m").
Parsing file "c:\matlab701\toolbox\matlab\iofun\tempname.m"
(Referenced from:
"c:\matlab701\toolbox\compiler\deploy\deployprint.m").
Generating file "untitled_main.c".
Depfun main loop, iteration 1
Processing C:\MATLAB701\toolbox\matlab\mcc.enc
1 items added
Processing C:\MATLAB701\toolbox\control\mcc.enc
1 items added
Processing C:\MATLAB701\toolbox\nnet\mcc.enc
1 items added
Processing C:\MATLAB701\work\new scripts3\untitled.fig
1 items added
Processing dependencies...
0 items added
Depfun main loop, iteration 2
Processing dependencies...
0 items added
Processing include files...
2 items added.
Processing exclude list...
0 items removed.
Processing installed directories...
1849 items removed.
Generating MATLAB path...
Created 40 path items.
Depfun main loop converged in 2 iterations, total number of files =
230
Generating file "untitled_mcc_component_data.c".
Executing command: mbuild -O -v -output "untitled" "untitled_main.c"
"untitled_mcc_component_data.c" -link exe
This is mbuild Copyright 1984-2004 The MathWorks, Inc.

-> Default options filename found in C:\Documents and
Settings\jbourke\Application Data\MathWorks\MATLAB\R14
----------------------------------------------------------------
-
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Fri, 21 Jan 2005 07:33:38


Is this what is being done by the steps you have given me below?


I'm not sure exactly what you mean, sorry. What is the generic DLL
interface and how is this related to using my s232.dll file? I can
call functions from s232.dll at the command line, just not from the
stand-alone application, I assume this is why?

'mfilename',

All I have is a s232.dll file, I dont have a header file, and I am
not sure which mfilename I am suppose to put in place, nor which m
file should be substituted for 'GenericDLLAPI.m'. sorry.


After doing this, do I just call:

mcc -m my_application my_mfiles s232.m

assuming that I am able to create s232.m?

Sorry for being dense, but I have very little experience with writing
stand-alone applications with matlab.

Thanks for your help,
Justin.
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by Peter Web » Sat, 22 Jan 2005 02:04:36


See the documentation for LOADLIBRARY.


loadlibrary('s232.dll', 's232.h', 'mfilename', 's232API.m');

(This generates the M file s232API.m, which the compiled application can
use. s232.h must have C declarations (not extern "C") for the exported
functions in s232.dll. If you don't have s232.h, you'll need to write it.)

Then, you modify my_application to look like this:

function my_application
if ~libisloaded('s232')
loadlibrary('s232.dll', @s232API);
end
x = calllib('s232', 'function_name', function_inputs);

Test it in MATLAB. Then compile it:

mcc -m my_application my_mfiles s232.m
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Sat, 22 Jan 2005 08:45:28

Hi Peter,

I have written a s232.h file, but have only included a few functions
to begin with, as there are a few hundred functions in s232.dll, is
this ok, or will it cause problems? My header file looks like this:

#ifndef s232_h
#define s232_h

int S2init(int dn, int mode, int apt);
int XBlock(int mtry, int fstart);
void XBunlock(int fend);
void PA4atten(int din, float level);
void S2close(void);

#endif /* s232_h */

when running the command:

loadlibrary('s232.dll', 's232.h', 'mfilename', 's232API.m');

I get the response:

Warning: The function 'S2init' was not found in the library
Warning: The function 'XBlock' was not found in the library
Warning: The function 'XBunlock' was not found in the library
Warning: The function 'PA4atten' was not found in the library
Warning: The function 'S2close' was not found in the library

then not suprisingly, my_application does not run, giving the error:

??? Error using ==> calllib
method was not found.

Error in ==> my_application at 8
calllib('S232','S2init',1,'INIT_SECONDARY',1000); %initialise
AP card

thanks again,
Justin.
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by Peter Web » Sat, 22 Jan 2005 23:37:43

LOADLIBRARY examines your DLL, looking for exported functions that match the
signatures of the functions given in your header file. You can look at the
exported functions yourself with the DOS utility DUMPBIN.

C:\> dumpbin /exports s232.dll

Do the names in your header file match those in the list of functions
exported from the DLL?
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Tue, 25 Jan 2005 08:07:42

Hi Peter,

I am still trying to get a copy of dumpbin.exe, I will let you know
what I come up with when I find a copy.

Thanks,
Justin.
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by rraman5 » Sat, 05 Feb 2005 06:40:08


Hi,

I have a similar problem in that my DLL loads fine with

loadlibrary alt.dll alt.h, but then when I try to see the functions
using

libfunctions alt.dll, I get

No methods for class lib.alt.dll or no class lib.alt.dll.

I used the programs dumpbin as well as dependency as suggested, and
they give all the dll functions perfectly. Checking the .h file shows
that all the functions are given in

//function names
#define nmfunction1 "FUNCTION1"
#define nmfunction2 "FUNCTION2"
etc.

and (//function definitions) then gets into class definition,
including protected and public (what the functions are and what they
return are defined).

I am very new to C++ and very confused about how to get the dll
functions to work in Matlab. Should I re-write the .h file to reflect
the functions only? This header was written for C++, and I imagine
that the definition of the class is not being interpreted correctly.

Thanks for any help you can provide.

Regards,
/Ryan


match the
at the
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by Todd Flana » Sat, 05 Feb 2005 23:36:57

I'm not clear if you are trying to use loadlibrary from MATLAB or a compiled
app. If the latter, loadlibrary isn't supported in compiled applications:

http://www.yqcomputer.com/
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by rraman5 » Tue, 08 Feb 2005 02:57:21


Hi,

Yes, I am trying to use loadlibrary from MATLAB. The problem is now
clear that my header file doesn't quite match the dll (it is slightly
different). I would like to edit it to match the dll, but am not too
well versed in this.

Do you have any examples that make clear the requirements for a dll
header file?

Regards,
/Ryan


compiled
applications:
href=" http://www.yqcomputer.com/ "> http://www.yqcomputer.com/ ;/a>
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Tue, 08 Feb 2005 07:01:57

Todd says:

loadlibrary isn't supported in compiled applications

Peter says:

look at loadlibrary documentation,

I am trying to use functions within a dll file in a compiled program,
am I suppose to use loadlibrary or not?

I am sorry for being abrupt, but there are a lot of people searching
for a solution to this problem, and no posted solution seems to have
been successful yet. I am still trying to get hold of dumpbin.exe
file, but I have to purchase other software to get it, so it wont
arrive till later this week.

Thanks,
Justin.
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by rraman5 » Tue, 08 Feb 2005 22:46:31


Justin,

Are you sure about that? I just ran the MS-DOS prompt from WinXP Home
SP1, and dumpbin worked from there using the syntax posted.

Regards,
/Ryan
 
 
 

calling functions in existing dll files from stand-alone MATLAB programs

Post by justin bou » Wed, 09 Feb 2005 07:18:33

Yes Ryan, I'm sure,

dumpbin.exe comes with Vstudio .NET, I dont have that. You would
probably have that installed. A search of my hard drive came up with
nothing, I am purchasing the software needed but that is not really
the problem that I am having.

thanks,
Justin.


dumpbin.exe
wont