IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi Group Home

WEEKLYLESSON
Week 46 Lesson◄╣

MY
GROUPSETTINGS
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE
   ► All GroupsDelphi Group   Print This    All Groups  

Weekly Delphi Lesson

Week 46, Posted Monday 11/13/2017

Content changes every Monday @ 1AM!! Check back once per week. Learn! Review! Test Yourself!

Weekly Delphi Lesson

[Print Lesson]

Week 46, Posted Monday 11/13/2017

Content changes every Monday @ 1AM!! Check back once per week. Learn! Review! Test Yourself!

FAQ of the Week
Question:

Can I call a DotNet DLL from my Delphi Win32 application? For example, with Delphi 7 or Delphi 2010?



Answer:

It's not easy because the .NET RTL resides in a different process. You can wrap up the DotNet DLL into an ActiveX control and call it or have both your app and the DotNet DLL write to the registry, a file, etc. By the way, you CAN wrap up a Win32 DLL in a DotNet DLL and call it from DotNet.

Tip of the Week
Tip: Before adding many new entries to a TStringList, set its Sorted property to false. Add all your entries. Finally, set the Sorted property back to True (if desired). Sorting is expensive, in terms of macnine cycles. By leaving the Sorted property set to True, you force the TStingList to re-sort itself after each entry. This can significantly slow things down. This applies to TStrings, as well, and all components that have a TStrings property, like TListBox, etc.
Code Snippet of the Week
Application.ProcessMessages

I always found the sleep command in ObjectPAL very useful. The following code does about the same thing in Delphi. It makes use of GetTickCount which is a Win32 API call that retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

procedure TUtils.Delay(MillisecondsDelay: Integer);
var
   FirstTickCount: LongInt;
begin
   FirstTickCount := GetTickCount;
   repeat 
       Application.ProcessMessages;
   until ((GetTickCount-FirstTickCount) >= Longint(MillisecondsDelay));
end;
Definition of the Week
Strict Visibility:  The Strict specifier tightens up the scope for private and protected. With Strict Private, members within a class are visible ONLY within the class declared and Strict Protected are visible only within the class declared and descendent classes. Without strict, private and protected members can also be seen by all classes declared in the same unit (friendly classes). The Strict specifier was introduced with Delphi for .Net preview in Delphi 7 in an update to the .Net compiler preview and with Win32 in Delphi 2005 to fully comply with the .NET CLS. Delphi's traditional private specifier maps to the CLR's assembly visibility and the protected specifier maps to the CLR's assembly or family visibility.
Link of the Week
Dr. Bob 42 A popular, and charming, website containing the latest news, press releases, articles, book reviews, tools, third-party tool reviews, and news on conferences & training using Delphi for Win32, Delphi for .NET, Delphi for PHP, Kylix (Delphi for Linux), C++Builder (Delphi for C++) & C#Builder as well as ASP.NET, XML, SOAP and Web Services.
File of the Week

None This Week

Lastest Delphi Flashcard

Our flashcards are part of our library of tidbits.

Tidbits are FAQs, Definitions, Tips, and Code Snippets from our knowledge base.

BDE Paradox Robust table openning

This is a rough draft version of a procedure that provides a robust way to open paradox tables in a delphi program

unit DataModule;

interface

uses
  windows,SysUtils, Classes, DB, DBTables, controls, DateUtils, Dialogs, shellapi;

type
  TDataModule1 = class(TDataModule)
    Table1: TTable;

    ......

    ......

  public
    SauveTable: boolean;
  end;

......

.......

procedure TDataModule1.DataModuleCreate(Sender: TObject);
var
  DtBs : string;
  DirVers : string;
  DirDepuis: string;
  TableNam : string;
  st1,st2 : string;
  i,j,k : integer;
  TableTemp,
  TableSours : TTable;
  Transefrt : TBatchMove;
  res : boolean;
  f: TSearchRec;
  ListFichier : TStringList;
begin
SauveTable:=true;
DecimalSeparator:='.';
ThousandSeparator:=' ';

Transefrt:=TBatchMove.Create(Self);
TableTemp:=TTable.Create(Self);

