Posts Tagged ‘delphi’

is file a valid .NET assembly (CLR type)

Tuesday, May 26th, 2009

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

Wednesday, January 14th, 2009

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)

Thursday, December 4th, 2008

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

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

IsThirdPartyWorld

Friday, November 28th, 2008

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

Tuesday, October 28th, 2008

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

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

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

get default system charset

Tuesday, September 16th, 2008

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

Thursday, September 11th, 2008

{$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

Wednesday, September 10th, 2008

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)

Tuesday, September 2nd, 2008

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;

Delphi 2009

Monday, September 1st, 2008

The compiled exe/dll can’t be executed in Windows 95/98/ME because there is no unicode support.
The bad news because CodeGear could create the wrappers (from unicode->ansi functions) for this situation. Now they suggest to create these wrappers yourself and update the Windows.pas unit

Hope they will include the new unit with wrappers in next update for D2009