Recent

Author Topic: Run code during DLL load  (Read 283 times)

LemonParty

  • Hero Member
  • *****
  • Posts: 537
Run code during DLL load
« on: May 20, 2026, 04:48:28 pm »
1. Is it possible to run some code during load of DLL?
2. Can I somehow extend/rewrite functions of DLL during loading?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: Run code during DLL load
« Reply #1 on: May 20, 2026, 05:44:21 pm »
Hi
Do you mean execute code simultaneously with loading a dll in another thread?
If so, then Yes. I do that in my 'PluginMgr'.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 19279
  • Glad to be alive.
Re: Run code during DLL load
« Reply #2 on: May 21, 2026, 06:44:17 am »
If it is your own dll you can execute code in the initialization section.
Otherwise the dll needs to be fully loaded and then you can use trampolines to runtime patch any code.
"During loading" is not possible, because loading is not deterministic. What is possible is "during initialization" or "after loading".
- a dll can patch code during its initialization
or the other way around:
- you can only patch code in a dll after it is fully loaded. Because only then all addresses are resolved.
« Last Edit: May 21, 2026, 07:01:29 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

LemonParty

  • Hero Member
  • *****
  • Posts: 537
Re: Run code during DLL load
« Reply #3 on: May 21, 2026, 08:12:07 am »
Yes, I actually need to patch code when library fully loaded. How it can be done?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Khrys

  • Sr. Member
  • ****
  • Posts: 458
Re: Run code during DLL load
« Reply #4 on: May 21, 2026, 08:45:08 am »
Yes, I actually need to patch code when library fully loaded. How it can be done?

There was a discussion about this exact topic on this forum a few weeks ago

LemonParty

  • Hero Member
  • *****
  • Posts: 537
Re: Run code during DLL load
« Reply #5 on: May 21, 2026, 11:48:20 am »
That seems what I need.
I have one more question. Is it possible to generate code during DLL initialization, and then be able to run this code when DLL is loaded?
This is similar for what discussed in that topic but different.
For example during initialization of DLL I check CPUID instruction to find what processor capable of. Then I generate instructions for according CPU. Then I should somehow give the function addresses I just generated back to calling program. Is this possible?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018