Solved

Get name of DLL Caller ?

Posted on 2004-10-11
5
842 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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