Article: Q166938
Product(s): Microsoft C Compiler
Version(s):
Operating System(s):
Keyword(s): kbcode kbinterop kbDAOsearch kbDatabase kbMFC kbVC400 kbVC410 kbVC420 kbVC500 kbVC600
Last Modified: 04-AUG-2001
-------------------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual C++, versions 4.0, 4.1
- Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0
- Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0
-------------------------------------------------------------------------------
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:
Q176670 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 <WINDOWS.H>
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 <windows.h>
#include <stdio.h>
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 : kbcode kbinterop kbDAOsearch kbDatabase kbMFC kbVC400 kbVC410 kbVC420 kbVC500 kbVC600
Technology : kbVCsearch kbVC400 kbAudDeveloper kbVC410 kbVC420 kbVC500 kbVC32bitSearch kbVC500Search
Issue type : kbhowto
=============================================================================