我虽然再也没有继续像大学那样没日没夜的写计算机代码了,前几天听见我的朋友说他的公司里面老是有ip冲突,他那边工程师说只要修改了ip地址就可以上网了,可是这个男生可笨了!居然换了好多IP都被人家给占用了,求我给他写个程序,免的IP换的头疼。
那么开始吧!
首先要利用InternetGetConnectedState函数,这个函数是反应你的机器成功连网的函数,返回的是true和false
返回是true
就成功了,不是就连网失败
关于ip修改最好用上系统自己带的netsh命令
那么我就把不才的程序代码贴上来了
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,shellapi,wininet, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Timer1: TTimer; Button1: TButton; StaticText1: TStaticText; Edit1: TEdit; StaticText2: TStaticText; StaticText3: TStaticText; StaticText4: TStaticText; Edit2: TEdit; StaticText5: TStaticText; procedure Button1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure Delay(MSecs: Longint); var FirstTickCount,Now:Longint; begin FirstTickCount := GetTickCount(); repeat Application.ProcessMessages; Now:=GetTickCount(); until (Now-FirstTickCount>=MSecs)or(Now<FirstTickCount); end; procedure TForm1.Button1Click(Sender: TObject); var a:string; b:string; c:integer; d:string; e:string; rec:dword; ask:boolean; ProcessHandle,CMD1 : THandle; ProcessID: DWORD; begin e:=\' \'; a:=\'/k \'; for c:= strtoint(edit2.text) to 255 do begin cmd1:=GetWindowThreadProcessId(FindWindow(\'consolewindowclass\',NIL), ProcessID); if cmd1<>0 then begin ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId); TerminateProcess(ProcessHandle,4); end else b:=\'192.168.13.\'+inttostr(c); statictext2.Caption :=b; d:=a+\' netsh interface ip set address\'+e+\'"\'+edit1.Text+\'"\'+e+\'static\'+e+b+e+\'255.255.255.0\'+e+\'192.168.13.1\'+\' 1\';begin ShellExecute(handle, \'open\', pchar(\'cmd.exe\'),pchar(d), nil, SW_show); end; ask:=InternetGetConnectedState(@rec,0); if ask then begin break; end else begin Delay(25000); cmd1:=GetWindowThreadProcessId(FindWindow(\'consolewindowclass\',NIL), ProcessID); IF cmd1<>0 THEN begin ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId); TerminateProcess(ProcessHandle,4); end end; end; end; procedure TForm1.Timer1Timer(Sender: TObject); var rec:dword; ask:boolean; begin ask:=InternetGetConnectedState(@rec,0); if ask then begin StaticText1.Caption:=\'网络连接成功\'; button1.Enabled:=false; end else begin StaticText1.Caption:= \'网络连接失败\'; button1.Enabled:=true; end; end; end.