본문 바로가기

Delphi/꿀팁

IE 핸들로 인터페이스 가져오기

uses

  Windows, SHDocVw, MSHTML, ActiveX;

function getHandleToIWeb2(WHandle: HWND; var IE: IWebbrowser2): HRESULT;

type

  TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObject): HRESULT; stdcall;

var

  hLib: THandle;

  lRes: Cardinal;

  nMSG: Integer;

  Doc: IHTMLDocument2;

  ObjectFromLresult: TObjectFromLresult;

begin

  Result := -1;


  WHandle := FindWindowEX(WHandle, 0, 'Frame Tab', nil);

  WHandle := FindWindowEX(WHandle, 0, 'TabWindowClass', nil);

  WHandle := FindWindowEX(WHandle, 0, 'Shell DocObject View', nil);

  WHandle := FindWindowEX(WHandle, 0, 'Internet Explorer_Server', nil);


  if WHandle = 0 then Exit;


  hLib := LoadLibrary('Oleacc.dll');

  @ObjectFromLresult := GetProcAddress(hLib, 'ObjectFromLresult');


  try

    if @ObjectFromLresult <> nil then

    begin

      nMSG := RegisterWindowMessage('WM_HTML_GETOBJECT');

      SendMessageTimeOut(WHandle, nMSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);

      Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, Doc);


      if Result = S_OK then

        (Doc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);

    end;

  finally

    FreeLibrary(hLib);

  end;

end;

Ex)

var

  IE : IWebbrowser2;

hIE : THandle;

begin

..

  if getHandleToIWeb2(hIE ,IE) = 0 then

begin

ShowMessage((IE as IHTMLDocumet2).body.innerHTML);

end;

end;


반응형