Calling a Delphi generated DLL fails to load in Matlab

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.

 Risposta accettata

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.

2 Commenti

Thanks, I never realized that the Matlab version I'm using is a 64 bit application... Strange error by the way. As the dll is a correct 32 bit application I would have anticipated a message stating that the dll is not a valid 64 bit application to run in Matlab?
Installing the 32 bit Matlab version worked!
Oscar: I agree that "is not a valid Win32 application" is very non-intuitive - unfortunately, this is an error from the OS AFAIK. If you perform a web search for that phrase, you'll see that it's not limited to MATLAB, but is a Windows thing.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by