Twój kod nie jest do końca prawidłowy. Drugim parametrem funkcji enumerującej okienka jest typ LPARAM
. Ale jeżeli musisz mieć to tak dziwnie to sobie zostaw tylko nie powinno się tak robić raczej.
Poniżej masz kod mojego modułu, pobierającego na podstawie uchwytu okna głownego - uchwyt ikonki programu. Wywnioskujesz z niego jak uzyskać PID na podstawie HWND, a następnie pełną ściezkę do pliku procesu o tym PID. Jeżeli ściezka nie jest ustalona pierwszą metodą (jest pusta), to kod sprawdza ją dla procesu 64 bitowego. Dopasuj sobie kod do swoich potrzeb. A więcej informacji znajdziesz na MSDNie.
Doam, że kod pisany pod Delphi 7 dla celów używania w programach pisanych w czystym WinAPI.
unit get_hicon_from_hwnd;
interface
uses
Windows, PSApi, ShellApi;
function GetIconFromHandle(WindowHandle : HWND) : HICON;
implementation
function StrLen(const Str : PChar) : Cardinal; assembler;
asm
MOV EDX,EDI
MOV EDI,EAX
MOV ECX,0FFFFFFFFH
XOR AL,AL
REPNE SCASB
MOV EAX,0FFFFFFFEH
SUB EAX,ECX
MOV EDI,EDX
end;
function GetIconFromHandle(WindowHandle : HWND) : HICON;
function ProcessFullPath(Pid : DWORD) : string;
var
AHandle : THandle;
begin
Result := '';
AHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, Pid);
if AHandle <> 0 then
begin
try
SetLength(Result, MAX_PATH);
if GetModuleFileNameEx(AHandle, 0, PChar(Result), MAX_PATH) > 0 then
begin
SetLength(Result, StrLen(PChar(Result)));
end
else
begin
Result := '';
end;
finally
CloseHandle(AHandle);
end;
end;
end;
function ProcessFullPath64Bit(Pid : DWORD) : string;
const
PROCESS_QUERY_LIMITED_INFORMATION = $1000;
var
Len : DWord;
AHandle, DllHandle : THandle;
QueryFullProcessImageNameA : function(HProcess : THandle; dwFlags : DWord; lpExeName : PAnsiChar; lpdwSize : PDWord) : Bool; stdcall;
begin
Result := '';
DllHandle := LoadLibrary('kernel32.dll');
if DllHandle > 0 then
begin
QueryFullProcessImageNameA := GetProcAddress(DllHandle, 'QueryFullProcessImageNameA');
if Assigned(QueryFullProcessImageNameA) then
begin
AHandle := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, Pid);
if AHandle <> 0 then
begin
Len := MAX_PATH;
SetLength(Result, Len - 1);
QueryFullProcessImageNameA(AHandle, 0, PAnsiChar(Result), @len);
SetLength(Result, Len);
CloseHandle(AHandle);
end;
end;
FreeLibrary(DllHandle);
end;
end;
var
Pid : DWORD;
ProcesPath : string;
FileInfo : SHFILEINFO;
begin
Result := 0;
if ISWindow(WindowHandle) then
begin
GetWindowThreadProcessId(WindowHandle, Pid);
ProcesPath := ProcessFullPath(Pid);
if ProcesPath = '' then
begin
ProcesPath := ProcessFullPath64Bit(Pid)
end;
if ProcesPath <> '' then
begin
SHGetFileInfo(PChar(ProcesPath), 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_LARGEICON);
Result := FileInfo.hIcon;
end;
end;
end;
end.