How do I import overloaded functions from a shared library into MATLAB?
5 views (last 30 days)
Show older comments
I am trying to load ReadFile from kernel32 using the loadlibrary function. I created a header file:
#ifndef _MATLAB_READFILE_H_
#define _MATLAB_READFILE_H_
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C"
{
#endif
DLL_EXPORT BOOL _stdcall ReadFile(DWORD hFile, DWORD * data, DWORD
numbytesToRead, DWORD * pnumbytesRead, DWORD overlapped);
DLL_EXPORT BOOL _stdcall ReadFile(DWORD hFile, double * data, DWORD
numbytesToRead, DWORD * pnumbytesRead, DWORD overlapped);
DLL_EXPORT BOOL _stdcall ReadFile(DWORD hFile, char * data, DWORD
numbytesToRead, DWORD * pnumbytesRead, DWORD overlapped);
#ifdef __cplusplus
}
#endif
#endif
So, that I can easily read different file types without converting data types. However the LoadLibrary function only loads the last ReadFile declaration.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
Function overloading is a C++ feature and the current implementation of the general DLL loading capability in MATLAB does not support C++ specific features.
One way you can work around this problem is to create multiple versions of your header file and load the kernel32 library multiple times with different aliases. For instance,
loadlibrary('kernel32.dll','testkernel1.h','alias','kernel1')
loadlibrary('kernel32.dll','testkernel2.h','alias','kernel2')
loadlibrary('kernel32.dll','testkernel3.h','alias','kernel3')
where each of the header file contains the different signatures for the ReadFile function.
You can ignore the warning messages about some previously existing enumeration types.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!