NO

Author Topic: Why does this DLL compile for Win64 but not for Win32  (Read 1111 times)

Offline CProgrammer

  • Member
  • *
  • Posts: 8
Why does this DLL compile for Win64 but not for Win32
« on: July 05, 2023, 06:04:49 PM »
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:
Code: [Select]
#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:
Code: [Select]
#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.
« Last Edit: July 05, 2023, 06:08:45 PM by CProgrammer »

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 176
    • Bcx Basic to C/C++ Translator
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #1 on: July 05, 2023, 07:20:05 PM »
I do 100% of my compiling with batch files.

Below is my batch file that uses Pelles C to produce a 64-bit DLL. 

Using your unmodified code and my batch file produced a 64-bit DLL using Pelles C v 12.0

Windows 10 Pro 64-bit


Code: [Select]

IF NOT EXIST %1.c GOTO Usage

set  PellesCDir=C:\PellesC

echo Setting 64-bit environment for Pelles C...
set  PATH=%PellesCDir%\Bin;%PATH%
set  INCLUDE=%PellesCDir%\Include;%PellesCDir%\Include\Win;%INCLUDE%
set  LIB=%PellesCDir%\Lib;%PellesCDir%\Lib\Win64;%LIB%

SET  PoccOPTS=/Go /Gn /W1 /Gd /Ze /Zx /Tamd64-coff /D_WIN32_WINNT=0x501  /std:c17
SET  PolinkOPTS= /dll /machine:X64 /subsystem:windows,5.01  /STACK:10485760               
SET  PolinkLIBS=kernel32.lib advapi32.lib delayimp.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib ole32.lib oleaut32.lib


ECHO Pelles C is compiling %1
call c:\pellesc\bin\pocc   %PoccOPTS%    %1.c
call c:\pellesc\bin\polink %PolinkLIBS%  %PolinkOPTS%  %1.obj %2 %3 %4 %5 %6 %7 %8 %9
ECHO Completed ...
GOTO done


:Usage
ECHO **************************************************************
ECHO  For building 64-bit DLL files using Pelles C Compiler
ECHO  Usage:   PD64   "C" FileName  [Optional Linker Files: 2-9]
ECHO  Example: PD64    MyGuiApp      MyIcons.res   MyManifest.res
ECHO **************************************************************
:done

Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline Robert

  • Member
  • *
  • Posts: 245
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #2 on: July 06, 2023, 01:19:45 AM »
Hi CProgrammer:

Only you, God, and maybe TimoVJL know what's in your .ppj file.

The rest of us have to guess.

There are an enormous number of possibilities.

Please zip up your entire non-functional 32 bit project and attach it to your post so we can see what is what.

Offline CProgrammer

  • Member
  • *
  • Posts: 8
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #3 on: July 06, 2023, 10:12:24 AM »
@MrBcx: Thank you for your batch file. At present I am trying to relearn C and I prefer to compile programs in the IDE as this (to me) is easier than using the command line instructions, although when I get up to speed I will find your batch file useful. The IDE will produce a working 64-bit DLL but not a working 32-bit DLL

@Robert: I did not realise that you needed to see the project file as well as the two pieces of code in the first post of this thread. I am attaching the zipped files here.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #4 on: July 06, 2023, 04:39:22 PM »
The functions decorations change between 32 and 64bits units, because the ABI (Application Binary Interface) changes.
The function:
Code: [Select]
int __declspec( dllexport ) __stdcall SampleFunction(int a, int b)is exported as _SampleFunction@8 for 32bits compilations and as SampleFunction for 64bits units.
You may impose the export name aliasing it by means of a module definition file, but this is far more complex compilation.
For now see the attached project, that is yours basically, rearranged in directories and grouped in a single workspace for easier usage.
The exe file is defined as "helper exe" in the DLL project, so you can simply open the workspace, set the DLL as active project and run the DLL. The IDE will automagically invoke the exe file.
Changing both the projects from 32 to 64bits and reverse and rebuilding the workspace will set everything for you (in the workspace the exe is defined as dependent from the DLL, so the last will be compiled first).
« Last Edit: July 07, 2023, 12:56:19 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #5 on: July 06, 2023, 08:21:00 PM »
Hi frankie,

About the export name aliasing method, could you present a small example? Thanks.
Code it... That's all...

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #6 on: July 06, 2023, 09:18:43 PM »
Hi frankie,

