Thread: Retrieve users connected to Access database

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

    Retrieve users connected to Access database

    Can anyone help me convert the following code to C++? It's code for retrieving the active users of an Access MDB database file. Unfortunately, I cannot google any C++ examples. What totally confuses me is JET_SCHEMA_USERROSTER constant.

    Thanx
    Bob

    Code:
    const
      JET_SCHEMA_USERROSTER = '{947bb102-5d43-11d1-bdbf-00c04fb92675}';
    var
      i: Integer;
      Line: String;
    
    with ADOConnection1.OpenSchema(adSchemaProviderSpecific, EmptyParam, JET_SCHEMA_USERROSTER) do
      while not EOF do
      begin
        Line := '';
        with Fields do
          for i := 0 to Count - 1 do
          begin
            Line := Line + Item[i].Name + ':';
            if VarIsNull(Item[i].Value) then
              Line := Line + '(Null)'
            else
              Line := Line + String(Item[i].Value);
            Line := Line + #13;
          end;
    
        // remove zero bytes
        for i := 1 to Length(Line) do
          if Line[i] = #0 then
            Line[i] := ' ';
    
        ShowMessage(Line);
    
        MoveNext;
      end;

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    What compiler are you using? The JET_SCHEMA_USERROSTER constant is a GUID. In this case OpenSchema will accept the GUID as a string.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I'm using Microsoft Visual c++ version 7.0. I'm confused by JET_SCHEMA_USERROSTER. Is the 947bb102-5d43-11d1-bdbf-00c04fb92675 a randomly generated value or does it have some special relevance for the OpenSchema function? I'm trying to create the C++ version of this code snippet.

    Thanx

    Bob

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    The value of JET_SCHEMA_USERROSTER is, as anonytmouse said, a GUID (globally unique identifier). What this means is that number is unique across the whole world. Yes, it was randomly generated originally, but because there are so many possible combinations of hexadecimal digits and the generation of it was randomized in certain ways, it is perceived to be a constant which can't be generated again (it can, but the probability is very very very very small).

    Hence, if you wanted to call something "Bob" on everyone's Windows PC, and wanted to ensure that no-one else could mistakenly (not maliciously) call something "Bob" as well, which may cause a collision, you can use a GUID instead.

    I'm not genned up on C++ and ADO, however. To be honest I think VB is the best tool for that sort of thing.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>JET_SCHEMA_USERROSTER = 947bb102-5d43-11d1-bdbf-00c04fb92675

    This may be a registered COM interface. Each is given a GUID to identify the interface in the Component Services section of the registry.
    You build a COM+ dll and register it under 'COM+ applications' in Windows OS 'Component Services'.
    This is used to hold code specific to a table, view ect in the DB. That is a class to handle interaction with one part of the Db schema.
    They can be called from many languages, typically VB or ASP.

    Usually there is an .IDL for the dll that contains these class identifiers (CLSID). There is also a text version of the IDL the TBL (?).

    IIRC the 11d1 means it was time generated using a MAC address (or is that a 4?).

    MSDN
    "A globally unique identifier (GUID) associated with an OLE class object. If a class object will be used to create more than one instance of an object, the associated server application should register its CLSID in the system registry so that clients can locate and load the executable code associated with the object(s). Every OLE server or container that allows linking to its embedded objects must register a CLSID for each supported object definition."
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    So, essentially, this is a guid for a active user query which is a custom query. I'd have to build a COM+ dll that contains this query and then register it. Can anyone point me to any C++ samples to accomplish this task?

    Thanx for all your help!!!!!

    Bob

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Assumes VC++

    Code:
    #include <windows.h>
    #include <comdef.h>
    #include <comutil.h>
    #include <iostream>
    #include <sstream>
    
    #import "libid:{2A75196C-D9EB-4129-B803-931327F72D5C}" rename("EOF","EOF_")//ADO
    
    const WCHAR* ConnectionString = L"Provider=Microsoft.Jet.OLEDB.4.0;"
    	L"Data Source=C:\\Test.mdb;Persist Security Info=False";
    
    int main(void)
    {
    	CoInitialize(0);
    	try
    	{
    		ADODB::_ConnectionPtr oCon;
    		HRESULT Res = oCon.CreateInstance(L"ADODB.Connection");
    		if(FAILED(Res))
    			throw _com_error(Res);
    		oCon->Open(ConnectionString,L"",L"",0);
    		ADODB::_RecordsetPtr oRec = oCon->OpenSchema(ADODB::adSchemaProviderSpecific,
    			vtMissing, L"{947bb102-5d43-11d1-bdbf-00c04fb92675}");		
    		long Count = oRec->GetFields()->GetCount();
    		_variant_t Var;
    		while(!oRec->EOF_)
    		{
    			std::stringstream ss;
    			for(long i = 0;i < Count;++i)
    			{
    				Var = oRec->GetFields()->GetItem(i)->GetValue();
    				if(Var.vt == VT_NULL)
    					ss << "<NULL> ";
    				else
    					ss << static_cast<LPCSTR>(_bstr_t(Var)) << " ";
    			}	
    			std::cout << ss.str() << std::endl;
    			oRec->MoveNext();
    		}
    		std::cout << "End of List" << std::endl;
    	}
    	catch(_com_error& e)
    	{
    		std::cerr << "Error Cought - " << e.ErrorMessage() << std::endl;
    	}
        CoUninitialize();
    }

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Fordy,

    THANX!!!!!

    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2005, 01:26 PM
  2. database application help.
    By Billy Baroo in forum Linux Programming
    Replies: 1
    Last Post: 09-02-2002, 06:23 PM
  3. database
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 08:46 AM
  4. DataBase access
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2001, 08:25 PM
  5. Database Access
    By bubbajones in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2001, 10:05 PM