Solved

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

Posted on 2003-11-01
4
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:kacor
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

630 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