About the export name aliasing method, could you present a small example? Thanks.
Hello Vortex. You're welcome.
I attached a sample based on the previous example modified through the use of a module definition file.
In this sample the standard decorated symbol is aliased to the undecorated symbol in the definition file at the line:
Code: [Select]
SampleFunction = _SampleFunction@8 ;AliasingNow you can obtain the procedure address using the undecorated name same as for the 64bits version:
Code: [Select]
SampleFunction = (LSAMPLEFUNCTION)GetProcAddress(hLib, "SampleFunction"); //Get address of SampleFunction
You can change the project types to 64bits mode, exclude the module definition file from DLL project, and recompile.
As you can see it works invoking the function with the same undecorated name.
You must remove the module definition file because the line of aliasing will fail in 64bits mode due to the missing decorated symbol.
« Last Edit: July 06, 2023, 09:33:41 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #7 on: July 06, 2023, 10:22:49 PM »
Just a note:
Code: [Select]
SET  PolinkOPTS= /dll /machine:X64 /subsystem:windows,5.01  /STACK:10485760 should it be /machine:X64 /subsystem:windows,5.02 ?
May the source be with you

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 176
    • Bcx Basic to C/C++ Translator
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #8 on: July 06, 2023, 10:39:57 PM »
Just a note:
Code: [Select]
SET  PolinkOPTS= /dll /machine:X64 /subsystem:windows,5.01  /STACK:10485760 should it be /machine:X64 /subsystem:windows,5.02 ?

Correct.

5.01 (x86)
5.02 (x64)
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline CProgrammer

  • Member
  • *
  • Posts: 8
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #9 on: July 07, 2023, 11:16:11 AM »
@frankie: Thanks for the zip file. I've downloaded it and will look at it later.

Offline CProgrammer

  • Member
  • *
  • Posts: 8
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #10 on: July 07, 2023, 12:19:03 PM »
@frankie: I've just tested the file on Windows 11 and it works well. The solution you provided was very simple and it will make it easy for me to compile DLL files for both 32 bit and 64 bit versions.

You have also provided me with my first introduction to workspaces. Thanks.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #11 on: July 07, 2023, 12:54:11 PM »
@frankie: I've just tested the file on Windows 11 and it works well. The solution you provided was very simple and it will make it easy for me to compile DLL files for both 32 bit and 64 bit versions.

You have also provided me with my first introduction to workspaces. Thanks.

You're welcome  ;)
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline John Z

  • Member
  • *
  • Posts: 796
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #12 on: July 08, 2023, 02:45:03 AM »
Thanks frankie,

I grabbed it too, I've not used the workspace feature in all these years, so about time for me to learn.

John Z

Offline Robert

  • Member
  • *
  • Posts: 245
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #13 on: July 08, 2023, 04:22:44 AM »
I do 100% of my compiling with batch files.

Below is my batch file that uses Pelles C to produce a 64-bit DLL. 

Using your unmodified code and my batch file produced a 64-bit DLL using Pelles C v 12.0

Windows 10 Pro 64-bit


Code: [Select]

IF NOT EXIST %1.c GOTO Usage

set  PellesCDir=C:\PellesC

echo Setting 64-bit environment for Pelles C...
set  PATH=%PellesCDir%\Bin;%PATH%
set  INCLUDE=%PellesCDir%\Include;%PellesCDir%\Include\Win;%INCLUDE%
set  LIB=%PellesCDir%\Lib;%PellesCDir%\Lib\Win64;%LIB%

SET  PoccOPTS=/Go /Gn /W1 /Gd /Ze /Zx /Tamd64-coff /D_WIN32_WINNT=0x501  /std:c17
SET  PolinkOPTS= /dll /machine:X64 /subsystem:windows,5.01  /STACK:10485760               
SET  PolinkLIBS=kernel32.lib advapi32.lib delayimp.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib ole32.lib oleaut32.lib


ECHO Pelles C is compiling %1
call c:\pellesc\bin\pocc   %PoccOPTS%    %1.c
call c:\pellesc\bin\polink %PolinkLIBS%  %PolinkOPTS%  %1.obj %2 %3 %4 %5 %6 %7 %8 %9
ECHO Completed ...
GOTO done


:Usage
ECHO **************************************************************
ECHO  For building 64-bit DLL files using Pelles C Compiler
ECHO  Usage:   PD64   "C" FileName  [Optional Linker Files: 2-9]
ECHO  Example: PD64    MyGuiApp      MyIcons.res   MyManifest.res
ECHO **************************************************************
:done


Hi MrBCX:

For a 64 bit build, in the SET PolinkLIBS= list

delayimp.lib

should be

delayimp64.lib

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 176
    • Bcx Basic to C/C++ Translator
Re: Why does this DLL compile for Win64 but not for Win32
« Reply #14 on: July 08, 2023, 03:24:26 PM »

Hi MrBCX:

For a 64 bit build, in the SET PolinkLIBS= list

delayimp.lib

should be

delayimp64.lib

Thanks Robert.

Copy&Paste is one bane of my coding experience.
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com