Tag: delphi

  • to check the password quality (strong or not strong)

    {returns an integer value (0 to 100) rating the key quality}
    function PasswordQuality(const strPassword: string): Integer;
    var
      i, j, intLen: Integer;
      QC: Double;
      boolUpper, boolLower: Boolean;
    begin
      Result := 0;

      intLen := Length(strPassword);
      QC := 4*intLen;

      {at least 5 characters in password required}
      if (intLen > 4) then
      begin
        {check for repetitions (abcabc, aaaaa, 121212, etc}
        for i := 1 to (intLen div 2) do
        begin
          j := i+1;
          while (j <= intLen ) do
          begin
            if (Copy(strPassword, 1, i) = Copy(strPassword, j, i)) then exit;
            Inc(j, i);
          end
        end;

        {check the upper/lower cases}
        boolUpper := False;
        boolLower := False;
        for i := 1 to intLen do
        begin
          if (strPassword[i] in [‘A’..’Z’]) then
            boolUpper := True;
          if (strPassword[i] in [‘a’..’z’]) then
            boolLower := True;
        end;
        if boolUpper and boolLower then
          QC := 1.5*QC;

        {check the numbers}
        for i := 1 to intLen do
        begin
          if (strPassword[i] in [‘0’..’9′]) then
            if (boolUpper or boolLower) then
              QC := 1.5*QC;
        end;

        {check the signs}
        for i := 1 to intLen do
        begin
          if (strPassword[i] < ‘0’) or (strPassword[i] > ‘z’) or
             ((strPassword[i] > ‘9’) and (strPassword[i] < ‘A’)) then
            QC := 1.5*QC;
        end;

        if (QC > 100) then
          QC := 100;
        Result := Trunc(QC);
      end;
    end;

    Sample to use:
      i := PasswordQuality(edPassword.Text);
      lblPWQuality.Caption := IntToStr(i);
      if (i < 30) then
        lblPWQuality.Font.Color := clRed
      else
      if (i < 60) then
        lblPWQuality.Font.Color := clYellow
      else
        lblPWQuality.Font.Color := clWindowText;

  • is file a valid .NET assembly (CLR type)

    Returns true if the file specified is a real CLR type, otherwise false is returned.

    function IsDotNetAssembly(const AFileName: string): Boolean;
    var
      fs: TFileStream;
      peHeader, peHeaderSignature,
      timestamp, pSymbolTable, noOfSymbol: dWord;
      machine, sections, optionalHeaderSize, characteristics: Word;
      dataDictionaryRVA: array[0..14] of dWord;
      dataDictionarySize: array[0..14] of dWord;
      i: Integer;
    begin
      fs := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
      try
        //PE Header starts @ 0x3C (60). Its a 4 byte header.
        fs.Seek($3C, soFromBeginning);
        fs.Read(peHeader, SizeOf(peHeader));
        //Moving to PE Header start location...
        fs.Position := peHeader;
        fs.Read(peHeaderSignature, SizeOf(peHeaderSignature));
        //We can also show all these value, but we will be
        //limiting to the CLI header test.
        fs.Read(machine, SizeOf(machine));
        fs.Read(sections, SizeOf(sections));
        fs.Read(timestamp, SizeOf(timestamp));
        fs.Read(pSymbolTable, SizeOf(pSymbolTable));
        fs.Read(noOfSymbol, SizeOf(noOfSymbol));
        fs.Read(optionalHeaderSize, SizeOf(optionalHeaderSize));
        fs.Read(characteristics, SizeOf(characteristics));
    
        { Now we are at the end of the PE Header and from here, the
        PE Optional Headers starts...
        To go directly to the datadictionary, we'll increase the
        stream's current position to with 96 (0x60). 96 because,
        28 for Standard fields
        68 for NT-specific fields
        From here DataDictionary starts...and its of total 128 bytes.
        DataDictionay has 16 directories in total,
        doing simple maths 128/16 = 8.
        So each directory is of 8 bytes.
    
        In this 8 bytes, 4 bytes is of RVA and 4 bytes of Size.
        btw, the 15th directory consist of CLR header! if its 0,
        it is not a CLR file }
    
        fs.Seek($60, soFromCurrent);
        for i := 0 to 14 do
        begin
          fs.Read(dataDictionaryRVA[i], SizeOf(dataDictionaryRVA[i]));
          fs.Read(dataDictionarySize[i], SizeOf(dataDictionarySize[i]));
        end;
      finally
        fs.Free
      end;
      Result := (dataDictionaryRVA[14] <> 0)
    end;
    

    Created in Delphi from http://www.codeproject.com/KB/cs/AutoDiagrammer.aspx

  • faq generator

    Yesterday I changed the generator for html-pages with Delphi tips
    Now the See Also section is not fixed and generated with random items (no more than 10 items per page)

    Also I added the abadev products as available items in See Also section

    PS: all online tips updated now. I re-uploaded all html-pages there

  • parser for Skype log-files (.dbb)

    New TSMImportFromSkype component added in SMImport suite
    The log files (.dbb) processed.

    PS: not all tag/record processed but most popular are there

  • IsThirdPartyWorld

    function IsThirdPartyWorld(const Country: string): Boolean;
    begin
      if (Country = ‘USA’) or
         (Country = ‘AU’) or
         (Country = ‘AT’) or
         (Country = ‘BE’) or
         (Country = ‘CA’) or
         (Country = ‘DK’) or
         (Country = ‘FI’) or
         (Country = ‘FR’) or
         (Country = ‘DE’) or
         (Country = ‘IS’) or
         (Country = ‘IE’) or
         (Country = ‘IT’) or
         (Country = ‘JP’) or
         (Country = ‘NL’) or
         (Country = ‘NZ’) or
         (Country = ‘NO’) or
         (Country = ‘PT’) or
         (Country = ‘ES’) or
         (Country = ‘SE’) or
         (Country = ‘CH’) or
         (Country = ‘US’) or
         (Country = ‘UK’) then
        Result := False
      else
        Result := True;
    end;

  • Delphi Prism

    CodeGear выпускает Delphi Prism для использования синтаксиса Pascal в VS Studio .NET

    http://www.codegear.com/products/delphi/prism

    Насколько я понимаю, это то, что раньше называлось Chrome от RemObjects. Тем более, что даде в анонсе сказано про использование их компилятора RemObjects Oxygene.

  • get default system charset

    function TranslateCharsetInfo(lpSrc: integer;
      var lpCs: TCharsetInfo; dwFlags: DWORD): BOOL; stdcall;
      external ‘gdi32.dll’ name ‘TranslateCharsetInfo’;

    function GetDefaultCharset: TFontCharset;
    var
      dwCodePage: dWord;
      CharSetInfo: TCharSetInfo;
    begin
      {get default codepage}
      dwCodePage := StrToIntDef(GetLocaleStr(GetUserDefaultLCID, LOCALE_IDEFAULTANSICODEPAGE, ”), 0);
      if dwCodePage = 0 then
        dwCodePage := StrToIntDef(GetLocaleStr(GetSystemDefaultLCID, LOCALE_IDEFAULTANSICODEPAGE, ”), GetACP());
      {convert codepage into charset}
      if TranslateCharsetInfo(dwCodePage, CharSetInfo, TCI_SRCCODEPAGE) then
        Result := CharSetInfo.ciCharset
      else
        Result := 0
    end;

  • directives for D2007 complier

    {$IFDEF VER200}
      {$DEFINE SMForDelphi3}
      {$DEFINE SMForDelphi4}
      {$DEFINE SMForDelphi5}
      {$DEFINE SMForDelphi6}
      {$DEFINE SMForDelphi7}
      {$DEFINE SMForDelphi2005}
      {$DEFINE SMForDelphi2006}
      {$IFDEF BCB}
        {$DEFINE SMForBCB2006}
        {$DEFINE SMForBCB2007}
        {$DEFINE SMForBCB2009}
      {$ENDIF}
      {$DEFINE SMForDelphi2007}
      {$DEFINE SMForRADStudio2007}
      {$DEFINE SMForDelphi2009}
    {$ENDIF}

  • TSMReport component: load/save from streams

    the TSMReport component now have the LoadReportFromStream and SaveReportToStream methods
    Using these methods you may load/save the reports in any stream, including BLOB-streams for field in any dataset or implement the packed/encoded streams (zip/ZLib, for example)

  • pack table (Paradox, dBase)

    If you use the TTable component in Delphi then to find the code to pack the table (to remove the records which are marked as deleted) is not a problem. For example:
    http://www.swissdelphicenter.ch/torry/showcode.php?id=35

    But how to pack the table using pure BDE API? What if you do not want to use the TTable/TDatabase components?

    Today I wrote the next code for this task:

    uses BDE; 
    
    procedure TfrmMain.piPackTableClick(Sender: TObject); 
    
      procedure DbiError(ErrorCode: DBIResult); 
      begin 
        raise Exception.CreateFmt('BDE Error Code: %x', [ErrorCode]); 
      end; 
    
      procedure Check(Status: DBIResult); 
      begin 
        if Status <> 0 then DbiError(Status); 
      end; 
    
      function StrToOem(const AnsiStr: string): string; 
      begin 
        SetLength(Result, Length(AnsiStr)); 
        if Length(Result) > 0 then 
          CharToOem(PChar(AnsiStr), PChar(Result)); 
      end; 
    
    var 
      hDatabase: hDbiDb; 
      hTableCursor: hDbiCur; 
      TableDesc: CRTblDesc; 
    begin 
      { check if BDE installed } 
      Check(dbiInit(nil));  
    
      { Open a database session with exclusive read/write access } 
      Check(DbiOpenDatabase('',  
                            nil, // database type (Standard) 
                            dbiReadWrite, // open mode (versus read-only) 
                            dbiOpenExcl, // exclusive (versus shared) 
                            nil, // database login password 
                            0, nil, nil, 
                            hDatabase)); 
      try 
        Check(DbiSetDirectory(hDatabase, Pointer(StrToOem(ExtractFilePath(cbFileName.Text)))));  
    
        { Open the table, returning its cursor handle } 
        Check(DbiOpenTable(hDatabase, 
              PChar(ExtractFileName(cbFileName.Text)), 
      {$IFDEF DB_PARADOX} 
              szParadox, 
      {$ENDIF}  
    
      {$IFDEF DB_DBASE} 
              szDBASE, 
      {$ENDIF} 
    
              nil, nil, 0, // no result index required 
              dbiReadOnly, dbiOpenShared, 
              xltField, // use logical field types (normal) 
              False, nil, 
              hTableCursor)); 
    
      {$IFDEF DB_PARADOX} 
        FillChar(TableDesc, SizeOf(CRTblDesc), #0); 
        with TableDesc do 
        begin 
          StrCopy(szTblName, PChar(ExtractFileName(cbFileName.Text))); 
          StrCopy(szTblType, szParadox); 
          bPack := True; 
        end; 
        Check(DbiCloseCursor(hTableCursor)); 
        Check(DbiDoRestructure(hDatabase, 1, @TableDesc, nil, nil, nil, False)); 
      {$ENDIF}  
    
      {$IFDEF DB_DBASE} 
        Check(DbiPackTable(hDatabase, hTableCursor, nil, szDBASE, True)) 
        Check(DbiCloseCursor(hTableCursor)); 
      {$ENDIF} 
      finally 
        Check(DbiCloseDatabase(hDatabase)); 
      end; 
    end;