Author Topic: How to compile DLL in PE32?  (Read 2842 times)

Offline deepdish

  • Single posting newcomer
  • *
  • Posts: 3
How to compile DLL in PE32?
« on: August 16, 2022, 11:14:41 am »
Hello, Im a newbie in c++ and codeblocks, I managed to compile a dll but when I try to attach it to the application I get this error: "A PE32 can not import functions from PE64 and viceversa "... please help.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1549
Re: How to compile DLL in PE32?
« Reply #1 on: August 16, 2022, 11:42:58 am »
The message is clear, you have compiled the application and the dll using different number of bits. You must recompile one of them to match the other.

EDIT: regarding PE32, it depends on your compiler; use the same flags used for the application.
« Last Edit: August 16, 2022, 12:01:15 pm by Miguel Gimenez »

Offline deepdish

  • Single posting newcomer
  • *
  • Posts: 3
Re: How to compile DLL in PE32?
« Reply #2 on: August 17, 2022, 06:37:19 am »
 I need specific instruction to compile this dll in 32 bits, not a broad answer.

Offline Commaster

  • Almost regular
  • **
  • Posts: 171
Re: How to compile DLL in PE32?
« Reply #3 on: August 17, 2022, 10:48:37 am »
Looks like the compiler you are using for your library either does not support 32 bit mode or is missing all those 32 bit libraries your library wants...

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1549
Re: How to compile DLL in PE32?
« Reply #4 on: August 17, 2022, 11:52:40 am »
Quote
I need specific instruction to compile this dll in 32 bits, not a broad answer.

Then, why you did not specify your compiler or OS?. Broad questions receive broad answers...

Offline deepdish

  • Single posting newcomer
  • *
  • Posts: 3
Re: How to compile DLL in PE32?
« Reply #5 on: August 21, 2022, 06:10:05 pm »
Thanks for replies, I did manage to compile using 32bit minigw and choosing PE32 somewhere in options. Also attached the dll and it does its job renaming the window(which is what is was supposed to do) but not as good as expected, it does rename window but when I minimize the application for a blink of a second old name comes back and dll changes it back after(I need constant change) and also when I login via user the application changes the window name to username - windowname, and since usernames are different its impossible to catch every instance without using wildcards in this code to rename the window back, im sure there is a better solution out there for this task:

main.cpp
Code
#include "main.h"
#include "Winuser.h"
#include <process.h>


void TitleReplaceHookThread(void *param)
{
    DWORD pid = 0;
    DWORD current_pid = GetCurrentProcessId();
while(true)
{
Sleep(1);
HWND hWnd = FindWindow(NULL, "Original Application Name");
GetWindowThreadProcessId(hWnd,&pid);
if(hWnd&&current_pid==pid)
        {
        SetWindowText(hWnd, "My Custom Name");
        break;
        }
}
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
             HANDLE hThread;
            hThread = (HANDLE)_beginthread(TitleReplaceHookThread, 0, NULL);
            CloseHandle(hThread);
            break;
    }
    return TRUE;
}

main.h
Code
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

#endif

Also I found the static pointer which is responsible for renaming the window in the first place, the preferred choice would be to block that pointer via code, I have no idea how hard is it to write such code, I can pay for a code which can modify this.
« Last Edit: August 21, 2022, 06:28:20 pm by deepdish »