The Road to Delphi

Delphi – Free Pascal – Oxygene


4 Comments

Detect if an antivirus software is installed in Windows using C#

The next code shows how detect if an antivirus software is installed in Windows using C# and the WMI.

using System;
using System.Text;
using System.Management;

namespace ConsoleApplication1
{
  class Program
  {
    public static bool AntivirusInstalled()
    {

      string wmipathstr = @"\\" + Environment.MachineName + @"\root\SecurityCenter";
      try
      {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipathstr, "SELECT * FROM AntivirusProduct");
        ManagementObjectCollection instances = searcher.Get();
        return instances.Count > 0;
      }

      catch (Exception e)
      {
        Console.WriteLine(e.Message);
      }

      return false;
    } 

    public static void Main(string[] args)
    {
      bool returnCode = AntivirusInstalled();
      Console.WriteLine("Antivirus Installed " + returnCode.ToString());
      Console.WriteLine();
      Console.Read();
    }


  }
}


Leave a comment

Detect If my Delphi application is running under a 64-bit version of Windows.

The next code shows how detect If my Delphi application is running under a 64-bit version of Windows.

uses 
  Windows;

    function IsWow64Process: Boolean;
    type
      TIsWow64Process = function( hProcess: Windows.THandle; var Wow64Process: Windows.BOOL): Windows.BOOL; stdcall;
    var
      IsWow64Process: TIsWow64Process;
      Wow64Process  : Windows.BOOL;
    begin
      Result := False;
      IsWow64Process := GetProcAddress(GetModuleHandle(Windows.kernel32), 'IsWow64Process');
      if Assigned(IsWow64Process) then
      begin
        if not IsWow64Process(GetCurrentProcess, Wow64Process) then
        Raise Exception.Create('Invalid handle');
        Result := Wow64Process;
      end;
    end;


Leave a comment

Using Rijndael Algorithm in Delphi 2007. Net

The next code shows a sample of how use the Rijndael Algorithm in Delphi 2007. Net

uses
   System.Security.Cryptography, 
   System.Text;

type
  TDynamicArrayOfByte = array of Byte;

function Encrypt(StrtoEncrypt, PK: string): TDynamicArrayOfByte; // pk, must be of a string of 32 characters
var
   miRijndael:  Rijndael;
   encrypted:   TDynamicArrayOfByte;
   toEncrypt:   TDynamicArrayOfByte;
   bytPK:       TDynamicArrayOfByte;
   i: integer;
begin
   Result     := nil;
   miRijndael := System.Security.Cryptography.RijndaelManaged.Create;
   try
    toEncrypt :=  System.Text.Encoding.UTF8.GetBytes(StrtoEncrypt);
    bytPK     :=  System.Text.Encoding.UTF8.GetBytes(PK);    
    miRijndael.Key := bytPK;
    miRijndael.GenerateIV;
    encrypted := (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt, 0, Length(toEncrypt));
    setlength(result, Length(miRijndael.IV) + Length(encrypted));

      for i:=0 to Length(miRijndael.IV)-1 do
         result[i] := miRijndael.IV[i];

      for i:=0 to Length(encrypted)-1 do
         result[i + Length(miRijndael.IV)] := encrypted[i];

   finally
      miRijndael.Clear();
   end;
end;

function DesEncrypt(BufferEncrypted: TDynamicArrayOfByte; PK: string): string; //   pk, must be of a string of 32 characters
var
   miRijndael:  Rijndael;
   encrypted:   TDynamicArrayOfByte;
   tempArray:   TDynamicArrayOfByte;
   bytPK:       TDynamicArrayOfByte;
   i : integer;
begin
   Result     := '';
   miRijndael := System.Security.Cryptography.RijndaelManaged.Create;
   setlength(tempArray, Length(miRijndael.IV));
   setlength(encrypted, Length(BufferEncrypted) - Length(miRijndael.IV));
   try
    bytPK     :=  System.Text.Encoding.UTF8.GetBytes(PK);
    miRijndael.Key :=  bytPK;

      for i:=0 to Length(tempArray)-1 do
         tempArray[i] := BufferEncrypted[i];

      for i:=0 to Length(encrypted)-1 do
         encrypted[i] := BufferEncrypted[i + Length(tempArray)];

    miRijndael.IV := tempArray;
    Result :=  System.Text.Encoding.UTF8.GetString((miRijndael.CreateDecryptor()).TransformFinalBlock(encrypted, 0, Length(encrypted)));
   finally
     miRijndael.Clear();
   end;
end;


Leave a comment

How to mark all the instances of a word in a TRichEdit using Delphi

the Nect code shows how to mark all the instances of a word in a TRichEdit using Delphi


procedure MarkString(RichEdit:TRichEdit;const strtomark : string);
Var
FoundAt : integer;
begin
    FoundAt:=RichEdit.FindText(strtomark,0,maxInt,[stWholeWord]);
    while FoundAt <> -1 do
    begin
             RichEdit.SelStart := FoundAt;
             RichEdit.SelLength := Length(strtomark);
             RichEdit.SelAttributes.Style := [fsBold];
             RichEdit.SelAttributes.Color := clRed;
             RichEdit.SelText :=strtomark;
             FoundAt:=RichEdit.FindText(strtomark,FoundAt + length(strtomark),maxInt,[stWholeWord]);
    end;
end;


procedure UnMarkString(RichEdit:TRichEdit;const strtomark : string);
Var
FoundAt : integer;
begin
    FoundAt:=RichEdit.FindText(strtomark,0,maxInt,[stWholeWord]);
    while FoundAt <> -1 do
    begin
             RichEdit.SelStart := FoundAt;
             RichEdit.SelLength := Length(strtomark);
             RichEdit.SelAttributes.Style := [];
             RichEdit.SelAttributes.Color := clBlack;
             RichEdit.SelText :=strtomark;
             FoundAt:=RichEdit.FindText(strtomark,FoundAt + length(strtomark),maxInt,[stWholeWord]);
    end;
end;


MarkString(RichEdit1,'delphi'); //To Mark a string

UnMarkString(RichEdit1,'delphi'); //To UnMark a string



3 Comments

List of changes between versions of Delphi (What’s New)

In response to this question asked in stackoverflow leave here a list of changes between versions of Delphi.
Bye.