dll得到主窗體的handle

function MainApplicationHandle: HWND;
var
  MainModule: HMODULE;
  CurrentPID: DWORD;
  WindowsPID: DWORD;
begin
  MainModule := GetModuleHandle(nil);
  CurrentPID := GetCurrentProcessID();

  Result := 0;
  while True do
  begin
    Result := FindWindowEx(0, Result, 'TApplication', nil);
    if (Result = 0) then Exit; // 未找到

    GetWindowThreadProcessID(Result, WindowsPID);
    if (WindowsPID = CurrentPID) and
       (HMODULE(GetWindowLong(Result, GWL_HINSTANCE)) = MainModule)
    then Exit; // 找到!!
  end;
end;

 
posted @ 2008-04-05 14:15  delphi中间件  阅读(303)  评论(0编辑  收藏  举报