MATLAB: Using LOADLIBRARY to load ‘visa32.dll’ and ‘visa.h’ in 64-bit Matlab R2017b

loadlibraryMATLAB

With Matlab R2015b on 32-bit Windows 7, LOADLIBRARY was working fine:
library = 'C:\Windows\System32\visa32.dll';
headerFile = 'C:\Program Files\IVI Foundation\VISA\WinNT\include\visa_Matlab.h';
loadlibrary(library, headerFile, 'alias','visa32');
and I can use functions in 'visa32.dll', such as,
defaultRm = 0;
[err, defaultRm] = calllib('visa32', 'viOpenDefaultRM', defaultRm);
vi = 0;
InstrAddr = 'GPIB0::29::INSTR';
pInstrAddr = libpointer('int8Ptr', [int8( InstrAddr ) 0] );
[err, tempVar, vi] = calllib('visa32', 'viOpen', defaultRm, pInstrAddr, 0, 0, vi);
to open instrument's communication port…
But after I have upgraded to 64-bit Windows 7 and Matlab R2017b, LOADLIBRARY won't work any more, even I have use 62-bit versions of 'visa32.dll' and 'visa.h'. There are two problems:
(1) Firstly, Matlab complaints about not have the right compiler installed and can't even run LOADLIBRAY. Follow their online instruction to install the MinGW-w64 Compiler addon. This problem seems to have revolved.
(2) LOADLIBRAY seems to be able to run, but then it has the following error:
Failed to parse type '( fastcall )) viOpenDefaultRM ( ViPSession vi' original input '( fastcall )) viOpenDefaultRM ( ViPSession vi ' Found on line 217 of input from line 93 of file C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Include\visa.h Error parsing argument for function attribute function may be invalid. …
I wonder if somebody could help please. Many thanks in advance!

Best Answer

It looks like this error could be related to one of the following two issues:
1) In 64-bit Windows, the VISA driver header files contain the "__fastcall" keyword in function signatures. "loadlibrary" is not able to deal with it and hence throws an error. This can be resolved by adding the following line at the top of the header file
#define __fastcall
You can refer to this MATLAB Answers post for more details:
2) Another possible cause of the error could be that the function definitions in the header file "visa.h" are written with ellipsis(“…”) to define variable number of inputs, such as:
ViStatus viPrintf ( ViSession vi , ViString writeFmt , ...);
This is misinterpreted by "loadlibrary" and hence it gives an error. A workaround for this is to explicitly define the number and type of arguments for the functions, by editing the prototype file. Details on how this is done can be found in the following MATLAB Answers post:
Hope this helps you resolve the issue.