Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread How to: Limit instances of your app being run on a network (ie: 3 user license)
Wed, May 10 2006 6:05 AMPermanent Link

Graham Wood
I probably got this from here in the first placeSmiley but I can't remember, so in case I
didn't, here's some damn handy code.  
Great for limiting the instances of your app being run on a network.  Would work just as
effectively for File Server and Client/Server, so long as the exe is being run from a
shared server folder.  It gives your users incentive to purchase extra licenses.

Only issue is that if one user is 'goofy' enough to run extra instances of your exe on
their machine, they'll be considered as extra users.  You can get around this by educating
your users, or by putting in instance limiting code.


Here's the handy stuff:


{** Function to count the number of users on a given EXE }

unit u_LimitUsers;

interface

uses
 Messages,
 SysUtils,
 Windows;

{** Function to count the number of users on a given EXE |
Adds the user and returns his user number, or -1 if all
slots are taken. You don't have to remove him. That will
happen automatically when he exits the program.

fn is a legal file name in an existing directory that will
    control access. The file need not exist, or have any
    contents.

max is the Maximum number of users allowed.
}

function AddUser(fn: string; max: integer): integer;

var
 FileMustExist: boolean;
 BIGNUMBER:     dword;

implementation

var
 h: integer;

function AddUser(fn: string; max: integer): integer;
var
 n, i:     integer;
 FoundOne: boolean;
begin
 Result := -1;

 // The magic file must be open for this to work.
 // if it doesn't exist, create it. Then open it,
 // making sure access is shared so other instances of
 // the app can also open it.

 if h <= 0 then
 begin
   if not FileExists(fn) then
   begin
     if FileMustExist then
     begin
       exit;
     end;
     begin
       h := FileCreate(fn);
       FileClose(h);
     end;
   end;
   h := FileOpen(fn, fmShareDenyNone);
   if h <= 0 then
   begin
     exit;
   end;
 end;

 n := 0;
 FoundOne := False;

 for i := 0 to max - 1 do
 begin
   if LockFile(h, i{ BIGNUMBER}, 0, 1, 0) then
   begin
     if not FoundOne then
     begin
       FoundOne := True;
     end
     else
     begin
       UnlockFile(h, i{ BIGNUMBER}, 0, 1, 0);
     end;
     // continue to loop after finding a slot, to count users.
     // Just be sure to unlock the bytes we locked solely
     // for counting purposes.
   end
   else
   begin
     Inc(n);
   end;
   // Current user count is the number of bytes we can't lock.
 end;

 if FoundOne then
 begin
   Result := n;
 end{ 1};
end;

initialization
 h := -1;
 FileMustExist := False;
 BIGNUMBER := 0;
end.


USE THE ABOVE UNIT LIKE THIS:
___________________________________________________________________________________
var
 iUserCount: Integer;
 sTheAppPath: String;
