IB7.1 Number of users logged into the DB, and who they are.

Interbase 7.1
Delphi 7
Looking for a code sample on how to determine the number of users currently logged into a specific database (not just the server).  Any other supporting logon information would also be helpful.   Do not want to give the users SYSDBA access to get this information.   This information is to be used to control the number of valid users within an application.
LVL 1
chart11610Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BAlexandrovCommented:
I have done this one time  - it was for puprose to be able to view whish users are currently connected. The application itself had users - a table with information about each user but without the password.
When new user is created - it creates new user on the server also with the suffix - application name.
When user logon - it is authenticated to the server(user name that he have supplied + application name and the password) - with logging in to the database.
Then it is easy to get list of currently active users on the server, filter this list with users only from our application (comparing with the users table in our database) and list only them.

Drawbacks - you have to include in your app hidden administrative password - an account that will have rights to make new servers accounts.

Just as example..

Bojidar Alexandrov
Gary BenadeCommented:
below is a function to retrieve the usernames into a StringList using the Interbase API.
I am actually a c programmer so I may have made some syntax errors.

procedure TForm1.GetConnStats( FUserNames: TStringList)

var
Status_Vector: ISC_STATUS_VECTOR;
db_handle: isc_db_handle;
DPB_Buffer: array [0..255] of Char;
DPB_Index: Integer;
DBName_Cstr: array [0..255] of Char;
temp_buffer: array[0..255] of Char;

{Options buffer and index}

DBStatOpts: array [0..15] of Byte;
NumOpts: Integer;

{Result buffer array, index and pointer}

Result_Buffer: array [0..1023] of Byte;
RB_index: Integer;
RB_ptr: Pointer;

{Variables for handling each return item}
Item: Byte;
ItemLen: ISC_LONG;
Page_Size: Longint;
Num_Pages: Longint;
i, user_length: Integer;

begin

{Initialise the buffer index and set the first character value which is always the parameter buffer version number}
DPB_Index := 0;
DPB_Buffer[DPB_Index] := Char(isc_dpb_version1);
Inc(DPB_Index);

{Set the user name parameter identifier and copy the name from the user name edit field on the form}
DPB_Buffer[DPB_Index] := Char(isc_dpb_user_name);
Inc(DPB_Index);
DPB_Buffer[DPB_Index] := Char(Length(Edit_Username.Text));
Inc(DPB_Index);
StrPCopy(@DPB_Buffer[DPB_Index], Edit_Username.Text);
Inc(DPB_Index, Length(Edit_Username.Text));

{Set the password parameter identifier and copy the name from the password edit field on the form}
DPB_Buffer[DPB_Index] := Char(isc_dpb_password);
Inc(DPB_Index);
DPB_Buffer[DPB_Index] := Char(Length(Edit_Password.Text));
Inc(DPB_Index);
StrPCopy(@DPB_Buffer[DPB_Index], Edit_Password.Text);
Inc(DPB_Index, Length(Edit_Password.Text));

{Copy the database name from the edit field on the form to a C-string style character array}
StrPCopy(DBName_Cstr, Edit_DBName.Text);

{Initialise the database handle to nil - mandatory}
db_handle := nil;

{Attach to the database}
isc_attach_database(@Status_Vector, Length(Edit_DBName.Text), @DBName_Cstr, @db_handle, DPB_Index, DPB_Buffer);


{ Username extraction starts here }

{Exit if we’re not connected}
if db_handle = nil then
Exit;

{Set up the information options required}
NumOpts := 0;
DBStatOpts[NumOpts] := isc_info_page_size;
Inc(NumOpts);
DBStatOpts[NumOpts] := isc_info_allocation;
Inc(NumOpts);

{Issue the call to get the information required about usernames}
if isc_database_info (@Status_Vector, @db_handle, NumOpts, @DBStatOpts, SizeOf(Result_Buffer), @Result_Buffer) <> 0 then
isc_print_status(@Status_Vector);

RB_Index := 0;
FUserNames.Clear;

while Result_Buffer[RB_Index] = Char(isc_info_user_names)
do
begin
Inc(RB_Index, 3); { skip "isc_info_user_names byte" & two unknown bytes of structure}
user_length := Long(Result_Buffer[RB_Index]);
Inc(RB_Index,1);  
Move(Result_Buffer[RB_Index], temp_buffer[0], user_length);
Inc(RB_Index, user_length);  
temp_buffer[user_length] := #0;
FUserNames.Add(String(temp_buffer));
end; { while }

{ Username extraction section ends here }

{Detach to the database}
if db_handle <> nil then isc_detach_database( @Status_Vector, @db_handle);

end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BAlexandrovCommented:
Great job, hobbit72!
But this can be done via ibx components (or other)...
The main problem is that this is list of users connected to the _server_. To _any_ database on this server.
The problem is how to get users connected only to your application's own database.

Bojidar Alexandrov
kacorretiredCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
      to split points between BAlexandrov (100) and hobbit72 (150)
Please leave any comments here within the next four (4) days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

If you will get a useful answer to your newer question next time, please accept it after evaluating. By this way the experts will get sooner his expert points for supporting you. If you have question about, please look at  http://www.experts-exchange.com/help.jsp.

kacor
EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.