Solved

Get name of DLL Caller ?

Posted on 2004-10-11
5
829 Views
Last Modified: 2010-04-04
In global hook ,how can i get the name of the file exe,which called file dll?
code: results of paramstr(0)  in file dll are not same because activate window changed too.
example :
project.exe calls a.dll ,the caller is project1.exe. when a.dll loading,it always shows [messagebox(paramstr(0))] the caller is  "c:\my\project.exe" but if we set focus to yahoo messenger it will show "c:\program files\yahoo\messenger\ypager.exe"

So how can we get the filename of the DLL caller only everytime (project.exe)?

by the way,How can the file dll get  the filename of itself?

(i means a.dll when loading will showmessage the name of itself like c:\....\a.dll )
0
Comment
Question by:lehongwang
  • 3
5 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12274700
SysUtils.GetModuleName(0) does that just fine :)
0
 
LVL 20

Accepted Solution

by:
Madshi earned 250 total points
ID: 12274720
You can't get the name of the exe which started the global hook - if that is what you need. You can get the name of the exe in which the current copy of your dll is loaded. That's exactly what "ParamStr(0)" does. Probably you know that a copy of your dll is loaded into every running GUI process?

Asking the name of the dll itself is easy. Just use GetModuleFileName(HInstance, ...).
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12274727
oh .. my bad .. same thing
although it's very weird .. but since you're in a hook dll it's logical that the topmost window has the input
when do you want to know the modulename ?
anyway you can create a global var and store the name there - every process get's it's own copy of the variable
so in the dllmain entry proc set the global var .. should work :)


// in the dll's .dpr file - ModName can be declared anywhere

var ModName: string = '';

function DoInit: Integer; stdcall;
begin
  ModName:=SysUtils.GetModuleName(0);
end;

procedure EntryPointProc(Reason: Integer);
begin
  case Reason of
    DLL_PROCESS_ATTACH: DoInit;
  end;
end;

begin
  DllProc:=@EntryPointProc;
  EntryPointProc(DLL_PROCESS_ATTACH);
end.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12274735
or shorter:

var ModName: string = '';
begin
  ModName:=SysUtils.GetModuleName(0);
end.


even shorter:

in any unit:

initialization
  ModName:=SysUtils.GetModuleName(0);
end.
0
 

Author Comment

by:lehongwang
ID: 12277418
thanks madshi
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now