I have been experimenting with DLL files and have borrowed some code from these forums to produce a DLL file and a program to test it:
DLL file:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
BOOL __stdcall DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int __declspec( dllexport ) __stdcall SampleFunction(int a, int b)
{
MessageBox(0, "SampleFunction", "TestDLL", MB_OK);
return a * b;
}
Test file:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DYNDLL
int WINAPI SampleFunction(int, int);
#ifdef DYNDLL
typedef int (WINAPI SAMPLEFUNCTION)(int a, int b);
typedef SAMPLEFUNCTION *LSAMPLEFUNCTION;
#else
#pragma comment(lib, "DLLTest32.lib")
#endif
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
#ifdef DYNDLL
HANDLE hLib;
static LSAMPLEFUNCTION SampleFunction;
hLib = LoadLibrary("DLLTest32.DLL");
if (hLib){
MessageBox(0, "Library has loaded", "SUCCESS", MB_OK | MB_ICONINFORMATION);
SampleFunction = (LSAMPLEFUNCTION)GetProcAddress(hLib, "SampleFunction");
#endif
SampleFunction(0, 0);
#ifdef DYNDLL
FreeLibrary(hLib);
}
else{
MessageBox(0, "Library has NOT loaded", "FAILED", MB_OK | MB_ICONINFORMATION);
}
#endif
return 0;
}
When I build these files in the Pelles C IDE by selecting Win64 Dynamic Library (DLL) and Win64 Program (EXE) to build these files then the program runs and displays the message box in the DLL file as expected.
However, when I build these files in the Pelles C IDE by selecting Win32 Dynamic Library (DLL) and Win32 Program (EXE) to build these files then the program runs and displays a message to say that the library has been loaded but the message box in the DLL file is not shown.
How should I build a 32 bit version of these files that run correctly? I am using Windows 11 on a 64 bit PC.