Solved

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

Posted on 2003-11-01
4
608 Views
Last Modified: 2013-12-09
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.
0
Comment
Question by:chart11610
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:BAlexandrov
BAlexandrov earned 100 total points
ID: 9809876
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
0
 
LVL 19

Accepted Solution

by:
Gary Benade earned 150 total points
ID: 9865086
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;
0
 
LVL 6

Expert Comment

by:BAlexandrov
ID: 9866114
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
0
 
LVL 10

Expert Comment

by:János Szabó
ID: 10340582
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
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question