Trouble connecting to dll file from MATLAB

23 visualizzazioni (ultimi 30 giorni)
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 Commenti
Kaustubha Govind
Kaustubha Govind il 30 Apr 2011
When you use just "hvpositionerv2.dll", do you also ensure that the path to the library has been added to the system PATH variable?
Will
Will il 30 Apr 2011
Yes, right now I have everything in the MATLAB folder which is the default path that MATLAB opens on startup.

Accedi per commentare.

Risposta accettata

Kaustubha Govind
Kaustubha Govind il 30 Apr 2011
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 Commenti
Will
Will il 2 Mag 2011
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!
Kaustubha Govind
Kaustubha Govind il 2 Mag 2011
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).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su C Shared Library Integration in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by