begin
 sTheAppPath := ExtractFilePath(Application.ExeName);
 iUserCount := 3; // 3 user license as an example, usually we set it up with $IF DEFS;

   if not (iUserCount = 0) then
   begin
     //  Check for license conditions.
     if (AddUser(TheAppPath + 'your_app.lic', iUserCount) = -1) then
     begin
       //  Exceeded the user limit.
       MessageDlg('There are too many people currently logged in to this program.' +
         #13 + #10 + 'You have a ' + IntToStr(iUserCount) +
         ' user license and the limit has been exceeded.' +
         #13 + #10 + #13 + #10 + 'This program will now close.', mtInformation, [mbOK], 0);
     end
     else
     begin
       // Logged on within the limit.
       Close;
     end;
   end
   else
   begin
     //  Unlimited user.
     Close;
   end;
end;  
___________________________________________________________________________________



Wed, May 10 2006 11:25 AMPermanent Link

Jon Lloyd Duerdoth
and into "Knowledgebase" it goes...
thanks Graham... thanks Steve

Jon

Graham Wood wrote:
> I probably got this from here in the first placeSmiley but I can't remember, so in case I
> didn't, here's some damn handy code.  
> Great for limiting the instances of your app being run on a network.  Would work just as
> effectively for File Server and Client/Server, so long as the exe is being run from a
> shared server folder.  It gives your users incentive to purchase extra licenses.
>
> Only issue is that if one user is 'goofy' enough to run extra instances of your exe on
> their machine, they'll be considered as extra users.  You can get around this by educating
> your users, or by putting in instance limiting code.
>
>
> Here's the handy stuff:
>
>
> {** Function to count the number of users on a given EXE }
>
> unit u_LimitUsers;
>
> interface
>
> uses
>   Messages,
>   SysUtils,
>   Windows;
>
> {** Function to count the number of users on a given EXE |
>  Adds the user and returns his user number, or -1 if all
>  slots are taken. You don't have to remove him. That will
>  happen automatically when he exits the program.
>
>  fn is a legal file name in an existing directory that will
>      control access. The file need not exist, or have any
>      contents.
>
>  max is the Maximum number of users allowed.
> }
>
> function AddUser(fn: string; max: integer): integer;
>
> var
>   FileMustExist: boolean;
>   BIGNUMBER:     dword;
>
> implementation
>
> var
>   h: integer;
>
> function AddUser(fn: string; max: integer): integer;
> var
>   n, i:     integer;
>   FoundOne: boolean;
> begin
>   Result := -1;
>
>   // The magic file must be open for this to work.
>   // if it doesn't exist, create it. Then open it,
>   // making sure access is shared so other instances of
>   // the app can also open it.
>
>   if h <= 0 then
>   begin
>     if not FileExists(fn) then
>     begin
>       if FileMustExist then
>       begin
>         exit;
>       end;
>       begin
>         h := FileCreate(fn);
>         FileClose(h);
>       end;
>     end;
>     h := FileOpen(fn, fmShareDenyNone);
>     if h <= 0 then
>     begin
>       exit;
>     end;
>   end;
>
>   n := 0;
>   FoundOne := False;
>
>   for i := 0 to max - 1 do
>   begin
>     if LockFile(h, i{ BIGNUMBER}, 0, 1, 0) then
>     begin
>       if not FoundOne then
>       begin
>         FoundOne := True;
>       end
>       else
>       begin
>         UnlockFile(h, i{ BIGNUMBER}, 0, 1, 0);
>       end;
>       // continue to loop after finding a slot, to count users.
>       // Just be sure to unlock the bytes we locked solely
>       // for counting purposes.
>     end
>     else
>     begin
>       Inc(n);
>     end;
>     // Current user count is the number of bytes we can't lock.
>   end;
>
>   if FoundOne then
>   begin
>     Result := n;
>   end{ 1};
> end;
>
> initialization
>   h := -1;
>   FileMustExist := False;
>   BIGNUMBER := 0;
> end.
>
>
> USE THE ABOVE UNIT LIKE THIS:
> ___________________________________________________________________________________
> var
>   iUserCount: Integer;
>   sTheAppPath: String;
> begin
>   sTheAppPath := ExtractFilePath(Application.ExeName);
>   iUserCount := 3; // 3 user license as an example, usually we set it up with $IF DEFS;
>
>     if not (iUserCount = 0) then
>     begin
>       //  Check for license conditions.
>       if (AddUser(TheAppPath + 'your_app.lic', iUserCount) = -1) then
>       begin
>         //  Exceeded the user limit.
>         MessageDlg('There are too many people currently logged in to this program.' +
>           #13 + #10 + 'You have a ' + IntToStr(iUserCount) +
>           ' user license and the limit has been exceeded.' +
>           #13 + #10 + #13 + #10 + 'This program will now close.', mtInformation, [mbOK], 0);
>       end
>       else
>       begin
>         // Logged on within the limit.
>         Close;
>       end;
>     end
>     else
>     begin
>       //  Unlimited user.
>       Close;
>     end;
> end;  
> ___________________________________________________________________________________
>
>
>
>
Wed, May 10 2006 5:21 PMPermanent Link

Steve Forbes

Team Elevate Team Elevate

Hi Jon,

Nice tip Graham! thanks.

And thanks for the eplug, Jon <bg>
--
Best regards

Steve

"Jon Lloyd Duerdoth" <jld@welshdragoncomputing.ca> wrote in message
news:0D03BBB0-34AC-462B-96DF-54AA11EB0A8A@news.elevatesoft.com...
> and into "Knowledgebase" it goes...
> thanks Graham... thanks Steve

Wed, May 10 2006 8:57 PMPermanent Link

Jon Lloyd Duerdoth
Well (of course) I should have mentioned the
Knowledgebase web site
    http://www.ozmosys.net.au/
while I was at it, seeing as Steve would not want to
do that himself. Smile

BTW: I am just a happy user and not associated in anyway
     with ozmosys.

Steve Forbes wrote:
> Hi Jon,
>
> Nice tip Graham! thanks.
>
> And thanks for the eplug, Jon <bg>
Wed, May 10 2006 9:20 PMPermanent Link

"Walter Matte"
It had been loaded into my KnowledgeBase too!!

TY Graham and .... Steve. Smiley

Walter

"Jon Lloyd Duerdoth" <jld@welshdragoncomputing.ca> wrote in message
news:26E05270-DA27-438D-A838-6766ABEB9A81@news.elevatesoft.com...
> Well (of course) I should have mentioned the
> Knowledgebase web site
>     http://www.ozmosys.net.au/
> while I was at it, seeing as Steve would not want to
> do that himself. Smile
>
> BTW: I am just a happy user and not associated in anyway
>      with ozmosys.
>
> Steve Forbes wrote:
>> Hi Jon,
>>
>> Nice tip Graham! thanks.
>>
>> And thanks for the eplug, Jon <bg>

Thu, May 11 2006 8:09 PMPermanent Link

"Norman W. Clark [Clark-Tech Inc.]"
Like most of you I assume, Knowledgebase is pinned to my Start button - and
always handy there to drop in a hint.  Sort of like my own private Google.
.... Now, where did I put that code snippet?  Thank you Steve.

--
.... Norm

Norman W. Clark, Clark-Tech Inc.
nclark@clark-tech.com
www.clark-tech.com
www.smbproducts.com
"Jon Lloyd Duerdoth" <jld@welshdragoncomputing.ca> wrote in message
news:0D03BBB0-34AC-462B-96DF-54AA11EB0A8A@news.elevatesoft.com...
> and into "Knowledgebase" it goes...
> thanks Graham... thanks Steve
>
> Jon
>
> Graham Wood wrote:
>> I probably got this from here in the first placeSmiley but I can't remember, so
>> in case I
>> didn't, here's some damn handy code.  Great for limiting the instances of
>> your app being run on a network.  Would work just as
>> effectively for File Server and Client/Server, so long as the exe is being
>> run from a
>> shared server folder.  It gives your users incentive to purchase extra
>> licenses.
>>
>> Only issue is that if one user is 'goofy' enough to run extra instances of
>> your exe on
>> their machine, they'll be considered as extra users.  You can get around this
>> by educating
>> your users, or by putting in instance limiting code.
>>
>>
>> Here's the handy stuff:
>>
>>
>> {** Function to count the number of users on a given EXE }
>>
>> unit u_LimitUsers;
>>
>> interface
>>
>> uses
>>   Messages,
>>   SysUtils,
>>   Windows;
>>
>> {** Function to count the number of users on a given EXE |
>>  Adds the user and returns his user number, or -1 if all
>>  slots are taken. You don't have to remove him. That will
>>  happen automatically when he exits the program.
>>
>>  fn is a legal file name in an existing directory that will
>>      control access. The file need not exist, or have any
>>      contents.
>>
>>  max is the Maximum number of users allowed.
>> }
>>
>> function AddUser(fn: string; max: integer): integer;
>>
>> var
>>   FileMustExist: boolean;
>>   BIGNUMBER:     dword;
>>
>> implementation
>>
>> var
>>   h: integer;
>>
>> function AddUser(fn: string; max: integer): integer;
>> var
>>   n, i:     integer;
>>   FoundOne: boolean;
>> begin
>>   Result := -1;
>>
>>   // The magic file must be open for this to work.
>>   // if it doesn't exist, create it. Then open it,
>>   // making sure access is shared so other instances of
>>   // the app can also open it.
>>
>>   if h <= 0 then
>>   begin
>>     if not FileExists(fn) then
>>     begin
>>       if FileMustExist then
>>       begin
>>         exit;
>>       end;
>>       begin
>>         h := FileCreate(fn);
>>         FileClose(h);
>>       end;
>>     end;
>>     h := FileOpen(fn, fmShareDenyNone);
>>     if h <= 0 then
>>     begin
>>       exit;
>>     end;
>>   end;
>>
>>   n := 0;
>>   FoundOne := False;
>>
>>   for i := 0 to max - 1 do
>>   begin
>>     if LockFile(h, i{ BIGNUMBER}, 0, 1, 0) then
>>     begin
>>       if not FoundOne then
>>       begin
>>         FoundOne := True;
>>       end
>>       else
>>       begin
>>         UnlockFile(h, i{ BIGNUMBER}, 0, 1, 0);
>>       end;
>>       // continue to loop after finding a slot, to count users.
>>       // Just be sure to unlock the bytes we locked solely
>>       // for counting purposes.
>>     end
>>     else
>>     begin
>>       Inc(n);
>>     end;
>>     // Current user count is the number of bytes we can't lock.
>>   end;
>>
>>   if FoundOne then
>>   begin
>>     Result := n;
>>   end{ 1};
>> end;
>>
>> initialization
>>   h := -1;
>>   FileMustExist := False;
>>   BIGNUMBER := 0;
>> end.
>>
>>
>> USE THE ABOVE UNIT LIKE THIS:
>> ___________________________________________________________________________________
>> var
>>   iUserCount: Integer;
>>   sTheAppPath: String;
>> begin
>>   sTheAppPath := ExtractFilePath(Application.ExeName);
>>   iUserCount := 3; // 3 user license as an example, usually we set it up with
>> $IF DEFS;
>>
>>     if not (iUserCount = 0) then
>>     begin
>>       //  Check for license conditions.
>>       if (AddUser(TheAppPath + 'your_app.lic', iUserCount) = -1) then
>>       begin
>>         //  Exceeded the user limit.
>>         MessageDlg('There are too many people currently logged in to this
>> program.' +
>>           #13 + #10 + 'You have a ' + IntToStr(iUserCount) +
>>           ' user license and the limit has been exceeded.' +
>>           #13 + #10 + #13 + #10 + 'This program will now close.',
>> mtInformation, [mbOK], 0);
>>       end
>>       else
>>       begin
>>         // Logged on within the limit.
>>         Close;
>>       end;
>>     end
>>     else
>>     begin
>>       //  Unlimited user.
>>       Close;
>>     end;
>> end;
>> ___________________________________________________________________________________
>>
>>
>>
Image