Archive

Posts Tagged ‘sizeof’

How to Resolve a Host Name

December 15th, 1999 m3Rlin No comments

Ever needed to convert a host name to an IP number? There is no direct routine available in Delphi for this but we can always code our way through :-)
Here’s the code with error handling:

uses
  Winsock;
...
// The IP number will be returned in string format in the sIP parameter
function HostToIP(sHost: string; var sIP: string): Boolean;
var
  aHostName: array[0..255] of Char;
  pcAddr   : PChar;
  HostEnt  : PHostEnt;
  wsData   : TWSAData;
begin
  WSAStartup($0101, wsData);
  try
    GetHostName(aHostName, SizeOf(aHostName));
    StrPCopy(aHostName, sHost);
    hostEnt := GetHostByName(aHostName);
    if Assigned(HostEnt) then
      if Assigned(HostEnt^.H_Addr_List) then begin
        pcAddr := HostEnt^.H_Addr_List^;
        if Assigned(pcAddr) then begin
          sIP := Format('%d.%d.%d.%d', [Byte(pcAddr[0]), Byte(pcAddr[1]),
            Byte(pcAddr[2]), Byte(pcAddr[3])]);
          Result := True;
        end else
          Result := False;
      end else
        Result := False
    else begin
      Result := False;
    end;
  finally
    WSACleanup;
  end;
end;

How to Read the Drive’s Serial Number

December 15th, 1999 m3Rlin No comments

The drive’s serial number can be useful for for a lot of things (check out previous tip). This example code shows you how to read the hard drives serial number.

function GetHddSerial: Integer;
var
  aBuffer: array [0..255] of Char;
  dwTemp: DWord;
  pdwSerial: PDWord;
begin
  if GetVolumeInformation('c:', aBuffer, SizeOf(aBuffer), @pdwSerial, dwTemp, dwTemp, nil, 0) then
    Result := pdwSerial^;
end;