Microsoft KB Archive/166938

= How To Find Users Connected to an Access 7.0 Database =

Article ID: 166938

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition

-



This article was previously published under Q166938



SUMMARY
Use Msldbusr.dll to determine which users are connected to an Access 7.0 Database.



MORE INFORMATION
Download Msldbusr.dll using the instructions in the following article in the Microsoft Knowledge Base:

176670 ACC: Microsoft Jet Utilities Available on MSL

Then follow the instructions below to use this DLL from Visual C++. Use the Msldbusr.dll to find out who is connected to a Jet database.

The Msldbusr.dll Functions

 * LDBUser_GetUsers - After being called, this function returns two key pieces of information, the list of users and the number of users connected (if successful, otherwise returns an error number).
 * LDBUser_GetError - Returns a string error message stating what the return value from LDBUser_GetUsers was.

Function Declarations
To use the functions from within C/C++, they should be declared in the following way: #include 

typedef INT (WINAPI *PFNLDBUSERGETUSER)(LPSAFEARRAY FAR *, LPCSTR,  INT); PFNLDBUSERGETUSER lpfnLdbUser_GetUsers;

typedef BSTR (WINAPI *PFNLDBUSERGETERR)(INT); PFNLDBUSERGETERR lpfnLdbUser_GetError;

Function Parameters for C/C++
INT LDBUser_GetUsers (      LPSAFEARRAY FAR * lpsaUserList,         // Returned list of users        LPCSTR            lpcstrMdbFilename,    // The name of the MDB file        INT               nOptions              // Specifies return options    )

lpsaUserList - This is an array of BSTRs that contains a list of users when the function returns. This BSTR array in C/C++ should initially be allocated with a lower bound equal to 1 (however, this is not required) and the count of elements set to 1. The Msldbusr.dll will redimension the count of elements for the BSTR array if the number of elements is less than the number of users.

For example:

     SAFEARRAY FAR*    psaUserList; SAFEARRAYBOUND   sabUserList[1]; sabUserList[0].lLbound = 1; sabUserList[0].cElements = 1; psaUserList = SafeArrayCreate(VT_BSTR, 1, sabUserList); lpcstrMdbFilename - This is the name of the filename with its associated database extension (MDB, MDW, MDA, etc.). A corresponding LDB file must exist, otherwise LDBUser_GetUsers will return an error.

nOptions - Specifies the type of users that are returned into lpsaUserList.    - 1 = All users who have logged in since the LDB file was created. - 2 = Only users who are currently logged in. - 4 = Only users who are causing the database file to be corrupt. - 8 = Just returns the count of users. No users are returned in the BSTR array. BSTR LDBUser_GetError (      INT nError // The error number returned by LDBUser_GetUsers   ) nError - This is the error value returned when LDBUser_GetUsers returns a negative value. This should normally be in the range of -1 to -14. The error values are:      -1 = Can't open the LDB file -2 = No user connected -3 = Can't create an array -4 = Can't redimension array -5 = Invalid argument passed -6 = Memory allocation error -7 = Bad index -8 = Out of memory -9 = Invalid Argument -10 = LDB is suspected as corrupted -11 = Invalid argument -12 = Unable to read MDB file -13 = Can't open the MDB file -14 = Can't find the LDB file

Sample Code
#include   #include 

typedef INT (WINAPI *PFNLDBUSER)(LPSAFEARRAY FAR *, PCSTR, INT ); PFNLDBUSER lpfnLdbUser_GetUsers;

typedef BSTR (WINAPI *PFNLDBUSERGETERR)(INT); PFNLDBUSERGETERR lpfnLdbUser_GetError;

BOOL ShowUsers(char *);

void main( int argc, char* argv[] ) {      if (argc != 2) return;

ShowUsers(argv[1]); }

//   // Display a list of JET users to the console screen. //   BOOL ShowUsers( char* pszDatabase ) {      HINSTANCE       hLdbUserInstance; SAFEARRAY FAR* psaUserList = NULL; SAFEARRAYBOUND sabUserList[1]; int            cUsers; LONG           iLoop; HRESULT        hr; char*          szBuff; BOOL           fRetVal = TRUE; BSTR           bstrError = NULL;

// Get the library instance handle. hLdbUserInstance = LoadLibrary("MSLDBUSR.DLL"); if (!hLdbUserInstance) return FALSE;

// Get a pointer to the main function in the library. lpfnLdbUser_GetUsers = (PFNLDBUSER)GetProcAddress(hLdbUserInstance,          "LDBUser_GetUsers"); if (!lpfnLdbUser_GetUsers) {      fRetVal = FALSE; goto Exit; }

// Get a pointer to the error function in case an error occurs. lpfnLdbUser_GetError = (PFNLDBUSERGETERR)GetProcAddress(      hLdbUserInstance, "LDBUser_GetError"); if (!lpfnLdbUser_GetError) {      fRetVal = FALSE; goto Exit; }

// Create a SAFEARRAY to hold the list of users. // NOTE: We'll only create a 1 element array because the // LDBUser_GetUsers will adjust the size if needed. sabUserList[0].lLbound   = 1; sabUserList[0].cElements = 1; psaUserList = SafeArrayCreate(VT_BSTR, 1, sabUserList);

// Make the function call and obtain the list of users. cUsers = lpfnLdbUser_GetUsers(&psaUserList, pszDatabase, 0x1);

if (!cUsers ) {      printf("No users\n"); goto Exit; }

// Display error messages if any occurred. if (cUsers < 0) {      bstrError = SysAllocString( lpfnLdbUser_GetError( cUsers ) ); printf("Error #:%d -- %s\n",cUsers, bstrError); goto Exit; }

// Display the list of users. for(iLoop=1;iLoop <= cUsers; iLoop++) {      hr = SafeArrayGetElement(psaUserList, &iLoop, &szBuff); if (SUCCEEDED(hr)) printf("User %d:%s\n",iLoop, szBuff); else printf("Failed on User %d\n",iLoop ); }

// Cleanup. Exit: if (bstrError) SysFreeString(bstrError);

if (psaUserList) SafeArrayDestroy(psaUserList);

// Free up the Msldbusr.dll. FreeLibrary(hLdbUserInstance);

return TRUE; }

Additional query words: Jet

Keywords: kbhowto kbinterop kbdatabase kbcode KB166938

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.