Trouble connecting to dll file from MATLAB

23 次查看(过去 30 天)
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 个评论
Kaustubha Govind
Kaustubha Govind 2011-4-30
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 2011-4-30
Yes, right now I have everything in the MATLAB folder which is the default path that MATLAB opens on startup.

请先登录,再进行评论。

采纳的回答

Kaustubha Govind
Kaustubha Govind 2011-4-30
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 个评论
Will
Will 2011-5-2
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 2011-5-2
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).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 C Shared Library Integration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by