views:

559

answers:

3

Possible Duplicates:
Speed up File.Exists for non existing network shares
Faster DirectoryExists function?

We need to write text to a file on our network, but there may be a situation where that location does not exist and we need to write to another location instead. How do you check quickly that a network location exists? Attempting to write text to a location that does not exist using a 'try...except' takes a whopping 30 seconds(ish). There must be a faster way, surely?

I'd be very grateful if someone could give me some pointers please.

Thanks!

+2  A: 

Use FileExists. In Delphi 5, FileExists is built-in (in the SysUtils library)

I'm unsure if there's any way to "quickly" do it. If you notice the time windows take to resolve a network location, through Samba, when it doesn't exists, it takes about your 30 seconds. It has probably something to do with the internal timeout of the windows API for samba calls (just guessing).

Jorge Córdoba
Thanks, but I'm already using that. If the IP address I'm trying to get to cannot be found on the network, it still takes 30 second to check...
JamesW
That is exactly what I feared - thanks for responding. I'll wait a while and see if anyone else has any ideas.
JamesW
+2  A: 

For those of you interested in the answer for a Delphi context, I used the Indy component IdIcmpClient component to ping the IP, as follows:

  IdIcmpClient1.Host:= '10.0.0.999';
  try
    IdIcmpClient1.Ping();
  except
    showmessage('Not found');
  end;

You get a result in just over 3 seconds if it is not there, or almost instantly if it is.

JamesW
Careful that this may provide invalid information if ICMP is blocked somewhere between you and the destination.
Scott W
Do you use SuperSize bytes for addresses in your internal network? ;-)
mghie
Mick's answer quotes a message on the Embarcadero forums saying that the Indy Icmp component uses raw sockets, and thus it's only allowed to be used by administrators. If this worked for you, then either Indy has changed its implementation, or you're only testing as administrator. That's a bad idea, James.
Rob Kennedy
Thanks for the info, but it will be okay because the machine where it will be running will always be logged in with an administrator user - it's an old legacy system that just keeps on running! I realise that this won't work in all cases and I'm grateful that yourself and others are posting warnings about this, but I'm sure that it's the best solution for my current predicament. Thanks
JamesW
A: 

I ping the location:

Usage:

bPingSuccess := Ping(szServer, 5000);

unit uPing;

interface

uses
  Windows,
  SysUtils,
  Classes;

type
  TSunB = packed record
    s_b1, s_b2, s_b3, s_b4: byte;
  end;

  TSunW = packed record
    s_w1, s_w2: word;
  end;

  PIPAddr = ^TIPAddr;
  TIPAddr = record
    case integer of
      0: (S_un_b: TSunB);
      1: (S_un_w: TSunW);
      2: (S_addr: longword);
  end;

  IPAddr = TIPAddr;

  TIcmpCreateFile = function(): THandle; stdcall;
  TIcmpCloseHandle = function(icmpHandle: THandle): boolean; stdcall;
  TIcmpSendEcho = function(IcmpHandle: THandle; DestinationAddress: IPAddr; RequestData:
    Pointer; RequestSize: smallint; RequestOptions: pointer; ReplyBuffer: Pointer;
    ReplySize: DWORD; Timeout: DWORD): DWORD; stdcall;

const
  IcmpCreateFile: TIcmpCreateFile = nil;
  IcmpCloseHandle: TIcmpCloseHandle = nil;
  IcmpSendEcho: TIcmpSendEcho = nil;

function Ping(InetAddress: string; iTimeout: cardinal): boolean;

implementation

uses
  WinSock;

function Fetch(var AInput: string; const ADelim: string = ' ';
  const ADelete: boolean = True): string;
var
  iPos: integer;
begin
  if ADelim = #0 then
  begin
    // AnsiPos does not work with #0
    iPos := Pos(ADelim, AInput);
  end
  else
  begin
    iPos := Pos(ADelim, AInput);
  end;
  if iPos = 0 then
  begin
    Result := AInput;
    if ADelete then
    begin
      AInput := '';
    end;
  end
  else
  begin
    Result := Copy(AInput, 1, iPos - 1);
    if ADelete then
    begin
      Delete(AInput, 1, iPos + Length(ADelim) - 1);
    end;
  end;
end;

procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
var
  phe: PHostEnt;
  pac: PChar;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  try
    phe := GetHostByName(PChar(AIP));
    if Assigned(phe) then
    begin
      pac := phe^.h_addr_list^;
      if Assigned(pac) then
      begin
        with TIPAddr(AInAddr).S_un_b do
        begin
          s_b1 := byte(pac[0]);
          s_b2 := byte(pac[1]);
          s_b3 := byte(pac[2]);
          s_b4 := byte(pac[3]);
        end;
      end
      else
      begin
        raise Exception.Create('Error getting IP from HostName');
      end;
    end
    else
    begin
      raise Exception.Create('Error getting HostName');
    end;
  except
    FillChar(AInAddr, SizeOf(AInAddr), #0);
  end;
  WSACleanup;
end;

function Ping(InetAddress: string; iTimeout: cardinal): boolean;
var
  hIcmpDll: HMODULE;
  hIcmpFile: THandle;
  InAddr: IPAddr;
  DW: DWORD;
  rep: array[1..128] of byte;
begin
  Result := False;
  { load a library }
  hIcmpDll := LoadLibrary('icmp.dll');

  if (hIcmpDll = 0) then
  begin
    raise Exception.Create('icmp.dll library can not be loaded or not found. ' +
      SysErrorMessage(GetLastError));
  end;

  try
    { load an address of required procedure}
    @IcmpCreateFile := GetProcAddress(hIcmpDll, 'IcmpCreateFile');
    @IcmpSendEcho := GetProcAddress(hIcmpDll, 'IcmpSendEcho');
    @IcmpCloseHandle := GetProcAddress(hIcmpDll, 'IcmpCloseHandle');

    {if procedure is found in the dll}
    if Assigned(IcmpCreateFile) and Assigned(IcmpSendEcho) and Assigned(IcmpCloseHandle)
      then
    begin
      hIcmpFile := IcmpCreateFile;
      try
        if hIcmpFile = INVALID_HANDLE_VALUE then
          Exit;
        TranslateStringToTInAddr(InetAddress, InAddr);
        DW := IcmpSendEcho(hIcmpFile, InAddr, nil, 0, nil, @rep, 128, iTimeout); //0);
        Result := (DW <> 0);
      finally
        IcmpCloseHandle(hIcmpFile);
      end;
    end;
  finally
    {unload a library}
    FreeLibrary(hIcmpDll);
  end;

end;

end.
Mick
ping requires administrator rights - which might be missing...
mjustin
I don't believe that is correct. If you notice my code provided uses functions within icmp.dll to perform the ping (see IcmpSendEcho) for the sole reason of not requiring admin rights to do it. You only need to have admin rights when creating your own raw sockets. Please see here for more information: https://forums.embarcadero.com/message.jspa?messageID=50554
Mick
-1 for unnecessary untyped parameter, poor error and exception handling, and not using the MSDN-recommended way of loading the Icmp functions.
Rob Kennedy
What issue do you have with the exception handling? How would you improve it? Also, what is the MSDN recommended way of loading ICMP functions?
Mick
You catch *all* exceptions, and to fix whatever went wrong, you just fill a buffer with zeros and proceed as though everything's fine. MSDN: http://msdn.microsoft.com/en-us/library/aa366045.aspx
Rob Kennedy
I have update the code to (hopefully) resolve all the issues.
Mick