for i:=0 to Self.ComponentCount-1 do
 if (self.Components[i] is TTable) and (self.Components[i]<>TableTemp) then
 begin
  if not TTable(Self.Components[i]).Exists then
    if Messagedlg('Erreurr Fatal: La table '+TTable(Self.Components[i]).TableName+' est manquante'#10#13+
                   'Voulez vous recuprer la table partir de la dernire sauveguarde',mtError ,mbOKCancel,0)=mrok
       then begin
          DirDepuis:=Sessions.FindSession(TTable(Self.Components[i]).SessionName).FindDatabase(TTable(Self.Components[i]).DatabaseName).Directory;
          if DirDepuis[length(DirDepuis)]='\' then DirDepuis:=copy(DirDepuis,1,length(DirDepuis)-1);
          ListFichier:=TStringList.Create;
          res:=false;
          if FindFirst(DirDepuis+'\sauv_*.*',faDirectory,f)=0
          then
              repeat
                    ListFichier.Add(f.name);
              until FindNext(f)<>0;
          FindClose(f);
          ListFichier.Sorted:=true;
          if ListFichier.Count<1
            then begin
              ShowMessage('Pas de sauvguard trouv?!! Veuillez contacter l''administrateur informatique');
              for k:=0 to Self.ComponentCount-1 do
                 if self.Components[k] is TTable
                   then if TTable(Self.Components[k]).Active
                            then TTable(self.Components[k]).close;
              ListFichier.Free;
              FormPrincipal.Close;
              exit;
            end;
          DirVers:=DirDepuis+'\'+ListFichier[ListFichier.count-1];
          ListFichier.Free;
          ListFichier:=TStringList.Create;
          res:=false;
          if FindFirst(DirVers+'\'
              +copy(TTable(Self.Components[i]).TableName,1,pos('.',TTable(Self.Components[i]).TableName)-1)+'.*',faAnyFile,f)=0
          then
              repeat
                    ListFichier.Add(f.name);
              until FindNext(f)<>0;
          FindClose(f);
          for j := ListFichier.Count - 1 downto 0
          do    CopyFile(Pchar(DirVers+'\' + ListFichier[j]),
                           Pchar(DirDepuis+'\' + ListFichier[j]),res);
          ListFichier.Free;
       end;

  try
    TTable(Self.Components[i]).Open;
    except
    DirDepuis:=Sessions.FindSession(TTable(Self.Components[i]).SessionName).FindDatabase(TTable(Self.Components[i]).DatabaseName).Directory;
    if DirDepuis[length(DirDepuis)]='\' then DirDepuis:=copy(DirDepuis,1,length(DirDepuis)-1);
    DirVers:=DirDepuis+'\Sauv_'+FormatDateTime('dd-mm-yyyy',Date)+'_'+FormatDateTime('hh-mm',time);
    if not DirectoryExists(DirVers)
     then CreateDir(DirVers);
    ListFichier:=TStringList.Create;
    res:=false;
    if FindFirst(DirDepuis+'\'
        +copy(TTable(Self.Components[i]).TableName,1,pos('.',TTable(Self.Components[i]).TableName)-1)+'.*',faAnyFile,f)=0
    then
        repeat
              ListFichier.Add(f.name);
        until FindNext(f)<>0;
    FindClose(f);
    for j := ListFichier.Count - 1 downto 0
    do    CopyFile(Pchar(DirDepuis+'\' + ListFichier[j]),
                     Pchar(DirVers+'\' + ListFichier[j]),res);
    DirVers:=DirDepuis+'\Rindx';
    if not DirectoryExists(DirVers)
     then CreateDir(DirVers);
    st1:=DirDepuis+'\'+TTable(Self.Components[i]).TableName;
    st2:=DirVers+'\'+TTable(Self.Components[i]).TableName;
    copyfile(pchar(st1),pchar(st2),res);
    if FileExists(DirDepuis+'\'+ChangeFileExt( TTable(Self.Components[i]).TableName,'.MB'))
      then begin
            st1:=DirDepuis+'\'+ChangeFileExt( TTable(Self.Components[i]).TableName,'.MB');
            st2:=DirVers+'\'+ChangeFileExt( TTable(Self.Components[i]).TableName,'.MB');
            copyfile(pchar(st1),pchar(st2),res);
      end;
    ListFichier.Free;
    st1:='-t '+DirDepuis+'\Rindx\'
                      + TTable(Self.Components[i]).TableName
                      + ' -AUTO -CLOSE -p amadou';
    ShellExecute(FormPrincipal.Handle,
                'open',
                'TableRepairCommand.exe',
                Pchar(st1),
                nil,
                1);

    tabletemp.DatabaseName:=DirDepuis+'\Rindx';
    TableTemp.TableName:=TTable(Self.Components[i]).TableName;
    TableTemp.SessionName:=TTable(Self.Components[i]).SessionName;
    if TTable(Self.Components[i]).Active then TTable(Self.Components[i]).Close;
    if TTable(Self.Components[i]).Exists
     then TTable(Self.Components[i]).DeleteTable;
    TTable(Self.Components[i]).CreateTable;
    TTable(Self.Components[i]).Open;
    TableTemp.Open;  //
    Transefrt.Source:=TableTemp;
    Transefrt.Destination:=TTable(Self.Components[i]);
    Transefrt.Mode:=batAppend;
    Transefrt.Execute;
    TableTemp.Close;
  end;
 end;
Transefrt.Destroy;
TableTemp.Destroy;
for i:=0 to Self.ComponentCount-1 do
   if self.Components[i] is TTable
     then if not TTable(Self.Components[i]).Active
              then TTable(self.Components[i]).Open;

SauveTable:=true;
end;

......

......

procedure TDataModule.DataModuleDestroy(Sender: TObject);

var
  DtBs : string;
  DirVers : string;
  DirDepuis: string;
  TableNam : string;
  st1,st2 : string;
  i,j,k,l : integer;
  TableTemp,
  TableSours : TTable;
  Transefrt : TBatchMove;
  res : boolean;
  f: TSearchRec;
  ListFichier : TStringList;
begin
if (Messagedlg('Voulez vous effectuer une sauvguarde?',mtConfirmation ,mbOKCancel,0)=mrok
)
   and (SauveTable)

  then begin
    for l:=0 to form1.Application.ComponentCount-1 do
     if (form1.Application.components[l] is TForm) or (form1.Application.components[l] is TDataModule)
      then begin
         for i:=0 to form1.Application.components[l].ComponentCount-1 do
            if (form1.Application.components[l].Components[i] is TTable) and (form1.Application.components[l].Components[i]<>TableTemp) then
             begin
                DirDepuis:=Sessions.FindSession(TTable(form1.Application.components[l].Components[i]).SessionName).FindDatabase(TTable(form1.Application.components[l].Components[i]).DatabaseName).Directory;
                if DirDepuis[length(DirDepuis)]='\' then DirDepuis:=copy(DirDepuis,1,length(DirDepuis)-1);
                DirVers:=DirDepuis+'\Sauv_'+FormatDateTime('yyyy-mm-dd',Date)+'_'+FormatDateTime('hh-mm',time);
                if not DirectoryExists(DirVers)
                 then CreateDir(DirVers);
                ListFichier:=TStringList.Create;
                res:=false;
                if FindFirst(DirDepuis+'\'
                    +copy(TTable(form1.Application.components[l].Components[i]).TableName,1,pos('.',TTable(form1.Application.components[l].Components[i]).TableName)-1)+'.*',faAnyFile,f)=0
                then
                    repeat
                          ListFichier.Add(f.name);
                    until FindNext(f)<>0;
                FindClose(f);
                for j := ListFichier.Count - 1 downto 0
                do    CopyFile(Pchar(DirDepuis+'\' + ListFichier[j]),
                                 Pchar(DirVers+'\' + ListFichier[j]),res);
                ListFichier.Free;
             end;
          end;
      end;
For i:=0 to Self.ComponentCount-1 do
  if self.Components[i] is TTable then TTable(Self.Components[i]).Close;
end;

Posted By Ahmed.A, Post #102273, KB Topic: BDE
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2017 Prestwood IT Solutions.   [Security & Privacy]