Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi GetWindowModuleFileName gibt nichts zurück (https://www.delphipraxis.net/34348-getwindowmodulefilename-gibt-nichts-zurueck.html)

c113plpbr 21. Nov 2004 17:36


GetWindowModuleFileName gibt nichts zurück
 
Ich versuche per GetWindowModuleFileName den Namen (& Pfad) eines Programms über das FensterHandle herauszufinden, doch leider gibt die Funktion nichts zurück.
Mein Code sieht in etwa so aus:
Delphi-Quellcode:
var
  hWnd_ : HWND;
  fname : PAnsiChar;
begin
  fname := nil;
  hWnd_ := FindWindow('Notepad', nil);
  if(hWnd_ = 0)then
  begin
    Application.MessageBox('FindWindow failed!', '', 0);
    exit;
  end;

  GetWindowModuleFileNameA(hWnd_, fname, MAX_PATH);

  Application.MessageBox(fname, 'fname', 0);
end;
Die "fname"-Message bleib immer leer. Weis jemand woran das liegt?

ciao & thx, Philipp

Dax 21. Nov 2004 17:40

Re: GetWindowModuleFileName gibt nichts zurück
 
So msollte es gehen:
Delphi-Quellcode:
var
  hWnd_ : HWND;
  fname : PAnsiChar;
begin
  GetMem(fname, MAX_PATH);
  hWnd_ := FindWindow('Notepad', nil);
  if(hWnd_ = 0)then
  begin
    Application.MessageBox('FindWindow failed!', '', 0);
    exit;
  end;

  GetWindowModuleFileNameA(hWnd_, fname, MAX_PATH);

  Application.MessageBox(fname, 'fname', 0);
end;

c113plpbr 21. Nov 2004 17:43

Re: GetWindowModuleFileName gibt nichts zurück
 
@Dax: Das hab ich auch schon probiert, leider erfolglos.

jim_raynor 21. Nov 2004 18:08

Re: GetWindowModuleFileName gibt nichts zurück
 
Versuche fName als Array[0..MAX_PATH] of Char zu deklarieren.

c113plpbr 21. Nov 2004 18:11

Re: GetWindowModuleFileName gibt nichts zurück
 
@jim_raynor: Hab ich auch schon probiert, funktioniert leider auch nicht. :(

jim_raynor 21. Nov 2004 18:14

Re: GetWindowModuleFileName gibt nichts zurück
 
Dann prüfe doch mal mit GetLastError den Fehlercode, den dir Windows zurück gibt.

c113plpbr 21. Nov 2004 18:19

Re: GetWindowModuleFileName gibt nichts zurück
 
Zitat:

Zitat von jim_raynor
Dann prüfe doch mal mit GetLastError den Fehlercode, den dir Windows zurück gibt.

Der ist ganz ordnungsgemäß 0 ...
Kann das mal jemand bei sich testen? Geht das allgemein nicht, liegt es an meinem rechner, oder is da irgendwas anderes faul?

Luckie 21. Nov 2004 18:49

Re: GetWindowModuleFileName gibt nichts zurück
 
Der Klssen name vom Notepad ist wahrscheinlich nicht "Notepad":
Delphi-Quellcode:
hWnd_ := FindWindow('Notepad', nil);
Als Handle wirst du dann eben null zurückbekommen. Guck doch erstmal, ob diu da ein gültiges Handle bekommst. Desweiteren würde ich den Vorschlag mit GetLastError nicht so einfach abtun, wer weiß, was eventuell noch shcief geht. :roll:

c113plpbr 21. Nov 2004 19:13

Re: GetWindowModuleFileName gibt nichts zurück
 
Zitat:

Zitat von Luckie
Der Klssen name vom Notepad ist wahrscheinlich nicht "Notepad":
Delphi-Quellcode:
hWnd_ := FindWindow('Notepad', nil);

Doch ^^ ... ich hab nachgeguggt! Außerdem würde ansonsten ja die andere Message kommen.
Zitat:

Zitat von Luckie
Desweiteren würde ich den Vorschlag mit GetLastError nicht so einfach abtun, wer weiß, was eventuell noch shcief geht. :roll:

Was verbreche ich denn noch alles? :gruebel: ;)

Kann jemand den code bei sich testen, ob er da funktioniert, bitte!

ciao, Philipp

Luckie 21. Nov 2004 19:33

Re: GetWindowModuleFileName gibt nichts zurück
 
Zitat:

Zitat von c113plpbr
Was verbreche ich denn noch alles? :gruebel: ;)

So lange du nicht GetLastError aufrufst, wirst du es nie erfahren. :roll:

SirThornberry 21. Nov 2004 19:54

Re: GetWindowModuleFileName gibt nichts zurück
 
GetModuleFilename funktioniert glaub ich nur wenn das handle zu deiner anwendung gehört, also du zum beispiel eine dll eingebunden hast. Wenn du an den dateinamen der Anwendung willst zu dem das Handle gehört musst du das anders machen.

[Edit]Hab grad mal die Suche bemüht und einen Beitrag gefunden den ich mal gepostet hab der genau das macht was du willst
Hier der Link: http://www.delphipraxis.net/internal...=146809#146809
[/Edit]

c113plpbr 21. Nov 2004 20:05

Re: GetWindowModuleFileName gibt nichts zurück
 
Also, ich hab nun mal GetLastError zu meinen Überwachten "ausdrücken" hinzugefügt (funktionsaufrufe sind gestattet), und bin schritt für schritt durchgegangen, und GetLastError war *immer* 0
Da ist kein "fehler" da ... oder? :gruebel: :wiejetzt: :roll:

