iwebbrowser2 c 读取返回html,Delphi通过IE窗口句柄获取网页接口(IWebBrowser2)

主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义。

uses SHDocVw, MsHtml, ActiveX;

type

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

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

var

hInst: HWND;

lRes: Cardinal;

MSG: Integer;

pDoc: IHTMLDocument2;

ObjectFromLresult: TObjectFromLresult;

begin

Result := S_False;

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

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

if @ObjectFromLresult <> nil then begin

try

MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');

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

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

if Result = S_OK then

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

finally

FreeLibrary(hInst);

end;

end;

end;

调用例子,以下代码快速关闭所有打开的IE窗口:

procedure TForm1.Button1Click(Sender: TObject);

var

hCurWindow, hMainWnd, hTabWnd, hCldWnd:HWnd; //窗口句柄

WinClsName:array[0..255] of char;

IE1: IWebbrowser2;

begin

hCurWindow := GetWindow(Handle,GW_HWNDFirst); //获取第一个窗口的句柄

while hCurWindow<>0 do

begin

GetClassName(hCurWindow, @WinClsName, 255);

if String(WinClsName) = 'IEFrame' then

begin

hMainWnd := hCurWindow;

hCldWnd := hCurWindow;

hTabWnd := 0;

repeat //循环查找所有选项卡

hTabWnd := FindWindowEx(hMainWnd, hTabWnd, 'Frame Tab', nil);

if hTabWnd <> 0 then hCldWnd := FindWindowEx(hTabWnd, 0, 'TabWindowClass', nil);

if hCldWnd <> 0 then hCldWnd := FindWindowEx(hCldWnd, 0, 'Shell DocObject View', nil);

if hCldWnd <> 0 then hCldWnd := FindWindowEx(hCldWnd, 0, 'Internet Explorer_Server', nil);

if hCldWnd <> 0 then if GetIEFromHWnd(hCldWnd, IE1) = S_OK then //获取IWebBrowser2

begin

IE1.Quit; //关闭IE,也可以执行其他操作,呵呵

end;

until hTabWnd = 0;

end;

hCurWindow:=GetWindow(hCurWindow,GW_HWNDNEXT); //获取下一个窗口的句柄

end;

end;

function Tfrm_Main.GetValueByHTMLDocument(): string;

var

Dispatch: IDispatch;

Web: IWebBrowser2;

ShellWindow: IShellWindows;

i: integer;

sUserID,sIP: string;

Finput: IHTMLInputElement;

HTMLDocument: IHTMLDocument2;

LoginListTemp:array[1..7]of string;

begin

Result := '';

// tmr3.Enabled:= False;

ShellWindow := CoShellWindows.Create;

Move(ListLogin,LoginListTemp,SizeOf(LoginListTemp));

for i := 0 to ShellWindow.Count do

begin

try

Dispatch := ShellWindow.Item(i);

if Dispatch = nil then continue;

Dispatch.QueryInterface(IWebBrowser2, Web);

if Web = nil then continue;

if Pos('iexplore.exe', LowerCase(Web.FullName)) = 0 then Continue;

if Pos('http://15.0.32.10:1320/cos_tapp/', LowerCase(Web.LocationURL)) <0 then Continue;

Web.Document.QueryInterface(IHTMLDocument2, HTMLDocument);

if HTMLDocument = nil then Exit;

Finput := HTMLDocument.all.item('ipt_userCode',varEmpty) as IHTMLInputElement;

if Finput <> nil then

begin

Result := Finput.value; // 工号

if AnsiIndexText(Result, ListLogin) >= 0 then

begin

try

sIP := GUserParam.UserIP;

//WriteTxtLog(' IP: '+sIP+', 尝试登陆调度工号: '+Result+'。');

HTMLDocument.parentWindow.execScript('document.getElementById(''ipt_userCode'').value=''您已被记录,请不要尝试登陆调度工号!'';','javascript');

IPCrab(sIP);

except on e: Exception do

begin

WriteLog('浏览器安全等级过高,请降低安全等级! ' + e.Message,'E');

end;

end;

end;

end;

except on e: Exception do

begin

end;

end;

end;

end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值