loadlibrary problem with DLL file

When I try to call loadlibrary in MATLAB on a particular DLL file (using 64bit R2013a in Windows 7), I get a big list of errors like these:
error C2143:
syntax error : missing ')' before '*'
error C2143:
syntax error : missing ')' before '='
The errors refer to all lines in the DLL's header file that have or optional inputs like:
Int32 NCB_API PositionerCheck( PositionerInfo** posInfo );
or
Int32 NCB_API PositionerMoveAbsolute( Int32 deviceHandle, Int32 axisNo, Int32 position, Int32 rotCount = 0 );
I wrote a short C++ mex function that uses LoadLibrary() (the C++ function) to load the DLL and call its functions. My mex function compiles without problems with Microsoft Visual C++ 2012 and can successfully call the DLL functions.
The company that produced the DLL also supplies a 32bit version. With the 32bit version, I can compile and use my mex function , and I can also call loadlibrary in MATLAB and use the DLL that way. The header file for the 32bit version is identical to the 64bit one.
So far, I have only found one solution page ( http://www.mathworks.com/support/solutions/en/data/1-HNFE5H/index.html?solution=1-HNFE5H ) that seems relevant. I tried adding
#define __fastcall
to the header file, but the errors were unchanged.
Is there anything else I can try to get loadlibrary to work? The errors seem pretty generic, so I am not sure what to do to debug the problem. The DLL is supplied by a third party, so I do not have access to its contents to check if it contains an error, but since the mex function works with the DLL and the company's 32bit DLL works okay, it does not seem like the DLL is the problem.
SEBASTIAN: Your private message had no contact information, so I have no way to respond to you directly. The only changes I made to attocube's code was to modify hvpositionerinfov2.h as Friedrich suggested. For MATLAB to communicate with the ANC350, you also need to have the ANC350 driver installed, the .dll (and maybe .lib) file needs to be on the Windows path and on the MATLAB path. Then you can use loadlibrary('hvpositionerv2.dll','hvpositionerv2.h') to load the library and calllib('hvpositionerv2',...) to access the ANC350 API. If you need more help, send another private message with some form of contact information. I hope this helps. I can send you my notes and MATLAB functions if you want them.

 Réponse acceptée

Friedrich
Friedrich le 16 Mai 2013

1 vote

Hi,
loadlibrary is for native C only. So if your header file contains C++ statements it won't work. Make sure your header file is C compatible.
In the case it doesn't help, upload your header files and DLL's and post the link. Also state how you call loadlibrary in MATLAB.

4 commentaires

Will
Will le 17 Mai 2013
Modifié(e) : Will le 17 Mai 2013
Hi Friedrich, thanks for the reply. Before I modified it, the header file for the 64 bit DLL was the same as the one for the 32 bit DLL, so since loadlibrary worked for the 32 bit DLL I think that the C/C++ formatting must be okay (unless the requirements are different for 32 and 64 bit).
The DLL and header file are available at http://www.attocube.com/download/ANC350/ANC350v2_v1.4.50.zip. The DLL is in "DLL\Win64\lib\hvpositionerv2.dll" and the header is in "DLL\Win64\inc\hvpositionerv2.h". The only changes I have made to the header are to try adding
#include <windows.h>
#define __fastcall
to the beginning of the file (I get the same errors with and without these lines). loadlibrary worked with the 32 bit DLL without adding these lines to the header.
With the DLL and header on MATLAB's path, this command works for 32 bit:
loadlibrary('hvpositionerv2.dll','hvpositionerv2.h')
(EDIT: in case you try looking at the 32 bit file, I found that I had to add a couple other DLL's (msvcp71d.dll and msvcr71d.dll) to the path for it to work properly, but I could compile my mex function using the 64 bit DLL without any extra DLL files).
Friedrich
Friedrich le 21 Mai 2013
Modifié(e) : Friedrich le 21 Mai 2013
Hi,
the problem is that this
Int32 NCB_API PositionerMoveAbsolute( Int32 deviceHandle, Int32 axisNo, Int32 position, Int32 rotCount = 0 );
Is a C++ construct ("Default values in parameters") and MATLAB supports C only for loadlibrary. The difference between 32bit and 64bit ML is that the 64bit ML generates a thunk library so ML parses the header file differently. I guess that parse error is skipped in 32bit ML.
So removing the = 0 should resolve the issue as long the other header content is native C. In ML you simply pass down a 0 for rotCount and you are done.
Friedrich
Friedrich le 21 Mai 2013
Modifié(e) : Friedrich le 21 Mai 2013
Small update. Whoever did this header file had no knowledge about native C. The struct definition for PositionerInfo is not really correct in the way PositionerInfo is used later on. This will result in an invalid identifier PositionerInfo. Declare the struct like this:
typedef struct PositionerInfo{
int id;
bool locked;
}PositionerInfo;
Will
Will le 21 Mai 2013
Thanks, Friedrich! With those changes I was able to communicate with the device.

Connectez-vous pour commenter.

Plus de réponses (2)

Philip Borghesani
Philip Borghesani le 15 Mai 2013
Modifié(e) : Philip Borghesani le 15 Mai 2013

0 votes

Most likely there is #include statement missing from the library header file. "windows.h" is a common offender but it could be any header. You will need to modify the header you are using for loadlibary or create a new header that includes all needed headers and use a loadlibrary containing an addheader option:
loadlibrary(..., 'addheader','originalheader')
To find the headers you need look to any headers included before the libraries on in you mex file. To reproduce the compile error move the library header to first in the mex file.

3 commentaires

Will
Will le 15 Mai 2013
Thanks for the reply, Philip.
My mex cpp file includes the following: "mex.h", cstdio, cstdlib, and windows.h. Adding those to the header of my dll, gives me 100 errors related to cstdio and cstdlib (the list is cut off at 100). Could there be other header files that I need to include?
Philip Borghesani
Philip Borghesani le 15 Mai 2013
cstdio and cstdlib are c++ headers and not compatible with loadlibrary. To accuratly test for loadlibrary purposes a c mex file is needed. Try just including windows.h
windows.h will produce quite a few warnings from loadlibrary but these can usually be safely ignored.
If I add just
#include <windows.h>
to the beginning of my dll's header file, I get the same errors as without the include. I see lots of warnings as well, presumably the ones that you say can be safely ignored.
If I put that include statement in a separate file and use 'addheader' to add it as a second header file, I get the same errors.
If I put this second header file with just the windows.h line first and then put my original header as the 'addheader' file, I get "Warning: No functions found in library."

Connectez-vous pour commenter.

Wardo
Wardo le 20 Août 2016

0 votes

Hi Guys,
What do you suggest is the best way to control the Attocube ANC300 controller via MATLAB?
I have been searching online sometime now for some support on running the Attocube hardware from MATLAB and have found basically nothing.
So far I think my best solution is to build a LabView VI algorithm into a mex/dll file for MATLAB. You seem to have taken a different approach. Friedrich, above seems to have taken a different approach.
Sincerely, Ward

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by