Trouble connecting to dll file from MATLAB

I am having trouble connecting MATLAB to a C++ .dll library. First I tried using the loadlibrary function in MATLAB, but this gave many syntax errors when building the _thunk_pcwin64 file (I assume because loadlibrary only works with C). I am using R2011a on 64-bit Windows 7.
Next, I tried using mex to interface with the library. My code (named attotest.cpp) is given at the end. The function compiles fine in MATLAB with mex using Microsoft Visual C++ 2010 Express. When I try to call attotest() though, I get a segmentation violation. Debugging the function with mex -g, I find that hGetProcIDDLL is listed as 0x00 {unused=???} with "CXX0030: Error: expression cannot be evaluated" in the debugger. My guess is that LoadLibrary is somehow not being called/evaluated properly (EDIT: Upon further research using GetLastError(), I have found that the error generated when calling LoadLibrary() is 193 "note a valid win32 application"). When I compile a C++ program with the exact same lines of code, it loads the .dll fine and calls the function as expected.
(By the way, if I comment out the line "Int32 numDev =..." and initialize numDev independently to a constant, then the function runs fine in MATLAB. So the LoadLibrary and FreeLibrary functions are apparently being called in a way that doesn't crash anything, though they aren't really connecting with the .dll as I want them to.
EDIT: one more comment: PositionerInfo is a struct defined in hvpositionerv2.h).
Any suggestions on what is wrong, what I could do to fix this error, or what else I could try to call this .dll from within MATLAB?
thanks!
#include "mex.h"
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include "hvpositionerv2.h"
void attotest(double y[])
{
HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\hvpositionerv2.dll");
typedef int (_stdcall *PROCSTRUCTIN)(PositionerInfo**);
PROCSTRUCTIN funcPosCheck = (PROCSTRUCTIN)GetProcAddress(HMODULE (hGetProcIDDLL), "PositionerCheck");
PositionerInfo* pi = 0;
Int32 numDev = (funcPosCheck)(&pi);
FreeLibrary(hGetProcIDDLL);
y[0]=(double) numDev;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *x,*y;
mwSize mrows,ncols;
/* Check for proper number of arguments. */
if(nrhs!=0) {
mexErrMsgTxt("Zero input required.");
} else if(nlhs>1) {
mexErrMsgTxt("Too many output arguments.");
}
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
/* Assign pointers to each input and output. */
y = mxGetPr(plhs[0]);
attotest(y);
}

4 Comments

Just a thought: Should it be C:\hvpositionerv2.dll and not C:\\hvpositionerv2.dll? Not sure if you have a typo in your question - but maybe it's unable to find the library? Are you using the exact same path as you specified in your C++ program?
Hi, thanks for the response.
In my C++ program, I use "\\" for each "\" in the Windows path (the actual path is longer, but I shortened it for simplicity). The program does not work properly when single "\"s are used. I tried using the single "\" in my mex function any way, but it did not make a difference -- either way it seems unable to find the library. I have all of the files in MATLAB's current directory. I also tried using just "hvpositionerv2.dll" without the preceding path, but that did not work either.
When you use just "hvpositionerv2.dll", do you also ensure that the path to the library has been added to the system PATH variable?
Yes, right now I have everything in the MATLAB folder which is the default path that MATLAB opens on startup.

Sign in to comment.

 Accepted Answer

I just noticed that you the error you see is "not a valid win32 application - so it seems like MATLAB might be a 32-bit installation, and your DLL is 64-bit. Is that correct? If yes, you need 64-bit MATLAB to load a 64-bit library.

6 Comments

I am pretty sure that my MATLAB is 64-bit. I have 64-bit Windows and MATLAB runs fine on it. I just upgraded from R2010a to R2011a this week. I am not as sure about the DLL. It was supplied by the maker of a piece of electronics that I want to control with MATLAB. Is there some way to check this? I can ask the manufacturer. I don't have any problems calling the functions in the DLL from C++ programs compiled with Microsoft Visual C++ 2010 Express.
Visual C++ builds applications for a 32-bit target by default (unless you have specifically configured your project for a 64-bit platform). You could examine your DLL using dumpbin (http://support.microsoft.com/kb/177429) with the /headers option.
I should add to the original question that the "not a valid win32 application" error was error 193. I am not sure if it has a different interpretation on a 64 bit computer.
Thanks for the link. From dumpbin, I discovered the following header information:
14C machine (x86)
...
32 bit word machine
When I compile in the Visual Studio 2010 Command Prompt, this message appears:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86.
So it seems like the DLL is 32-bit and that the compiler recognizes this and uses a 32-bit version.
So far I have been keeping the DLL file in my default MATLAB folder (the active folder upon startup and in the MATLAB path). I tried using the mex -l option with the LIB file that I have. I discovered that I had to put this in the extern/lib folder of the MATLAB installation directory. Using this option didn't help anything though.
So is the only option to try to get the manufacturer to produce 64 bit versions of these DLLs? It is frustrating since I can compile the program on the same computer and use it to communicate with my device. It seems like there should be some way to wrap the working code into MATLAB.
Would installing 32-bit MATLAB on your 64-bit machine be acceptable (this is a valid configuration)? If yes, then compiling a 32-bit MEX should fix the issue.
I was actually just trying that! Using 32-bit MATLAB, I am able to load the DLL using the mex file given above. I can't think of any reason I need 64-bit MATLAB right now (perhaps it will run slower if I perform memory-intensive operations? That's not an issue right now).
I have asked the manufacturer if it can produce 64 bit DLL files. For now though, I will go with the 32-bit MATLAB when I need to use this piece of electronics. I feel like if I had a good knowledge of DLL files, C compilers, and MATLAB, I might be able to come up with some other workaround, but I have put enough time into getting this working for now.
Thanks for the help, Kaustubha!
Glad you got it working! And yes, the only downsides I can think of with using 32-bit MATLAB are the same as for running a 32-bit application on a 64-bit machine (like the one you mentioned about memory).

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!