Get Process Name From HWnd

procedure GetProcessNameFromWnd(Wnd: HWND;path: PAnsiChar)stdcall;
var
List: TStringList;
PID: DWORD;
I: Integer;
Result1:String;
begin
Result1 := '';
//path := PAnsiChar(AnsiString(Result1)) ;
StrPCopy(path, PAnsiChar(AnsiString(Result1)));
//MessageBoxA(0,'1','ok',1);
if IsWindow(Wnd) then
begin
PID := INVALID_HANDLE_VALUE;
GetWindowThreadProcessId(Wnd, @PID);
List := TStringList.Create;
try
if RunningProcessesList(List, True) then
begin
I := List.IndexOfObject(Pointer(PID));
if I > -1 then
Result1 := List[I];
StrPCopy(path, PAnsiChar(AnsiString(Result1)));
end;
finally
List.Free;
end;
end;
end;
typedef void (__stdcall *fun)(HWND,char*);
TCHAR* _dllname = _T("hwnd2name.dll");
ASSERT(PathFileExists(_dllname));
HMODULE _ = LoadLibrary(_dllname);
if(_){
char path[1024];
fun fun1 = (fun)GetProcAddress(_,"GetProcessNameFromWnd");
if(!fun1)
{
return TRUE;
}
if(HWND _3 = m_hWnd){
fun1(_3,path);
CString str(path);
AfxMessageBox(str);
}
}
else{
int n = GetLastError();
TRACE("%d",n);
ASSERT(0);
}

testdll

hwnddll

  1. No trackbacks yet.

Leave a comment