Recent

Author Topic: using global counters in a dll .. can it be done ?  (Read 5147 times)

RickD

  • New Member
  • *
  • Posts: 14
using global counters in a dll .. can it be done ?
« on: January 23, 2017, 09:43:06 am »
Hi all,

At this moment i use a dll to hook into a windows system function (as far as i know that is the only way to do this in delphi). The problem is that i want to store some global counter information about this dll and reuse it each time it is active.

Right now i store it into a file which i open and close each time the dll is used. This does work, but is not a great solution for obvious reasons.

Does anybody know if there is a better way to deal with this by sharing a part of the host.exe memory or some other trick ..

Any help on this is much appreciated.

Regards,
Rick


balazsszekely

  • Guest
Re: using global counters in a dll .. can it be done ?
« Reply #1 on: January 23, 2017, 10:21:16 am »
@RickD
Quote
At this moment i use a dll to hook into a windows system function (as far as i know that is the only way to do this in delphi).
In what system function are you trying to hook into?

Quote
Does anybody know if there is a better way to deal with this by sharing a part of the host.exe memory or some other trick ..
Yes, memory mapped files.

« Last Edit: January 23, 2017, 11:29:50 am by GetMem »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: using global counters in a dll .. can it be done ?
« Reply #2 on: January 23, 2017, 11:36:51 am »
something like:
Code: Pascal  [Select][+][-]
  1. library testme;
  2. uses windows;
  3. var
  4.   UseCounter:integer = 0;export;
  5.  
  6.   procedure DLLMain(dwReason: DWORD);stdcall;
  7.   begin
  8.     case dwReason of
  9.       DLL_PROCESS_ATTACH:inc(UseCounter);
  10.       DLL_PROCESS_DETACH:dec(UseCounter);
  11.     end;
  12.   end;
  13.    
  14. begin
  15.   DLLProc := @DLLMain;
  16.   DLLMain(DLL_PROCESS_ATTACH);
  17. end.

UNTESTED!

Note you probably need to adapt this for freepascal. See http://wiki.freepascal.org/shared_library (Dll_Process_Detach_Hook etc);
I think the wiki is incorrect, because this can be done : https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx The dllmain  example there works.
You then declare the counter as external in your main program.

If it doesn't work, use a registry key as the variable.
« Last Edit: January 23, 2017, 11:53:17 am by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: using global counters in a dll .. can it be done ?
« Reply #3 on: January 23, 2017, 12:29:44 pm »
Afaik that only counts if a process loads a dll multiple times, not if multiple processes load the same dll.

Memory mapped works, but the problem is still to start the process that has the memory with the variables on demand.


Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: using global counters in a dll .. can it be done ?
« Reply #4 on: January 23, 2017, 12:46:35 pm »
Afaik that only counts if a process loads a dll multiple times, not if multiple processes load the same dll.

Memory mapped works, but the problem is still to start the process that has the memory with the variables on demand.

Hence my suggestion to store in a registry key. The question is about a windows app.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: using global counters in a dll .. can it be done ?
« Reply #5 on: January 23, 2017, 05:33:08 pm »
Does anybody know if there is a better way to deal with this by sharing a part of the host.exe memory or some other trick ..
For temporary data you can share memory from page file {CreateFileMapping(INVALID_HANDLE_VALUE..., MapViewOfFile(..}. For persistent data use file (you choice) or registry (Thaddy choice).

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: using global counters in a dll .. can it be done ?
« Reply #6 on: January 23, 2017, 06:09:21 pm »
Its is not exactly my choice. It is a MS recommendation (for kernel mode dll's it is even required) See msdn.
Specialize a type, not a var.

tk

  • Sr. Member
  • ****
  • Posts: 361
Re: using global counters in a dll .. can it be done ?
« Reply #7 on: January 23, 2017, 07:03:16 pm »
Right now i store it into a file which i open and close each time the dll is used. This does work, but is not a great solution for obvious reasons.

What are the obvious reasons? I would stay with the file.
Normally use GetAppConfigDir and store the data in a custom file.
It works accross all main platforms (well, except for iOS where you could use your app's cache directory, see http://forum.lazarus.freepascal.org/index.php/topic,35120.0.html).

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: using global counters in a dll .. can it be done ?
« Reply #8 on: January 23, 2017, 07:07:40 pm »
It is a MS recommendation.
Only words without specifics ;D. Here are the concrete things: MSDN! Put, please, url for registry recommendations.

 

TinyPortal © 2005-2018