Using LOADLIBRARY to load 'visa32.dll' and 'visa.h' in 64-bit Matlab R2017b

38 views (last 30 days)
jy on 17 Jul 2018
Edited: Walter Roberson on 12 Sep 2018
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!

Accepted Answer

Siddharth Bhutiya
Siddharth Bhutiya on 19 Jul 2018
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.

More Answers (3)

jy on 30 Jul 2018
The advantages of using VISA library in MATLAB for instrument control are: direct (i.e. faster) access to ports, be able to use GTL (go-to-local) and handles communication error better, etc. Use LOADLIBRARY to load the VISA library and CALLLIB to access the functions in the loaded library, e.g.
[err, defaultRm] = calllib('visa32', 'viOpenDefaultRM', defaultRm);
However, limitations in LOADLIBRARY have caused a lot of trouble, and it works differently for different Windows architecture. Here is the solution that has been tested on 32- and 64-bit Windows:
  1. C++ compiler in MATLAB:
    a. This only affects 64-bit MATLAB;
    b. A MATLAB add-on called ‘MinGW-w64 Compiler’ must be installed:
      i. this is relatively easy for R2017b, just use MATLAB’s Add-Ons Manager;
      ii. for MATLAB version prior to R2017b, it is very difficult:
       Download and install 'MinGW-w64' from:
       Then configure MATLAB as described in:
  2. VISA library file:
    a. Different DLL file for different Windows architecture;
    b. The library file is available after the installation of NI-488.2 driver (the latest version to date is 'NI4882_1760f0.exe';
    c. For 32-bit Windows and 32-bit MATLAB:
      i. Library file: ‘visa32.dll’
      ii. Location: ‘C:\Windows\System32\’
    d. For 64-bit Windows and 64-bit MATLAB:
      i. Library file: ‘visa64.dll’
      ii. Location: ‘C:\Windows\System32\’
      iii. Notes:
       1. According to NI’s website, the 64-bit VISA DLL is ‘visa32.dll’ that resides in 'C:\Windows\SysWOW64\. This is not right;
       2. Alternatively, a 64-bit version of ‘visa32.dll’, which is smaller than ‘visa64.dll’ installed by NI-488.2 driver, can be downloaded from: .
  3. Header files:
    a. Two headers files are needed to load the VISA library: ‘visa.h’ and ‘visatype.h’;
    b. Both are available after the installation of NI-488.2 driver;
    c. For 32-bit Windows, both files are in: ‘C:\Program Files\IVI Foundation\VISA\Win64\include’
    d. For 64-bit Windows, both files are in: ‘C:\Program Files (x86)\IVI Foundation\VISA\WinNT\include’
    e. ‘visatype.h’ can be used as it is;
    f. ‘visa.h’ must be modified for MATLAB’s LOADLIBRARY. Changes made on ‘visa.h’ are:
      i. Add the following at the beginning of the header file:
#ifdef _WIN64
#define __fastcall
      ii. Mask out 'viInstallHandler' and 'viUninstallHandler', as they are not commonly used;
      iii. Remove ', ...' from the following functions as it’s not supported by LOADLIBRARY (
        viPrintf, viSPrintf, viScanf, viSScanf, viQueryf
      iv. Replace 'ViVAList' with 'ViConstBuf' in the following functions:
        viVPrintf, viVSPrintf, viVScanf, viVSScanf, viVQueryf
      v. Replace the 'viVxiServantResponse' function with the following:
#ifndef _WIN64
ViStatus _VI_FUNC viVxiServantResponse(ViSession vi, ViInt16 mode, ViUInt32 resp);
      g. Here is the modified ‘visa.h’, renamed as ‘visa_Matlab.h’:
% ‘visa32.dl’ or ‘visa64.dll’, ‘visa_Matlab.h’ and ‘visatype.h’ must be
% available in the local directory
archstr = computer('arch');
if ~isempty(strfind(archstr, 'PCWIN64')) || ~isempty(strfind(archstr, 'win64'))
dllName = 'visa64';
dllName = 'visa32';
headerFileName = 'visa_Matlab.h';
loadlibrary ([dllName '.dll'], 'visa_Matlab.h','alias',dllName);
visaFunctions = libfunctions(dllName,'-full');

grajcjan on 10 Sep 2018
Hi,I have used your file, but I still have a problem with fastcall
loadlibrary('C:\Windows\System32\visa64.dll', 'C:\Program Files\IVI Foundation\VISA\Win64\Include\visa_Matlab.h', 'alias','visa64')
Warning: Message from C preprocessor:
C:\Program Files\IVI Foundation\VISA\Win64\Include\visa_Matlab.h:102:0: warning:
"__fastcall" redefined
#define __fastcall
<built-in>: note: this is the location of the previous definition
> In loadlibrary

Sign in to comment.

grajcjan on 11 Sep 2018
Could you share some example of GPIB communication?
  1 Comment
jy on 12 Sep 2018
The attached zip file contains three .m files:
  1. 'visa_instr.m' - the main functions for instrument control, include write and read functions, etc.;
  2. 'visa_instr_help.m' - examples and instructions on how to use 'visa_instr.m';
  3. 'loadVisaDll.m' - to load DLLs automatically.
This is the first working version, done in few days, so it's far from being perfect. I am working on the improvement already, and will post them once I have finish, e.g.
  • You don't need to open a 'defaultRM' session every time you open a port;
  • Error handling can be improved;
  • the 'loadVisaDll.m' hasn't include two essential DLLs: 'visautilities.dll' and 'visaconfigMgr.dll', which means you still need to install the NI-488.2 driver (my aim was to be able to run the main programme without any driver installed, so I have embedded 'visa32.dll', 'visa64.dll', 'visa_Matlab.h' and 'visatype.h' in 'loadVisaDll.m').
Please share if you've found any bug or if you've any idea on how to improve it.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!