改变一下问法,在DLL中如何创建WebBrowser并取动态网页内容?(150)

H

hying95

Unregistered / Unconfirmed
GUEST, unregistred user!
这是Vsun大侠的代码,把这个放在DLL中如何做呢?(dll中动态创建一个WebBrowser)procedure TForm1.Button3Click(Sender: TObject);var Getdoc: IHTMLDocument2; Getall: IHTMLElementCollection; Getitem: IHTMLElement; i, stime, etime: integer;begin while (WebBrowser1.ReadyState < 4) do Application.ProcessMessages; Getdoc := IHTMLDocument2(WebBrowser1.Document); Getall := IHTMLElementCollection(Getdoc.Get_all); for i := 0 to Getall.Length - 1 do begin Getitem := Getall.Item(i, null) as IHTMLElement; if Getitem.id = 'start_score' then ShowMessage(Getitem.OuterText); end;end;procedure TForm1.FormCreate(Sender: TObject);begin WebBrowser1.Navigate('http://188100.net/1218.htm');end;我改的library PKM32;uses ShareMem, ActiveX, Windows, Messages, Classes, Forms, Variants, SHDocVw, MSHTML, WinInet;{$R *.res}var Wb: TWebBrowser; f: TForm;//检测计算机是否上网function CheckNet: Boolean; stdcall;const INTERNET_CONNECTION_MODEM = 1; INTERNET_CONNECTION_LAN = 2; INTERNET_CONNECTION_PROXY = 4; INTERNET_CONNECTION_MODEM_BUSY = 8;var dwConnectionTypes : DWORD;begin dwConnectionTypes := INTERNET_CONNECTION_LAN+INTERNET_CONNECTION_MODEM +INTERNET_CONNECTION_PROXY; Result := InternetGetConnectedState(@dwConnectionTypes, 0);end;procedure MyDLLHandler(Reason: integer);begin case Reason of DLL_PROCESS_DETACH: begin CoUninitialize(); Wb.Free; F.Free; end; DLL_Process_Attach: begin CoInitialize(nil); f:=TForm.Create(NIL); Wb:=TWebBrowser.CreateParented(F.Handle); F.InsertControl(Wb); if not CheckNet then Exit; wb.Navigate('http://188100.net/1218.htm'); end; end;end;function GetKMJG: string;stdcall;var Getdoc: IHTMLDocument2; Getall: IHTMLElementCollection; Getitem: IHTMLElement; i: integer;begin while (Wb.ReadyState < 4) do Application.ProcessMessages; Getdoc := IHTMLDocument2(Wb.Document); Getall := IHTMLElementCollection(Getdoc.Get_all); for i := 0 to Getall.Length - 1 do begin Getitem := Getall.Item(i, null) as IHTMLElement; if Getitem.id = 'start_score' then Result :=Getitem.outerText; end;end;exports GetKMJG; begin DLLProc := @MyDLLHandler; MyDLLHandler(DLL_Process_Attach);end.测试时调用 窗口一运行马上消失,在进程窗口可以看到,而且要用结束进程的方式才能结束它
 
可以启动个线程来干活while not Terminated dobegin Sleep(xxxx); CheckNet...............end;
 
to sianjun我想在DLL中实现它
 
在DLL中实现好象比较难
 
你要实现什么东西?那个TIMER?实现那个TIMER很简单的啊。可以自己做一个线程,然后不停循环去做你要做的事就可以了,或者你创建一个会话窗口,然后绑定WMTIMER消息,再去处理就可以了
 
在5分钟内读取更新的网页上内容,更新内成后得到结果,我想把procedure TFrmKM.WbnHttp(var s:string);var aSection: string; ii: tinifile;begin ii:=tinifile.Create('./WZ32.ini'); aSection:=ini.ReadString('Https','WZ','A'); begin S:=''; WebBrowser1.Navigate(ii.ReadString(aSection,'WZ','http://www.855800.com/1218.htm')); if not(WebBrowser1.Busy) then s:=(IHtmlDocument2(WebBrowser1.Document).Body.OuterText); end;end;这段代码放到DLL中,启动定时器,读取网页上内容,如果网页上已经更新,取得内容,返回结果。
 
