How to create a header file from thunkfiles

5 views (last 30 days)
David
David on 25 Jan 2014
Commented: Image Analyst on 31 Jan 2014
I recently switched from a 32-bit to 64-bit machine and have had difficulty using the loadlibrary command to interface my matlab code with a near-infrared camera. I have no experience with C/C++, but I generated some thunkfiles in matlab from the dll file of the camera and do not know how to proceed from here. I was wondering if there was a way to generate an appropriate 64-bit header file from the dll or thunkfiles that I have now.

Answers (1)

Image Analyst
Image Analyst on 25 Jan 2014
Here's a script I used with a DLL from Measurement Computing Corporation. Feel free to modify file names, etc. for your purposes:
% Make a protype file for the cbw.h file.
% That .h file is needed to call loadlibrary() which loads the functions from cbw64.DLL into memory.
% However you can't use the h file in a stand alone, compiled program.
% You must create a prototype file and use that instead of the h file.
% You must have run "mex -setup" and "mcc -setup" to set up the compiler.
% Useful references:
% http://www.mathworks.com/help/matlab/matlab_external/create-alias-function-name-using-prototype-file.html
% http://www.mathworks.com/help/matlab/ref/loadlibrary.html#btjfvd3
% http://www.mathworks.com/matlabcentral/answers/32007
% http://www.mathworks.com/help/matlab/matlab_external/prototype-files.html
%------------------------------------------------------------------------------------------------------
clc; % Clear the command window.
% First specify the h file that will be used to create the prototype file.
hFileName = 'C:\Users\Public\Documents\Measurement Computing\DAQ\C\cbw64.h';
% See if it exists or not.
if exist(hFileName, 'file')
% Good - it exists!
fprintf('The .h file name = %s\n', hFileName);
else
% Bad - it is missing! Cannot continue.
errorMessage = sprintf('Error: The .h file name = %s does not exist!\nCannot create the prototype file.\n', hFileName);
warndlg(errorMessage);
return;
end
% Delete existing file, if there is an existing file.
prototypeFileName = fullfile(pwd, 'cbw_prototype_file.m');
if exist(prototypeFileName, 'file')
% Send them to the recycle bin instead of destroying them totally,
% just in case we need to recover an old one.
recycle('on');
% Delete the file (send to recycle bin).
delete(prototypeFileName);
fprintf('Deleted existing prototype file %s\n', prototypeFileName);
% Creating a prototype file also creates a bunch of intermediate files
% with "thunk" in their names. Delete these also, just to clean up totally.
thunkFiles = fullfile(pwd, '\cbw64_thunk*.*');
delete('D:\Matlab\work\yourProject\cbw64_thunk*.*')
fprintf('Deleted existing thunk files %s\n', thunkFiles);
end
% Note, you can't call loadlibrary (which creates the prototype file)
% if the library is already loaded - it won't do anything at all.
% We must check if the library is loaded, and if it is loaded, we must unload the library.
if libisloaded('cbw64')
% Library is currently loaded into memory.
fprintf('cbw64 library is unloaded. Unloading it so we can create a new prototype file . . .\n');
unloadlibrary('cbw64'); % Unload it.
if ~libisloaded('cbw64') % Double check that it is gone.
% Verify to user if the unloading worked.
fprintf('cbw64 library has been unloaded.\n');
else
% Should not get here.
fprintf('Error: cbw64 library could not be unloaded. Exiting without creating prototype file.\n');
end
end
% Now call load library with the 'mfilename' option to create the prototype file.
fprintf('Creating prototype file: %s . . .\nPlease wait . . .\n', prototypeFileName);
% Do the actual generation of the prototype file.
% IMPORTANT NOTE: It will create a cbw_prototype_file.m file in the current folder, not the folder containing the h file.
% The first argument must be the name of the library in the DLL file that uses the .h file,
% in this case it's called cbw64, which is in cbw64.dll, despite the fact that the h file is cbw (no "64" in the filename).
% Finally, now we actually create the prototype file (plus a bunch of thunk files that we don't need).
loadlibrary('cbw64', hFileName, 'mfilename', 'cbw_prototype_file')
successMessage = sprintf('Done creating prototype file %s\n', prototypeFileName);
fprintf('%s\n', successMessage);
% Play a small sound to alert the user.
PlaySoundFile('D:\WaveFiles\Effects', 'DingLing.wav');
% Pop up a message box notifying user of success.
helpdlg(successMessage);
% Note: this may generate warnings if there are functions in the
% h file that are not in the library (I think that's the reason):
% Warning: The function 'cbMemSetDTMode' was not found in the library
% Warning: The data type 'FcnPtr' used by function cbEnableEvent does not exist.
% You can ignore these warnings (unless you needed to call those specific functions). They are harmless.
% Now you should have cbw_prototype_file.m, and a bunch of intermediate files: cbw64_thunk*.*.
% To use the following code in your main program:
% if isdeployed
% % A deployed (compiled) application, must use a prototype file instead of a header file.
% % To create the prototype file you must use the loadlibrary mfilename option.
% % Use the prototype file in compiled code.
% % Create prototype file, mxproto.
% % hfile = fullfile(matlabroot,'extern','include','matrix.h');
% % loadlibrary('libmx',hfile,'mfilename','mxproto')
% % MATLAB creates the file, mxproto.m, in the current folder.
% loadlibrary('cbw64', @cbw_prototype_file)
% else
% % If running in the MATLAB development environment, you can use the .h file.
% loadlibrary(DLLFileName, hFileName);
% end
% Then when you compile your application, you have to add this prototype m-file with an -a option:
% mcc -m image_acquire.m -d 'D:\MATLAB\desired output folder' -a 'D:\Matlab\work\the source code folder\cbw_prototype_file.m'
  3 Comments
David
David on 31 Jan 2014
The above comment is mine-I was signed into a different user's account.
Image Analyst
Image Analyst on 31 Jan 2014
Since mikhail has the compiler license, maybe ask him to call the Mathworks. He didn't let his support agreement lapse, did he?

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!