@SirThornberry: Sollte dann da nicht zumindest ein Hinweis in der PSDK dazu stehen? Ich hab keinen gesehen ...

Luckie 21. Nov 2004 20:08

Re: GetWindowModuleFileName gibt nichts zurück
 
Guck dir mal den ersten Parameter an:
Zitat:

hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.
Das ist nicht das Fensterhandle, sondern das Dateihandle.

Du müsstest also MSDN-Library durchsuchenGetModuleFileNameEx benutzen. Und dort findest du sogar noch ein Beispiel:
Enumerating All Modules For a Process
To determine which processes have loaded a particular DLL, you must enumerate the modules for each process. The following sample code uses the MSDN-Library durchsuchenEnumProcessModules function to enumerate the modules of current processes in the system.
Code:
#include <windows.h>
#include <stdio.h>
#include "psapi.h"

void PrintModules( DWORD processID )
{
    HMODULE hMods[1024];
    HANDLE hProcess;
    DWORD cbNeeded;
    unsigned int i;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Get a list of all the modules in this process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
    {
        for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
        {
            char szModName[MAX_PATH];

            // Get the full path to the module's file.

            if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
                                      sizeof(szModName)))
            {
                // Print the module name and handle value.

                printf("\t%s (0x%08X)\n", szModName, hMods[i] );
            }
        }
    }

    CloseHandle( hProcess );
}

void main( )
{
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return;

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the name of the modules for each process.

    for ( i = 0; i < cProcesses; i++ )
        PrintModules( aProcesses[i] );
}

SirThornberry 21. Nov 2004 20:08

Re: GetWindowModuleFileName gibt nichts zurück
 
wenn du mal anstelle des notepadhandles das handle der eigenen Anwendung nimmst wirst du sehen das dein Beispiel funktioniert. Daraus hab ich das einfach mal vermutet das es daran liegt das es kein Handle ist das zur eigenen Anwendung gehört. Wenn du eine DLL in dein programm einbindest und in der dll dein source mit dem handle der hostanwendung ausführst sollte das auch funktionieren... naja, wie schon geschrieben hab ich den link zu funktionierendem source schon gepostet.

w3seek 21. Nov 2004 20:12

Re: GetWindowModuleFileName gibt nichts zurück
 
Zitat:

Zitat von Luckie
Guck dir mal den ersten Parameter an:
Zitat:

hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.
Das ist nicht das Fensterhandle, sondern das Dateihandle.

Wenn ich dich korrigieren darf, das ist die base adress des modules (also im Grunde ein simpler pointer), kein handle ;)

c113plpbr 21. Nov 2004 20:13

Re: GetWindowModuleFileName gibt nichts zurück
 
Zitat:

Zitat von SirThornberry
[Edit]Hab grad mal die Suche bemüht und einen Beitrag gefunden den ich mal gepostet hab der genau das macht was du willst
Hier der Link: http://www.delphipraxis.net/internal...=146809#146809
[/Edit]

Jup, danke, ich kenne diese Methode, nur frag ich mich trotzdem, warum der obige befehl einfach so ohne Hinweis seinen Dienst verweigert ... (außerdem gefallen mir einzeilige befehle besser, als ganze proceduren ... ;) )

ok, da wohl niemand ne idee hat woran das liegen könnte ... muss ich wohl doch wieder zur längeren Methode greifen ... :-| ... trotzdem danke für eure Ratschläge, Tipps & Bemühungen!

ciao, Philipp

[edit]man seid ihr schnell ... ich wollte es schon aufgeben (hab die letzen posts nimmer gesehen) ... ^^ ... ok, danke[/edit]

Motzi 21. Nov 2004 20:24

Re: GetWindowModuleFileName gibt nichts zurück
 
Hab mal schnell den Code rausgesucht den ich in meinem X-Spy verwend und bissi angepasst...

Delphi-Quellcode:
function GetWindowModule(Window: HWND): String;
var
  dwProcessID : DWord;
  Process    : hProcess;
  Module     : hModule;
  Path       : array [0..MAX_PATH] of Char;
begin
  GetWindowThreadProcessID(Window, @dwProcessID);
  Module := GetClassLong(Window, GCL_HMODULE);
  if (Module = 0) and (GetLastError <> 0) then
    Result := '(Unavailable)';
  else
  begin
    Process := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,
      dwProcessID);
    if Process <> 0 then
    begin
      if GetModuleFileNameEx(Process, Module, Path, MAX_PATH) > 0 then
      begin
        GetLongPathName(Path, Path, MAX_PATH);
        Result := String(Path)
      end
      else
        Result := '(Unavailable)';
      CloseHandle(Process);
    end
    else
      Result := '(Unavailable)';
  end;
end;
@w3seek: jain.. es ist ein Module-Handle.. das PSDK sagt dazu:
Zitat:

Zitat von PSDK
HMODULE: Handle to a module. The value is the base address of the module.


w3seek 21. Nov 2004 23:21

Re: GetWindowModuleFileName gibt nichts zurück
 
es ist definitv kein handle :P weder ein user/gdi object, noch ein kernel handle. es ist einfach ein pointer zur base adresse des moduls. nichts mehr und nichts weniger ;)

perle 22. Nov 2004 14:27

Re: GetWindowModuleFileName gibt nichts zurück
 
bei mir geht das so:

Code:
var
 lProcessID: dWord;
 lHandle: THandle;
begin
 GetWindowThreadProcessId(WindowHandle, @lProcessID);
 SetLength(Buffer, MAX_PATH);
 lHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, True, lProcessID);
 if GetModuleFileNameEx(lHandle, 0, PChar(Buffer), MAX_PATH) > 0 then
   SetLength(Buffer, StrLen(PChar(Buffer)));
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:39 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz