Trouble connecting to dll file from MATLAB
15 次查看(过去 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
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?
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!