Delphi function to check a string if it's number


function IsNumber(Text: String): Boolean;
var i: Integer;
    c: char;
begin
  Result := True;
  if Length(Text) <= 0 then
    Result := False;
  for i := 1 to Length(Text) do
  begin
    c := Text[i];
    if not ((c >= '0') and (c <= '9')) then
    if not ((c = DecimalSeparator) and (i > 1)) then
    if not ((c = '-') and (i = 1)) then
    if not ((c = '+') and (i = 1)) then
    begin
      Result := False;
      //Break;
    end;
  end;
end;

Extract integer part of a string - Delphi function


function ExtractIntegerPartOfString(const AValue: String; var StrPart: String; var IntPart: Integer): String;
var
  I: Integer;
begin
  Result := '0';
  StrPart := '';
  IntPart := 1;
  for I := length(AValue)-1 downto 0 do
  begin
    if not (Ord(AValue[I]) in [Ord('0')..Ord('9')]) then
    begin
      if Length(AValue)-I > 0 then
      begin
        StrPart := LeftStr(AValue,I);
        try
          IntPart := StrToInt(RightStr(AValue,Length(AValue)-I))+1;
        except
          IntPart := 1;
          StrPart := LeftStr(AValue,I+1);
        end;
        Result := FormatFloat(Copy('000000000000',1,Length(AValue)-I),IntPart);
      end;
      Break;
    end;
  end;
end;

Delphi function for sending an email using MAPI

uses
  MAPI;

function SendMailMAPI(const Subject, Body, FileName, SenderName, SenderEMail,
                  RecepientName, RecepientEMail: String) : Integer;
var
  message: TMapiMessage;
  lpSender,
  lpRecepient: TMapiRecipDesc;
  FileAttach: TMapiFileDesc;
  SM: TFNMapiSendMail;
  MAPIModule: HModule;
begin
  FillChar(message, SizeOf(message), 0);
  with message do
  begin
    if (Subject<>'') then
    begin
      lpszSubject := PChar(Subject)
    end;
    if (Body<>'') then
    begin
      lpszNoteText := PChar(Body)
    end;
    if (SenderEMail<>'') then
    begin
      lpSender.ulRecipClass := MAPI_ORIG;
      if (SenderName='') then
      begin
        lpSender.lpszName := PChar(SenderEMail)
      end
      else
      begin
        lpSender.lpszName := PChar(SenderName)
      end;
      lpSender.lpszAddress := PChar('SMTP:'+SenderEMail);
      lpSender.ulReserved := 0;
      lpSender.ulEIDSize := 0;
      lpSender.lpEntryID := nil;
      lpOriginator := @lpSender;
    end;
    if (RecepientEMail<>'') then
    begin
      lpRecepient.ulRecipClass := MAPI_TO;
      if (RecepientName='') then
      begin
        lpRecepient.lpszName := PChar(RecepientEMail)
      end
      else
      begin
        lpRecepient.lpszName := PChar(RecepientName)
      end;
      lpRecepient.lpszAddress := PChar('SMTP:'+RecepientEMail);
      lpRecepient.ulReserved := 0;
      lpRecepient.ulEIDSize := 0;
      lpRecepient.lpEntryID := nil;
      nRecipCount := 1;
      lpRecips := @lpRecepient;
    end
    else
    begin
      lpRecips := nil
    end;
    if (FileName='') then
    begin
      nFileCount := 0;
      lpFiles := nil;
    end
    else
    begin
      FillChar(FileAttach, SizeOf(FileAttach), 0);
      FileAttach.nPosition := Cardinal($FFFFFFFF);
      FileAttach.lpszPathName := PChar(FileName);
      nFileCount := 1;
      lpFiles := @FileAttach;
    end;
  end;
  MAPIModule := LoadLibrary(PChar(MAPIDLL));
  if MAPIModule=0 then
  begin
    Result := -1
  end
  else
  begin
    try
      @SM := GetProcAddress(MAPIModule, 'MAPISendMail');
      if @SM<>nil then
      begin
        Result := SM(0, Application.Handle, message, MAPI_DIALOG or
                     MAPI_LOGON_UI, 0);
      end
      else
      begin
        Result := 1
      end;

    finally
      FreeLibrary(MAPIModule);
    end;
  end
  if Result<>0 then
  begin
    MessageDlg('Error sending mail ('+IntToStr(Result)+').', mtError, [mbOk],
               0)
  end;
end;

Delphi function to Detect if file is ReadOnly


function IsReadOnly(const FileName: string): Boolean;
var
  sr: TSearchRec;
begin
  (* Assume not read only *)
  Result := False;

  if FindFirst(FileName, faAnyFile, sr) = 0 then
  begin
    Result := (sr.Attr and faReadOnly) <> 0;
    FindClose(sr);
  end;
end;

Restrict an Delphi application to only one instance


procedure EnsureSingleInstance;
var
  Wnd: HWnd;
  WndClass, WndText: array[0..255] of char;
begin
{$ifdef Win32}
  { Try and create a semaphore. If we succeed, then check }
  { if the semaphore was already present. If it was }
  { then a previous instance is floating around. }
  { Note the OS will free the returned semaphore handle }
  { when the app shuts so we can forget about it }
  if (CreateSemaphore(nil, 0, 1,
        PChar(ExtractFileName(Application.ExeName))) <> 0) and
     (GetLastError = Error_Already_Exists) then
{$else}
  if HPrevInst <> 0 then
{$endif}
  begin
    Wnd := GetWindow(Application.Handle, gw_HWndFirst);
    while Wnd <> 0 do
    begin
      { Look for the other TApplication window out there }
      if Wnd <> Application.Handle then
      begin
        { Check it's definitely got the same class and caption }
        GetClassName(Wnd, WndClass, Pred(SizeOf(WndClass)));
        GetWindowText(Wnd, WndText, Succ(Length(Application.Title)));
        if (StrPas(WndClass) = Application.ClassName) and
           (StrPas(WndText) = Application.Title) then
        begin
          { This technique is used by the VCL: post }
          { a message then bring the window to the }
          { top, before the message gets processed }
          PostMessage(Wnd, wm_SysCommand, sc_Restore, 0);
{$ifdef Win32}
          SetForegroundWindow(Wnd);
{$else}
          BringWindowToTop(Wnd);
{$endif}
          Halt
        end
      end;
      Wnd := GetWindow(Wnd, gw_HWndNext)
    end
  end
end;