Archive

Posts Tagged ‘System’

How to Get the Windows Directory

December 15th, 1999 m3Rlin No comments

To found out the name of the Windows directory it’s best to use the Windows API GetWindowsDirectory() function.

Here’s the Delphi implementation:

uses Windows;
...
function GetWindowsDir: string;
const
(* The length of the directory buffer. Usually 64 or even 16 is enough :-)
**
** Must be DWORD type.
*)
  dwLength: DWORD = 255;
var
  pcWinDir: PChar;
begin
  GetMem(pcWinDir, dwLength);
  GetWindowsDirectory(pcWinDir, dwLength);
  Result := string(pcWinDir);
  FreeMem(pcWinDir, dwLength);
end;

How to Check if You are Connected to the Internet

December 15th, 1999 m3Rlin No comments

Whenever you try to connect to connect to a Internet server while no Internet connection is present you can be almost certain an Access Violation will fire.

The only way around this is to check whether a connection is present or not. You can use the code below.

uses
  Windows, SysUtils, Registry, WinSock, WinInet;
...
type
  TConnectionType = (ctNone, ctProxy, ctDialup);
...
function ConnectedToInternet: TConnectionType;
function RasConnectionCount: Integer;
...
implementation
...
const
  ERROR_BUFFER_TOO_SMALL = 603;
  RAS_MAXENTRYNAME = 256;
  RAS_MAXDEVICENAME = 128;
  RAS_MAXDEVICETYPE = 16;
type
  ERasError = class(Exception);

  HRASConn = DWORD;
  PRASConn = ^TRASConn;
  TRASConn = record
    dwSize: DWORD;
    rasConn: HRASConn;
    szEntryName: array[0..RAS_MAXENTRYNAME] of Char;
    szDeviceType: array[0..RAS_MAXDEVICETYPE] of Char;
    szDeviceName: array[0..RAS_MAXDEVICENAME] of Char;
  end;

  TRasEnumConnections =
    function(RASConn: PrasConn; // Buffer to receive Connections data
      var BufSize: DWORD; // Size of buffer (in bytes)
      var Connections: DWORD // Number of Connections written to buffer
    ): Longint;
  stdcall;
...
function ConnectedToInternet: TConnectionType;
var
  UseProxy: LongWord;
  Reg: TRegistry;
begin
  Result := ctNone;
  Reg := TRegistry.Create;
  with Reg do
    try
      try
        RootKey := HKEY_CURRENT_USER;
        if OpenKey('Software\\Microsoft\\Windows\\CurrentVersion\\Internet settings', False) then begin
          if GetDataType('ProxyEnable') = rdBinary then
            ReadBinaryData('ProxyEnable', UseProxy, SizeOf(Longword))
          else
            UseProxy := Integer(ReadBool('ProxyEnable'));
          if (UseProxy <> 0) and (ReadString('ProxyServer') <> '') then
            Result := ctProxy;
        end;
      except
        // No connection through proxy
      end;
    finally
      Free;
    end;

  if Result = ctNone then
    if RasConnectionCount > 0 then
      Result := ctDialup;
end;

function RasConnectionCount: Integer;
var
  Conns: array[1..4] of TRasConn;
  BufSize, NumConns: DWORD;
  RasDLL: HInst;
  RasResult: Longint;
  RasEnums: TRasEnumConnections;
begin
  Result := 0;
  // Load the RAS DLL
  RasDLL := LoadLibrary('rasapi32.dll');
  if RasDLL = 0 then
    Exit;

  try
    RasEnums := GetProcAddress(RasDLL, 'RasEnumConnectionsA');
    if @RasEnums = nil then
      raise ERasError.Create('RasEnumConnectionsA not found in rasapi32.dll');

    Conns[1].dwSize := SizeOf(Conns[1]);
    BufSize         := SizeOf(Conns);
    RasResult       := RasEnums(@Conns, BufSize, NumConns);
    if (RasResult = 0) or (Result = ERROR_BUFFER_TOO_SMALL) then
      Result := NumConns;
  finally
    FreeLibrary(RasDLL);
  end;
end;