Recent

Author Topic: What's the meaning of dllparam in TDLL_Entry_Hook ?  (Read 5412 times)

440bx

  • Hero Member
  • *****
  • Posts: 3946
What's the meaning of dllparam in TDLL_Entry_Hook ?
« on: February 16, 2019, 12:13:36 pm »
Hello,

To receive dll process/thread attach/detach, Borland used a procedure variable DllProc.

In Delphi 2 code, I use
Code: Pascal  [Select][+][-]
  1. DllProc := @DllMain;

in FPC, I changed that to:

Code: Pascal  [Select][+][-]
  1. Dll_Process_Detach_Hook := @DllMain;
  2. Dll_Thread_Attach_Hook  := @DllMain;    
  3. Dll_Thread_Detach_Hook  := @DllMain;

thinking that the parameter passed to DllMain is the reason for the call seems logical (thread/process attach/detach) but, that doesn't seem to be the case (at least not for thread attach/detach.)

my question is: what does the parameter FPC passes to the hook function mean ?

Thank you for your help.

PS: FPC v3.0.4
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #1 on: February 16, 2019, 02:49:30 pm »
thinking that the parameter passed to DllMain is the reason for the call seems logical (thread/process attach/detach) but, that doesn't seem to be the case (at least not for thread attach/detach.)
my question is: what does the parameter FPC passes to the hook function mean ?
As far as I can see from the sources, there are two global variables DLLreason and DLLparam (of course only on Windows platforms, because in other no dll). And DLLparam passed as paramemer.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #2 on: February 16, 2019, 02:58:31 pm »
BTW it is the same has in later Delphi's (like delphi 3!)
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #3 on: February 16, 2019, 03:58:04 pm »
As far as I can see from the sources, there are two global variables DLLreason and DLLparam (of course only on Windows platforms, because in other no dll). And DLLparam passed as paramemer.
Looks like we looked at the same code... this is what I found
Code: Pascal  [Select][+][-]
  1. 90 { Win32 Info }
  2. 91   startupinfo : tstartupinfo deprecated;  // Delphi does not have one in interface
  3. 92   MainInstance,
  4. 93   cmdshow     : longint;
  5. 94   DLLreason : dword; public name 'operatingsystem_dllreason';
  6. 95   DLLparam : PtrInt; public name 'operatingsystem_dllparam';
  7. 96   StartupConsoleMode : DWORD;
  8. 97 const
  9. 98   hprevinst: longint=0;
  10. 99
  11.  
  12. 100 type
  13. 101   TDLL_Entry_Hook = procedure (dllparam : PtrInt);
  14. 102
  15.  
  16. 103 const
  17. 104   Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  18. 105   Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  19. 106   Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  20.  
It's hard to make sense out of that.  Windows passes the instance, the reason and a reserved parameter to the dll.  Obviously the reserved parameter is of little to no use, the values received in dllparam make it clear it's not the dll instance/module, that only leaves the reason as a possible - and useful - parameter value but, that doesn't seem to be it either.  It looks like one of those parameters that should have been named "this_parameter_intentionally_left_useless".  ;)

I wonder why the FPC developers deviated from Delphi in this case.  I'm still curious as to what that parameter brings to the table. 

