MATLAB: How to import overloaded functions from a shared library into MATLAB

dllgenericMATLABoverloadoverloadedoverloading

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.

Best Answer

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.
Related Question