VCL Delphi

Title: Make a shareware easily (Component)
Question: How can i determine the number of times to use my application ?(shareware)
Answer:
Unit ShareWareTimes;
{********************************************************}
{ }
{ Creating a shareware easily !! }
{ Under7@iFrance.com }
{ }
{********************************************************}
Interface
Uses Registry, Classes, SysUtils, Windows;
(******************************************************}
Introduction
============
To use this component install it using delphi menu
Component Install a Component
give the correct address to the unit and then compile
the package.
After installing the component just drop it to your
main form.
How to use :
============
You can read the number of times that the application was
used by :
ShareWareTimes1.Read; (Integer value)
You can add what you want to the current value using :
ShareWareTimes1.Add(3);
You should set WriteSession to false after writing to
avoid adding 1 when the component is destroyed.
If you don't have access to the registry an error will be raised.
Hints :
=======
1) The number max uses is seen in your resources and it
can be easily modified !!! so you have to set it on runtime
by : ShareWare1.MaxUses := 10;
2) You should also write alone the number of times that the
app was used because the current writing wait until the app
destroys the component ! The problem is that who use your
application can terminate its process and the component will
not write ...
You can see that just by running your app under delphi and
then use terminate the program (Ctrl+F2)
3) You should use other keys, for example :
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
Sorry, my english is too bad !
Liquid Snake.
(******************************************************)
Type TOnElapsed = Procedure of object;
Type TRootKeys = (Classes_Root,Current_User,Local_Machine,
Users,Performance_Data,Current_Config,Dyn_Data);
Type
TShareWareTimes = Class(TComponent)
Private
FRootKey :TRootKeys;
FKeyName,
FKeyValue :String;
FMax :Byte;
FElapsed :TOnElapsed;
FWriteSession :Boolean;
Public
Function Read:Integer;
Constructor Create(Aowner :TComponent);override;
Destructor Destroy; override;
Procedure Add(Index:ShortInt);
Published
Property MaxUses : Byte read FMax write FMax;
Property RootKey : TRootKeys read FRootKey write FRootKey;
Property KeyName : String read FKeyName write FKeyName;
Property KeyValue : String read FKeyValue write FKeyValue;
Property OnElapsed : TOnElapsed read FElapsed write FElapsed;
Property WriteSession: Boolean read FWriteSession write FWriteSession;
end;
Procedure Register;
Implementation
Procedure Register;
begin
RegisterComponents('ShareWare', [TShareWareTimes]);
end;
Function RootKeyConvert(R:TRootKeys):HKEY;
begin
{Convert the rootkeys values to cardinals ...}
Result:=HKEY_CURRENT_USER;
if R=Local_Machine then Result:=HKEY_LOCAL_MACHINE;
if R=Classes_Root then Result:=HKEY_CLASSES_ROOT;
if R=Users then Result:=HKEY_USERS;
if R=Performance_Data then Result:=HKEY_PERFORMANCE_DATA;
if R=Current_Config then Result:=HKEY_CURRENT_CONFIG;
if R=Dyn_Data then Result:=HKEY_DYN_DATA;
end;
Function IsInteger(Value :String):Boolean;
var i:integer;
begin
//See if the value given is a valid integer.
Result:=False;
if Value='' then exit;
for i:=1 to length(Value) do
if not (Value[i] in ['0'..'9','+','-']) then exit;
Result:=True;
end;
Function TShareWareTimes.Read:integer;
var F:String;
begin
//Get the number of times that your application was used.
with TRegistry.Create do try
RootKey:=RootKeyConvert(FRootKey);
OpenKey(KeyName, True);
{I don't want to use the ReadInteger because it raise an
exception for the first time !!}
F:=ReadString(KeyValue);
if not IsInteger(F) then F:='0';
Result:=StrToInt(F)+1;
if (ResultMaxUses) and Assigned(OnElapsed) then OnElapsed;
finally Free;
end;
end;
Constructor TShareWareTimes.Create(Aowner :TComponent);
begin
inherited Create(AOwner);
RootKey :=Current_User;
KeyName :='ShareWare key';
KeyValue :='ShareWare value';
MaxUses :=10;
WriteSession:=True;
end;
Destructor TShareWareTimes.Destroy;
begin
if WriteSession then Add(1);
inherited Destroy;
end;
procedure TShareWareTimes.Add(Index:ShortInt);
begin
with TRegistry.Create do try
RootKey:=RootKeyConvert(FRootKey);
OpenKey(KeyName, True);
WriteString(KeyValue,IntToStr(Read+Index-1));
finally Free;end;
end;
end.