MATLAB: Calling a Delphi generated DLL fails to load in Matlab

delphi dllloadlibraryMATLAB

I have a dll created in Delphi XE2 which I would like to use in Matlab, but fails to load with loadlibrary. To simplify the problem a very basic dll has been created to test whether it works at all. When loading the simple library the dll cannot be loaded because it is not a valid Win32 application, although it is compiled for Win32 in Delphi. What is causing this?
>> loadlibrary('C:\Users\Me\Documents\Matlab\TestAPI.dll','C:\Users\Me\Documents\Matlab\TestAPI.h','alias','testapi')
Error using loadlibrary (line 421)
There was an error loading the library
"C:\Users\Me\Documents\MATLAB\TestAPI.dll"
C:\Users\Me\Documents\MATLAB\TestAPI.dll is not a valid Win32 application.
Caused by:
Error using loaddefinedlibrary
C:\Users\Me\Documents\MATLAB\TestAPI.dll is not a valid Win32
application.
The header file is:
#ifndef TESTAPI_DLL_H
#define TESTAPI_DLL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_TESTAPI_DLL
#define TESTAPI_DLL __declspec(dllexport)
#else
#define TESTAPI_DLL __declspec(dllimport)
#endif
// ----------------------------------------------------------------------------
// Test procedure; shows messagebox
// ----------------------------------------------------------------------------
bool __stdcall DllMessage;
#ifdef __cplusplus
}
#endif
#endif // !defined(TESTAPI_DLL_H)
The TestAPI:
library TestAPI;
uses
SysUtils,
Classes,
Dialogs;
{$R *.res}
function DllMessage: Boolean stdcall; export;
begin
ShowMessage('Hello world from a Delphi DLL') ;
Result:=true;
end;
exports
DllMessage;
begin
end.
I'm using Delphi XE2 on windows 7 64 with Matlab R2011b.

Best Answer

It seems like your DLL might be 32-bit, which cannot be loaded on 64-bit MATLAB. Please see this previous discussion which was about the same issue.
Related Question