LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Help creating mex file that uses DLL built in Labwindows/CVI...

Has anyone successfully used dlls created with labwindows with a mex file created in Matlab?
 
Specifically:
Matlab R14 sp3
Mex uses lcc version 2.4.1
 
I have a winsock wrapper api created by a colleague in Labwindows/CVI.  I built a dll from it.
 
For the mex file, I created a mexFunction and a simple function to call dll functions.  I can get the mex compilation to work fine and the mex file will run if I don't call functions from the dll.  However, if I add calls to functions in the dll, compile and then run the resultant mex file Matlab responds, "??? Invalid MEX-file 'e:\projects\mcomfm\src\mcomfm.mexw32': The specified procedure could not be found."
 
I tried adding cvirt.lib to the command line and adding the path to cvirt.dll for Matlab.  That did nothing.  I still get the same result.
 
Specifically, at the Matlab command prompt I'm typing:
mex -v -f petemexopts.bat mcomfm.c mapidll.lib "C:\Program Files\MeasurementStudio\cvi\extlib\cvirt.lib"
 
Has anyone successfully done this before?  Any input greatly appreciated as it is much less painful than pulling out gobs of hair!
 
Thanks,
-shrew
0 Kudos
Message 1 of 4
(4,093 Views)
Hi Shrew,

Calling a CVI dll using a MEX file should't be any different than calling a text-based language dll like C or C++.

Did you try using the dll from CVI itselft to make sure that it was built correctly? Another thing I noticed is that you are including the cvirt.lib that should't be relevant for running your dll, instead you need to make sure you include the .lib created for the dll. For example if your dll is called myfunction.dll you might need to add myfunction.lib for the call to work.

Double check the Matlab documentation on how to call dlls using a MEX file for a text-based language to make sure you are doing it correctly.

Good luck,

Tica T
Applications Engineer
National Instruments

0 Kudos
Message 2 of 4
(4,069 Views)

Hi Tica,

Thanks for the response.  I actually built a little test dll from a Labwindows/CVI example I found on ni.com.  I added one function (besides DllMain) to return 3.14159 as a double.  Here's the source:

#include <ansi_c.h>
#include <cvirte.h>
#include <cvidef.h>

int DLLEXPORT myvar = 0;

int __stdcall DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
 switch (fdwReason) {
  case DLL_PROCESS_ATTACH:
   if (InitCVIRTE (hinstDLL, 0, 0) == 0)
    return 0;   /* out of memory */
   break;
  case DLL_PROCESS_DETACH:
   CloseCVIRTE ();
   break;
 }
 
 return 1;
}

double DLLEXPORT returnValue(void) {
 double value = 3.14159;

 
 return(value);
}

I have read in cvi\manuals\progref.pdf (pg 3-11) that to resolve references to Utility Library calls you need to add cvi\extlib\refsym.c to the project.  Since the DllMain function calls InitCVIRTE and CloseCVIRTE I added refsym.c.  I did this because I thought that matlab crapped when it tried to initialize testdll.dll for use in the mex file hanging on the unreferenced CVI calls.  However, adding refsym.c to the project did nothing for me and I'm still getting the "procedure not found" thing.

testdll.dll works fine used with LW/CVI compiled code.  Mex/LCC happily compiles everything including refsym.c and hapily links everthing, too.  But when I try to run the mex file matlab still responds saying "specified procedure could not be found.  Now I wonder what procedure it cannot find.

I've even gone as far as to place all the dlls in the same directory as the mex file thinking there was some kind of PATH problem searching for the dlls: testdll.dll, cviauto.dll, cvibgi.dll, cviogl.dll, cvirt.dll, cvirte.dll.  I don't even know what they're all for, it was just a shot in the dark and it didn't work either.

-shrew

0 Kudos
Message 3 of 4
(4,050 Views)

I figured out what the problem was a while ago.  I figured that I would post it here in case someone else comes a searchin' for the answer.

To use the dll created in LabWindowsCVI you have to build a static library listing all the functions found in the dll.  CVI does this, but it is not compatible with lcc compiler packaged with Matlab which is what I use to build mex files.  There is a utility in Matlab in the lcc directory (MATLAB_ROOT\sys\lcc\bin) to build a proper static library for lcc to use when linking.  It is called buildlib.exe.  In order to create the .lib file you have to create an exports file for buildlib to read.  The file basically lists the functions found in the dll.

For instance if you built a dll named zurbit.dll with 3 functions named foo, bar, hoo.  Then the contents of the export file would look like this:

zurbit.dll

_foo

_bar

_who

You can name the file whatever you want but the following syntax will create a static library with the same name as the export file name so it is a good idea to use the same name for the export file as the name of the dll to be used.  In order to build the static library call "buildlib.exe zurbit.exp"  This will create a static library called zurbit.lib for use when linking in the function names found in the dll.  Use this library when building your mex file and you should be good to go!

 

-shrew

Message 4 of 4
(3,943 Views)