想看看码?
 
来学习一下,没有用过webbrowser,自己试着用熟悉一点的东西写了一段,给你参考一下library Project1;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses SysUtils, ComObj, Classes;{$R *.res}function GetHTML: string; stdcall;var LoadHttp, Matches, MyRegExp: OleVariant; GetUserName, GetPassWord: string;begin //读取目标网站信息 LoadHttp := CreateOleObject('MicroSoft.XMLHTTP'); LoadHttp.open('GET', 'http://www.naoz.cn/', false); LoadHttp.send; //将读取的信息进行分析处理,从中提取升级账号及密码 MyRegExp := CreateOleObject('VBScript.RegExp'); MyRegExp.Global := true; MyRegExp.IgnoreCase := false; MyRegExp.Pattern := 'nod32升级id用户名:([.//w:?/=/%/&/#/-]{0,})
nod32升级id密码:([.//w:?/=/%/&/#/-]{0,})
'; //显示找到的结果 if MyRegExp.test(LoadHttp.responsetext) then begin Matches := MyRegExp.Execute(LoadHttp.responsetext); GetUserName := Matches.Item[0].SubMatches[0]; //获取网站上的用户名; GetPassWord := Matches.Item[0].SubMatches[1]; //获取网站上的密码; end; result := Format('用户名:%s 密码:%s', [Matches.Item[0].SubMatches[0], Matches.Item[0].SubMatches[1]]);end;exports GetHTML;end.
 
Vsen大侠,请帮下忙下面还有两个贴有200分全部给你http://www.delphibbs.com/delphibbs/dispq.asp?lid=3986128http://www.delphibbs.com/delphibbs/dispq.asp?lid=3986912
 
to Vsun 大侠真早 LoadHttp.open('GET', 'http://www.naoz.cn/', false);这里的‘GET’每个网页是不是相同呢, MyRegExp.Pattern := 'nod32升级id用户名:([.//w:?/=/%/&/#/-]{0,})
nod32升级id密码:([.//w:?/=/%/&/#/-]{0,})
'; MyRegExp.Pattern的内容是如何确定呢? 我想取这里的开码内容“http://188100.net/1218.htm”
 
对OleVariant不懂,再请VSun帮忙
 
你现在是哪一部分无法实现?上面的回答基本上都覆盖了实现你需要的所有功能了。
 
to xianjun取这里的内容:http://188100.net/1218.htmLoadHttp.open('GET', 'http://www.naoz.cn/', false);这里的‘GET’每个网页是不是相同呢, MyRegExp.Pattern := 'nod32升级id用户名:([.//w:?/=/%/&/#/-]{0,})
nod32升级id密码:([.//w:?/=/%/&/#/-]{0,})
';
 
从来都没有用过OleVariant,Matches.Item[0].SubMatches[0];这里的SubMatches[里面的数字个数]能否把Matches.item保存下来看看?
 
直接用IdHttp是最方便的,没必要使用OleObject另外IdHttp取到的是静态内容,如果需要JavaScript脚本执行后的结果,那还是用WebBrowser才能实现要求。
 
我原本是用webbrowser,在DLL中没有实现,
 
感觉Vsen他的代码取内容比较 快
 
to Xianjun你能否用Webbrowser在DLL中实现呢?
 
用IdHttp取内容是一样快的,但估计不能达到你的要求,因为只能取到静态页面(未执行脚本前的页面)WebBrowser也可以在DLL中实现的,你试一下,如果有问题,可能要自己做一下消息循环。
 
在DLL中这段不好实现procedure TFrmKM.WbnHttp(var s:string);var aSection: string; ii: tinifile;begin ii:=tinifile.Create('./WZ32.ini'); aSection:=ini.ReadString('Https','WZ','A'); begin S:=''; WebBrowser1.Navigate(ii.ReadString(aSection,'WZ','http://www.855800.com/1218.htm')); if not(WebBrowser1.Busy) then s:=(IHtmlDocument2(WebBrowser1.Document).Body.OuterText); end;end;
 
顶部