I would like to know how to save the contents of a "variable" after program is closed and reopened.
for eg:
iCount:=0;
inc(iCount)=1;
when i close the program and reopen i want iCount to contain 1.
Thank you.
There are many ways to do this. You need to save the value somewhere: in a file, in the Windows registry, in the cloud, ...
File
Perhaps the easiest approach is to use an INI file. Try this:
Create a new VCL application.
Add a field FMyNumber: Integer to the main form.
To the main form, add the following methods (and make sure to include IniFiles and IOUtils in the implementation section's uses list):
function TForm1.GetSettingsFileName: TFileName;
begin
Result := TPath.GetHomePath + '\Fuzail\TestApp';
ForceDirectories(Result);
Result := Result + '\settings.ini';
end;
procedure TForm1.LoadSettings;
var
Ini: TMemIniFile;
begin
Ini := TMemIniFile.Create(GetSettingsFileName);
try
FMyNumber := Ini.ReadInteger('Settings', 'MyNumber', 0);
finally
Ini.Free;
end;
end;
procedure TForm1.SaveSettings;
var
Ini: TMemIniFile;
begin
Ini := TMemIniFile.Create(GetSettingsFileName);
try
Ini.WriteInteger('Settings', 'MyNumber', FMyNumber);
Ini.UpdateFile;
finally
Ini.Free;
end;
end;
Now make sure to call these when your application is starting and shutting down:
procedure TForm1.FormCreate(Sender: TObject);
begin
LoadSettings;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SaveSettings;
end;
Now the value of FMyNumber is saved between the sessions!
Registry
Another common approach, probably, is to use the registry. Try this:
Create a new VCL application.
Add a field FMyNumber: Integer to the main form.
To the main form, add the following methods (and make sure to include Registry in the implementation section's uses list):
procedure TForm1.LoadSettings;
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Fuzail\TestApp', False) then
try
if Reg.ValueExists('MyNumber') then
FMyNumber := Reg.ReadInteger('MyNumber')
finally
Reg.CloseKey;
end;
finally
Reg.Free;
end;
end;
procedure TForm1.SaveSettings;
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Fuzail\TestApp', True) then
try
Reg.WriteInteger('MyNumber', FMyNumber);
finally
Reg.CloseKey;
end;
finally
Reg.Free;
end;
end;
Now make sure to call these when your application is starting and shutting down:
procedure TForm1.FormCreate(Sender: TObject);
begin
LoadSettings;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SaveSettings;
end;
Again the value of FMyNumber is saved between the sessions!
Related
I have ported an application from ADO to FireDAC applying several RegExp replaces on the source code to convert the ADOQuery, ADOTables, ADOCommands, ADOStoredProcs, etc. ... to the corresponding FireDAC components.
It has worked fine, but now when running that application plenty of forms raise errors because of the type of the persistent fields being different than the type expected (the one defined from ADO when the persistent field was created).
I'm trying to make a list of those errors, creating an instance of all my forms and opening their datasets with persistent fields, and logging the errors. I can get the list of forms from the project source code, but when I try to use FindClass to create each form I get an error telling that the class has not been found.
Is there any other way to create a Form/DataModule from its class name ?.
This is my current code:
class procedure TfrmCheckFormularis.CheckDatasets(ProjecteFile: string);
var frmCheckFormularis: TfrmCheckFormularis;
Projecte: string;
rm: TMatch;
cc: TComponentClass;
c: TComponent;
i: integer;
Dataset: TFDQuery;
begin
Projecte := TFile.ReadAllText(ProjecteFile);
frmCheckFormularis := TfrmCheckFormularis.Create(Application);
try
with frmCheckFormularis do begin
Show;
qryForms.CreateDataSet;
qryErrors.CreateDataSet;
// I get a list of all the forms and datamodules on my project
for rm in TRegEx.Matches(Projecte, '^(?:.* in '')(?<File>.*)(?:'' {)(?<Class>.*)(?:},)', [roMultiline]) do begin
qryForms.AppendRecord([rm.Groups['File'].Value, rm.Groups['Class'].Value]);
end;
// Check every form and datamodule
qryForms.First;
while not qryForms.Eof do begin
cc := TComponentClass(FindClass(qryFormsClass.Value));
c := cc.Create(frmCheckFormularis);
try
for i := 0 to c.ComponentCount - 1 do begin
if c.Components[i] is TFDQuery then begin
Dataset := c.Components[i] as TFDQuery;
// When the Dataset has persistent fields, I open it to check if the persistent fields are correct
if Dataset.FieldDefs.Count > 1 then begin
try
Dataset.Open;
except
on E: Exception do qryErrors.AppendRecord([c.Name, Dataset.Name, E.Message]);
end;
end;
end;
end;
finally
c.Free;
end;
qryForms.Next;
end;
end;
finally
frmCheckFormularis.Free;
end;
end;
Thank you.
Using the "new" RTTI in Delphi is quite easy. The following code will (hopefully*) create one instance of each form in your application:
procedure TForm1.Button1Click(Sender: TObject);
var
Context: TRttiContext;
&Type: TRttiType;
InstanceType: TRttiInstanceType;
begin
Context := TRttiContext.Create;
for &Type in Context.GetTypes do
begin
if (&Type.TypeKind = tkClass) and &Type.IsInstance then
begin
InstanceType := TRttiInstanceType(&Type);
if InstanceType.MetaclassType.InheritsFrom(TForm) and (InstanceType.MetaclassType <> TForm) then
TFormClass(InstanceType.MetaclassType).Create(Application){.Show}; // optionally show it
end;
end;
end;
* Technically, it will create one instance of each proper descendant class of TForm.
I have components:
1 OpenPictureDialog (to open picture)
2 Edit (edtID & edtName)
1 Button (to save record)
I used UIBQuery to insert a new record, including picture in one section.
Here is my code:
with UIBQuery1 do
try
SQL.Clear;
SQL.Add('INSERT INTO EMPLOYEE');
SQL.Add('(ID, NAME, PIC)');
SQL.Add('VALUES');
SQL.Add('(:ID, :NAME, :PIC)');
params.AsInteger[0] := StrToInt(edtID.Text);
params.AsString[1] := edtName.Text;
// How to give a param for blob here?
Execute;
Transaction.Commit;
bsSkinMessage1.MessageDlg2('Has been saved.','New Record',mtInformation,[mbok],0);
except
Transaction.RollBack;
raise;
end;
I succeed show the record including picture from database, but I have no clue to store the picture into database in one click.
How to doing this? Is it possible to give parameter of picture in query?
Someone knows about UIB please..
I change my code but I get an access violation:
procedure TchfEmployee.btnSaveClick(Sender: TObject);
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
Image2.Picture.Bitmap.SaveToStream(ms);
ms.Position:=0;
begin
with UIBQuery1 do
try
SQL.Clear;
SQL.Add('INSERT INTO EMPLOYEE');
SQL.Add('(ID, NAME, PIC)');
SQL.Add('VALUES');
SQL.Add('(:ID, :NAME, :PIC)');
params.AsString[0] := edtID.Text;
params.AsString[1] := edtName.Text;
TBLOBField(Params.ByNameAsString['PIC']).LoadFromStream(ms);
Execute;
Transaction.Commit;
bsSkinMessage1.MessageDlg2('Has been saved.','New Record',mtInformation,[mbok],0);
except
Transaction.RollBack;
raise;
end;
try
UIBDataSet1.Close;
UIBDataSet1.Open;
except
raise;
end;
end;
end;
How's exactly save my TImage to BLOB using UIBQuery? I am really in a heavy stucked.
Try this:
...
ParamsSetBlob('PIC', ms);
Execute;
...
Take a look at UIB source code. Look for TUIBStatement class.
You need to do the following :-
Var
lStream : TMemoryStream;
Begin
lStream := TMemoryStream.Create;
Try
Image2.Picture.SaveToStream(lStream);
lStream.Position := 0;
Query.ParamSetBlob('Pic', lStream);
Finally
FreeAndNil(lStream);
End;
End;
Good day, I write plugins dll
from the main form call the dll
type
TCreateCustomWindow=function(ParentFrame:TWinControl; ParentHandle:integer; ParentRect:TRect; var WinHandle:THandle):integer; stdcall;
var
CreateW:TCreateCustomWindow;
begin
CreateW:=GetProcAddress(FHLib,'Create_LEF');
if Assigned(CreateW) then
begin
if Assigned(CreateW) then LEFT_OKNO:=CreateW(ScrollBox2, ScrollBox2.Handle, ClientRect, FChildHandle);
end;
in the dll itself, it looks like
function Create_LEF(ParentFrame:TWinControl; ParentHandle:integer; ParentRect:TRect; var WinHandle:THandle):integer; stdcall; export;
begin
Result:=0;
WinHandle:=0;
try
FD3:=TForm3.Create(nil);
FD3.Parent:= ParentFrame;
Result:=integer(FD3);
WinHandle:=FD3.Handle;
if ParentHandle<>0 then begin
SetParent(WinHandle,ParentHandle);
with FD3 do begin
FD3.Align:=alTop;
FD3.Width:=ParentFrame.Width;
hirina_left:=ParentFrame.Width;
FD3.Show;
end;
end;
except
On E:exception do MessageDlg(E.Message,mtError,[mbOK],0);
end;
end;
the problem is that I can not edit cells cxGrid can I do something wrong?
I have encountered this before, and there are a couple of ways around it. It was a long time ago, so you will have to do a bit of trial and error.
function Create_LEF(ParentFrame:TWinControl; ParentHandle:integer; ParentRect:TRect; var WinHandle:THandle):integer; stdcall; export;
begin
Result:=0;
WinHandle:=0;
try
FD3:=TForm3.Create(nil);
FD3.Parent:= ParentFrame;
Result:=integer(FD3);
WinHandle:=FD3.Handle;
if ParentHandle<>0 then begin
with FD3 do begin
ParentWindow := ParentFrame.Handle;
Parent := ParentFrame;
Align:=alTop;
Width:=ParentFrame.Width;
hirina_left:=ParentFrame.Width;
Show;
end;
end;
except
On E:exception do MessageDlg(E.Message,mtError,[mbOK],0);
end;
end;
That should fix your problem. Failing that, try setting DLL's Application.Handle to the application's Application.Handle. I usually do this with an Init function in the DLL. This function stores the the DLL's Application.Handle in a global variable and reassigns it to the application's handle, passed as a parameter to the function. When you unload the DLL, you assign the DLL's application.handle back to its original value, otherwise everything goes South.
var
FOldHandle: THandle;
procedure Init(AHandle: THandle); stdcall;
begin
FOldHandle := Application.Handle;
Application.Handle := AHandle;
end;
procedure UnInit; stdcall;
begin
Application.Handle := FOldHandle;
end;
...
I need to know if my program can write files to the disk (HDD, floppy, flash, CD) from where it is running.
I have tried something like:
{$I-}
Rewrite(myFile);
{$I+}
Result:= (IOResult = 0);
The problem is that if the disk is read-only, Windows gives me an error message telling me that
"appName.exe - Write Protect Error The disk cannot be written to because it is write protected. Please remove the write protection from the volume
USB_BOOT in drive D:. Cancel TryAgain Continue"
How can I test for write access without raising any error/warning messages?
Thanks.
Edit:
Ok. The "bug" has nothing to do with the above mentioned piece of code. I only thought that it appears there. I move the code to the read-only disk and ran it from there. The bug appears in a function called "CreateShortcutEx", on this line:
MyPFile.Save(PWChar(WFileName),False);
MyPFile is declared like this:
var
MyObject : IUnknown;
MySLink : IShellLink;
MyPFile : IPersistFile;
Directory : String;
WFileName : WideString;
MyReg : TRegIniFile;
begin
MyObject := CreateComObject(CLSID_ShellLink);
MySLink := MyObject as IShellLink;
MyPFile := MyObject as IPersistFile;
.....
end;
So, why is MyPFile trying to write to the application's drive (the one that is read-only) if the WFileName parameter is "C:\documents and settings\bla bla" ?
Call the Windows API SetErrorMode() function to disable the error message box.
Edit:
I just tried, and this:
procedure TForm1.Button1Click(Sender: TObject);
var
Str: TStream;
OldMode: Cardinal;
begin
OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
try
Str := TFileStream.Create('z:\foo.txt', fmOpenReadWrite);
try
finally
Str.Free;
end;
except end;
finally
SetErrorMode(OldMode);
end;
end;
works as expected.
Not really pretty but this seems to work for me.
function CanWrite(drive: string): boolean;
var
OldMode: Cardinal;
begin
OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
with TStringList.Create do
try
SaveToFile(drive + ':\file.txt');
result := true;
try
deletefile(drive + ':\file.txt');
except
end;
except
result := false;
end;
finally
SetErrorMode(OldMode);
end;
end;
Call to it with
if CanWrite('g') = true then
begin
showmessage('Can Write');
end
else
begin
showmessage('Can not write');
end;
What happens when you put your code inside an try/except?
Also, you can try (something like) this:
function CanWrite: boolean;
begin
result := true;
with TStringList.Create do
try
SaveToFile('file.txt');
except
result := false;
finally
Free;
end;
end;
Sorry, but I don't code in Delphi anymore and I don't have Delphi installed anywhere.
There exist a small freeware "Drive ready?" utility (dready.com) written by Horst Schaeffer that also can check write access. I have not tested it but as far as I can see this could be used as a solution; call it for instance as "DREADY C: /W" and check the return value.
I have Midas project that uses a TDataSetProvider in one of RemoteDataModules in the Server
Currently I am making use of the following events
BeforeApplyUpdates - to create an Object
BeforeUpdateRecord - to use the object
AfterApplyUpdates - to destory the object
Question:
Will ‘ AfterApplyUpdates’ always be called even if the is an update error ?
If you look at the sourcecode:
function TCustomProvider.DoApplyUpdates(const Delta: OleVariant; MaxErrors: Integer;
out ErrorCount: Integer; var OwnerData: OleVariant): OleVariant;
begin
SetActiveUpdateException(nil);
try
try
if Assigned(FOnValidate) then
FOnValidate(Delta);
DoBeforeApplyUpdates(OwnerData);
Self.OwnerData := OwnerData;
try
Result := InternalApplyUpdates(Delta, MaxErrors, ErrorCount);
finally
OwnerData := Self.OwnerData;
Self.OwnerData := unassigned;
end;
except
on E: Exception do
begin
SetActiveUpdateException(E);
raise;
end;
end;
finally
try
DoAfterApplyUpdates(OwnerData);
finally
SetActiveUpdateException(nil);
end;
end;
end;
Yoy see that DoAfterApplyUpdates is called in the finally block. This means it is always called regardles of any exception.