For DLLs that need the DllReason, it looks like it is simply not possible to port Delphi code to FPC without making structural changes in the code.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #4 on: February 16, 2019, 04:02:29 pm »
No. Delphi 3 and higher behave the same as FreePascal. It is just D2 that was not yet compatible with MS API's in the proper way,
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #5 on: February 16, 2019, 04:34:21 pm »
No. Delphi 3 and higher behave the same as FreePascal. It is just D2 that was not yet compatible with MS API's in the proper way,
Your signature is quite accurate, you really can't help it.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #6 on: February 16, 2019, 05:01:37 pm »
No, that's true. But it is correct.... Delphi2 is the first 32 bit version and did not mature, just like you... I would not rely on comparing D2 "features" to FPC. Start comparing from something like D7.
Btw sometimes I still use D2 because it renders smaller executables at the cost of non-fixed compiler bugs.
« Last Edit: February 16, 2019, 05:10:40 pm by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #7 on: February 16, 2019, 05:07:21 pm »
No, that's true. But it is correct.... Delphi2 is the first 32 bit version and did not mature, just like you... I would not rely on comparing D2 "features" to FPC. Start comparing from something like D7
At least you're funny.... that's a plus.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #8 on: February 18, 2019, 09:19:08 am »
The Windows entry point is declared (among others) in rtl\win32\sysinitpas.pp:
Code: Pascal  [Select][+][-]
  1.     procedure _FPC_DLLMainCRTStartup(_hinstance : longint;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLMainCRTStartup';
  2.     begin
  3.       IsConsole:=true;
  4.       sysinstance:=_hinstance;
  5.       dllreason:=_dllreason;
  6.       dllparam:=PtrInt(_dllparam);
  7.       SetupEntryInformation;
  8.       DLL_Entry(SysInitEntryInformation);
  9.     end;
  10.  
  11.  
  12.     procedure _FPC_DLLWinMainCRTStartup(_hinstance : longint;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLWinMainCRTStartup';
  13.     begin
  14.       IsConsole:=false;
  15.       sysinstance:=_hinstance;
  16.       dllreason:=_dllreason;
  17.       dllparam:=PtrInt(_dllparam);
  18.       SetupEntryInformation;
  19.       DLL_Entry(SysInitEntryInformation);
  20.     end;
So you can see that the global DllParam variable is set to the third parameter of the entry point. Now you can look at MSDN for DllMain where the third parameter is called lpvReserved and indeed has a documentation when it has what kind of value.

And as Thaddy said: please don't use Delphi 2 as a point of comparison with FPC for such things.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #9 on: February 18, 2019, 10:43:57 am »
Now you can look at MSDN for DllMain where the third parameter is called lpvReserved and indeed has a documentation when it has what kind of value.
That doesn't seem to make sense, the lpvReserved is, as its name says, reserved and MS has never documented what the values of that parameter mean.  I don't see getting that parameter value as being very useful.

And as Thaddy said: please don't use Delphi 2 as a point of comparison with FPC for such things.
Excuse me for stating this but, I really don't put much stock, if any at all, in what Thaddy says. What he stated in this thread
No. Delphi 3 and higher behave the same as FreePascal. It is just D2 that was not yet compatible with MS API's in the proper way,
Is a good reason.

As far as comparing with Delphi 2, TTBOMK, much later versions of Delphi still use DllProc, therefore it seems that, in this particular case,  comparing to Delphi 2 or even a much later version of Delphi makes no difference.

Again, TTBOMK, no version of Delphi uses the mechanism (Dll_Process_Detach_Hook, Dll_Thread_Attach_Hook, Dll_Thread_Detach_Hook) implemented by FPC.  Because of that, Dll source code requires more than just trivial changes to make it acceptable to Delphi (regardless of version) and FPC.

All that said, thank you for making it clear what the meaning of the parameter to the hook procedures mean. I appreciate your clarifying that.

« Last Edit: February 18, 2019, 10:48:18 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #10 on: February 19, 2019, 09:36:59 am »
Now you can look at MSDN for DllMain where the third parameter is called lpvReserved and indeed has a documentation when it has what kind of value.
That doesn't seem to make sense, the lpvReserved is, as its name says, reserved and MS has never documented what the values of that parameter mean.  I don't see getting that parameter value as being very useful.
To quote the documentation:
Quote from: MSDN
pvReserved [in]

    If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads.

    If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if FreeLibrary has been called or the DLL load failed and non-NULL if the process is terminating.
It explicitely states when lpvReserved will be NULL and when not.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: What's the meaning of dllparam in TDLL_Entry_Hook ?
« Reply #11 on: February 19, 2019, 09:47:15 am »
To quote the documentation:
Quote from: MSDN
pvReserved [in]

    If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads.

    If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if FreeLibrary has been called or the DLL load failed and non-NULL if the process is terminating.
It explicitely states when lpvReserved will be NULL and when not.
I stand corrected and learned something.   

The last time I read the documentation about DllMain was likely over 20 years ago and I could swear MS didn't publish anything about that parameter back then.   Thank you for updating my knowledge and, I can see that parameter being occasionally useful.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018