Internet

Get/set default page of IE

Posted in Internet

Information about default page of Internet Explorer you may find at 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main' path ('Start page' parameter). Use standard functions to set/get this information from registry.

uses Registry;
...
// Get default page
procedure TForm1.Button1Click(Sender: TObject);
begin
  with TRegistry.Create do
  begin
    RootKey:=HKEY_CURRENT_USER;
    if OpenKey('Software\Microsoft\Internet Explorer\Main', TRUE) then
      ShowMessage(ReadString('Start Page'))
    else
      MessageDlg('Registry read error', mtError, [mbOk], 0);
    CloseKey;
  end;
end;

Get current url

Posted in Internet

Use SetLink and RequestData methods of TDDEClientConv type. SetLink specifies the service and topic of a DDE conversation. RequestData requests data from a DDE server.

procedure TForm1.Button1Click(Sender: TObject);
var
  DDEClient: TDDEClientConv;
  Str: PChar;
begin
  DDEClient:=TDDEClientConv.Create(nil);
  with DDEClient do
  begin
    SetLink('IExplore','WWW_GetWindowInfo');
    Str:=RequestData('0xFFFFFFFF');
    Caption:=StrPas(Str);
    StrDispose(Str);
  end;
  DDEClient.Free;
end;

Get host name using IP-address

Posted in Internet

Use IPAddrToName function. This function get host information by using GetHostByAddr function.

function IPAddrToName(IPAddr: string): string;
var
  SockAddrIn: TSockAddrIn;
  HostEnt: PHostEnt;
  WSAData: TWSAData;
begin
  WSAStartup($101, WSAData);
  SockAddrIn.sin_addr.s_addr:=inet_addr(PChar(IPAddr));
  HostEnt:= GetHostByAddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
  if HostEnt<>nil then
  begin
    Rresult:=StrPas(Hostent^.h_name)
  end
  else
  begin
    Result:='';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:=IPAddrToName(Edit1.Text);
end;

Get Internet connected state

Posted in Internet

To get Internet connected state, use InternetGetConnectedState function. This example is working in Delphi4 and higher.

uses WinInet;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  State: DWord;
  Result: Boolean;
const
  Type1 = INTERNET_CONNECTION_MODEM;
  Type2 = INTERNET_CONNECTION_LAN;
  Type3 = INTERNET_CONNECTION_PROXY;
  Type4 = INTERNET_CONNECTION_MODEM_BUSY;
begin
  Result:=InternetGetConnectedState(@State, 0);
  if Result then
  begin
    if (State and Type1) = Type1 then
      Label1.Caption:=Label1.Caption+'Modem ';
    if (State and Type2) = Type2 then
      Label1.Caption:=Label1.Caption+'LAN ';
    if (State and Type3) = Type3 then
      Label1.Caption:=Label1.Caption+'Proxy ';
    if (State and Type4) = Type4 then
      Label1.Caption:=Label1.Caption+'Modem busy ';
  end;
end;

Create e-mail letter

Posted in Internet

Use ShellExecute function, like in this example, to create e-mail letter in default mail program.

uses shellapi;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  BodyStr, LineStr: string;
  i: Integer;
begin
  //create body
  BodyStr:='';
  for i:=0 to Memo1.Lines.Count-1 do
  begin
    LineStr:=Memo1.Lines[i];
    while Pos(' ', LineStr)>0 do
      LineStr:=Copy(LineStr,
                    1, 
                    Pos(' ', LineStr)-1)+'%20'+
                      Copy(LineStr, 
                        Pos(' ', LineStr)+1, 
                        Length(LineStr)-Pos(' ', LineStr));
    BodyStr:=BodyStr+LineStr+'%0D%0A';
  end;

ShellExecute(
  Handle,
  'Open',
  PChar('mailto:'+Edit1.Text+
    '?subject='+Edit2.Text+
    '&Body='+BodyStr+
    '&CC='+Edit3.Text),
  nil,
  nil,
  SW_RESTORE);
end;