Click here to Skip to main content
15,915,513 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape23-Oct-08 17:04
Supriya Tonape23-Oct-08 17:04 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery23-Oct-08 20:06
Mark Salsbery23-Oct-08 20:06 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape23-Oct-08 20:45
Supriya Tonape23-Oct-08 20:45 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery23-Oct-08 20:49
Mark Salsbery23-Oct-08 20:49 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape23-Oct-08 21:19
Supriya Tonape23-Oct-08 21:19 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery24-Oct-08 4:57
Mark Salsbery24-Oct-08 4:57 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape24-Oct-08 5:51
Supriya Tonape24-Oct-08 5:51 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery24-Oct-08 6:09
Mark Salsbery24-Oct-08 6:09 
hmm I tested it and it worked for me...

Here's my test code - I took your code and added stuff to the top
(copied right from that MSDN sample code) for me to build it...
int GetRemoteLoggedOnUsers()
{
HRESULT hres = WBEM_S_NO_ERROR;

    //hres =  CoInitializeSecurity(
    //    NULL, 
    //    -1,                          // COM authentication
    //    NULL,                        // Authentication services
    //    NULL,                        // Reserved
    //    RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
    //    RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
    //    NULL,                        // Authentication info
    //    EOAC_NONE,                   // Additional capabilities 
    //    NULL                         // Reserved
    //    );

    //                  
    //if (FAILED(hres))
    //{
    //    cout << "Failed to initialize security. Error code = 0x" 
    //        << hex << hres << endl;
    //    CoUninitialize();
    //    return 1;                    // Program has failed.
    //}
    
    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    IWbemLocator *pLoc = NULL;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);
 
    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;
	
    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );
    
    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


    // Step 5: --------------------------------------------------
    // Set security levels on the proxy -------------------------

    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }


IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_LogonSession WHERE LogonType = 2 OR LogonType = 10"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
pSvc->Release();
return WBEM_S_FALSE; // Program has failed.
}

IWbemClassObject *pclsObj;
ULONG uReturn = 0;

while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

if(0 == uReturn)
{
//ErrorInfo(hres,11);
break;
}

VARIANT vtProp;
VariantClear(&vtProp);

// Get the value of the Name property
hr = pclsObj->Get(_bstr_t(L"LogonId"), 0, &vtProp, 0, 0);
printf("Logon ID : %s",vtProp.bstrVal);

wstring wstrQuery = L"Associators of {Win32_LogonSession.LogonId=";
wstrQuery += vtProp.bstrVal;
wstrQuery += L"} Where AssocClass=Win32_LoggedOnUser Role=Dependent";

IEnumWbemClassObject* pEnumerator1 = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(wstrQuery.c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator1);

IWbemClassObject *pclsObj1;
ULONG uReturn1 = 0;

HRESULT hr1 = pEnumerator1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn1);
if(0 == uReturn1)
{
//ErrorInfo(hres,11);
break;
}

VARIANT vtProp1;
hr1 = pclsObj1->Get(_bstr_t(L"Name"), 0, &vtProp1, 0, 0);
printf("User Name : %s",vtProp1.bstrVal);
}

return 0;
}


Try copying that entire function into your code and calling it -
still the same result?

Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape24-Oct-08 6:35
Supriya Tonape24-Oct-08 6:35 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery24-Oct-08 6:36
Mark Salsbery24-Oct-08 6:36 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape24-Oct-08 6:38
Supriya Tonape24-Oct-08 6:38 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery24-Oct-08 6:39
Mark Salsbery24-Oct-08 6:39 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape24-Oct-08 6:41
Supriya Tonape24-Oct-08 6:41 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery24-Oct-08 7:30
Mark Salsbery24-Oct-08 7:30 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape25-Oct-08 12:39
Supriya Tonape25-Oct-08 12:39 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Mark Salsbery25-Oct-08 13:04
Mark Salsbery25-Oct-08 13:04 
GeneralRe: [Urgent] Re: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape25-Oct-08 13:52
Supriya Tonape25-Oct-08 13:52 
GeneralRe: Getting NULL value from 'Win32_LogonSession' through WMI Pin
Supriya Tonape29-Oct-08 13:12
Supriya Tonape29-Oct-08 13:12 
GeneralRe: Getting NULL value from 'Win32_LogonSession' through WMI Pin
etaig15-Jun-10 22:33
etaig15-Jun-10 22:33 
QuestionHow to use CompletionPort to exchange data between two threads Pin
Electronic7522-Oct-08 9:38
Electronic7522-Oct-08 9:38 
AnswerRe: How to use CompletionPort to exchange data between two threads Pin
Roger Stoltz22-Oct-08 22:19
Roger Stoltz22-Oct-08 22:19 
Question[Message Deleted] Pin
Member 284578422-Oct-08 7:32
Member 284578422-Oct-08 7:32 
AnswerRe: IHTMLServices Pin
Mark Salsbery22-Oct-08 8:33
Mark Salsbery22-Oct-08 8:33 
General[Message Deleted] Pin
Member 284578422-Oct-08 8:42
Member 284578422-Oct-08 8:42 
JokeRe: IHTMLServices Pin
Mark Salsbery22-Oct-08 8:47
Mark Salsbery22-Oct-08 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.