Talking about Internet programming skills under Delphi (1)
Author: lyboy99E-mail: [email protected] comes with a lot of Internet application programming controls, which makes it easier for us to develop Internet applications. Below I will gradually introduce some application programming skills on the Internet. These skills are Some subtle aspects, but it can add important functions to your applications, making it easier for you to develop applications under the Internet. After the opening narration, let’s first introduce: setting the system default browser and system default email sending and receiving software. 1. Get the default Internet browser address function: The following function obtains the address of the default Internet browser by reading the registry settings function GetDefaultShellHTTP: string;varreg: TRegistry;begin Reg:=TRegistry.Create; Reg .RootKey:=HKEY_CLASSES_ROOT; if Reg.KeyExists('http/shell/open/command') then begin Reg.OpenKey('http/shell/open/command',false); Result:=Reg.ReadString(''); end else Result:=''; Reg.Free;end;2. Set the internet browser PRocedure SetDefaultShellHttp (CmdLine : string);varreg : TRegistry;begin Reg:=TRegistry.Create; Reg.RootKey:=HKEY_CLASSES_ROOT; //The address of the registry: Reg.OpenKey('http/shell/open/command',true);//The address of the registry: Reg.WriteString('',CmdLine); Reg. Free;end;setDefaultshellhttp('"C:/PROGRA~1/INTERN~1/iexplorer.exe" -nohome');3. Obtain and set the function of the default E-Mail sending and receiving software. The following function obtains the address of the default E-mail sending and receiving software by reading the registry settings. function GetDefaultMail: string;varreg: TRegistry; begin Reg:=TRegistry.Create; Reg.RootKey:=HKEY_CLASSES_ROOT; if Reg.KeyExists('Mailto/shell/open/command') then begin Reg.OpenKey('Mailto/shell/open/command',false); Result:=Reg.ReadString(''); end else Result:=''; Reg.Free;end;4. Set the default mailbox procedure SetDefaultMail (CmdLine : string);varreg : TRegistry;begin Reg:=TRegistry.Create; Reg.RootKey:=HKEY_CLASSES_ROOT; Reg.OpenKey('Mailto/shell/open/command',true); Reg.WriteString('',CmdLine); Reg.Free;end;Use //SetDefaultMail('E:/Foxmail/FoxMail.exe -T " %1" -S "%2"');5. Have you ever wanted to have a function to convert a domain name to an IP address? Now I will give you a function to convert a domain name to an IP address: function GetIPName(Name: string): string;var WSAData: TWSAData; HostEnt: PHostEnt;begin WSAStartup(2, WSAData); HostEnt := gethostbyname(PChar(Name)); with HostEnt^ do Result := Format('%d.%d.% d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]); WSACleanup;end; 6. When writing Internet software, you often encounter checking the URL, E-mail address, etc. entered by the user. How to solve this problem? I happen to have a function written here. Check whether a URL is valid uses wininet; Function CheckUrl(url:string):boolean; //Check whether a URL is valid function var hsession, hfile, hRequest: hInternet; dwindex,dwcodelen:dWord; dwcode:array[1..20] of char; res : pchar; begin if pos('http://',lowercase(url))=0 then url := 'http://'+url; Result := false; hSession := InternetOpen('InetURL:/1.0', INTERNET_OPEN_TYPE_PRECONFIG,nil, nil, 0); if assigned(hsession) then begin hfile := InternetOpenUrl(hsession, pchar (url), nil, 0, INTERNET_FLAG_RELOAD, 0); dwIndex := 0; dwCodeLen := 10; HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex); res := pchar(@dwcode); result:= (res ='200') or (res ='302'); / /200,302 unrelocation flag if assigned(hfile) then InternetCloseHandle(hfile); InternetCloseHandle(hsession); end; end;
How to handle E-mail addresses, here is an E-mail address processing function function IsEMail(EMail: String): Boolean; var s: String; ETpos: Integer; begin ETpos:= pos('@', EMail); if ETpos > 1 then begin s:= copy(EMail,ETpos+1,Length(EMail)); if (pos('.', s) > 1) and (pos('.', s) <length(s)) then Result:= true else Result:= false; end else Result:= false; end; procedure TForm1.Button1Click(Sender: TObject); begin if isemail(Edit1.Text) then begin ShowMessage('eMail-Address!'); end; end; 7. Dynamically change the address of DNS Server
The following function can add the address of DNS Server
If you want to add 202.100.100.65 202.10.10.10
SetDNSAddresses('202.100.100.65 202.10.10.10');
//Note: Separate each address with a space.
SetTDNSAddresses is defined as follows: procedure SetDNSAddresses( sIPs : string );
begin
// If it is Windows NT, use the following code
SaveStringToRegistry_LOCAL_MACHINE(
'SYSTEM/CurrentControlSet' +
'/Services/Tcpip/Parameters',
'NameServer',
sIPs );
// If you are using Windows 95 use the following code
SaveStringToRegistry_LOCAL_MACHINE(
'SYSTEM/CurrentControlSet' +
'/Services/VxD/MSTCP',
'NameServer',
sIPs );
end;where SaveStringToRegistry_LOCAL_MACHINE is defined:
uses Registry;
procedure SaveStringToRegistry_LOCAL_MACHINE(
sKey, sItem, sVal : string );
var
reg: TRegIniFile;
begin
reg := TRegIniFile.Create( '' );
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.WriteString( sKey, sItem, sVal + #0 );
reg.Free;
end;