|
I just looked at this link
Example: Getting WMI Data from the Local Computer[^]
and it looks like query text just like I used.
The "while (pEnumerator) " loop is the same as my foreach loop.
Where's the problem?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yes I am using the same for looping. But I am not getting 'Name'field when I query diretly to 'Win32_LogonSession'class. There is some other way for it.. I am new to WMI stuff.. thru Win32_LoggedOnUser i shud get it but I am exploring it...I will try the query you had given me above..
Regards,
Supriya Tonape
|
|
|
|
|
Nope that query is not working. I am getting 'Win32_LogonSession.LogonId' after executing query : "SELECT * FROM Win32_LogonSession WHERE LogonType = 2 OR LogonType = 10"
what are next steps ? (in C++)
Regards,
Supriya Tonape
|
|
|
|
|
Supriya Tonape wrote: I am getting 'Win32_LogonSession.LogonId'
I thought you were already able to get the LogonId and you needed
to get the name?
To get the user name from the Win32_LoggedOnUser class, you need to do the
second query I showed you using the LogonId from the query you've already done.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yes I am getting LogonId ! Second query is not working for me...
Regards,
Supriya Tonape
|
|
|
|
|
What does the code look like?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It returns S_FALSE at line "HRESULT hr1 = pEnumerator1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn1);"
which is in last. I really donno if I doing right things... plz have a look,,
int WMIScannerModule::GetRemoteLoggedOnUsers(IWbemServices *pSvc,pWMIScanParams pScanParams)
{
HRESULT hres = WBEM_S_NO_ERROR;
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 = pclsObj->Get(_bstr_t(L"Win32_LogonSession.Name"), 0, &vtProp1, 0, 0);
printf("User Name : %s",vtProp1.bstrVal);
}
return 0;
}
|
|
|
|
|
Excellent thank you
Change
hr1 = pclsObj->>Get(_bstr_t(L"Win32_LogonSession.Name"), 0, &vtProp1, 0, 0);
to this (changes marked in red)
hr1 = <code>pclsObj1</code>->Get(_bstr_t(L"<code>Name</code>"), 0, &vtProp1, 0, 0);
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I had already tried that! It's not working as 'hr1' is getting as S_FALSE error and so 'vtProp1' is NULL for the time being...
HRESULT hr1 = pEnumerator1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn1);
VARIANT vtProp1;
hr1 = pclsObj1->Get(_bstr_t(L"Name"), 0, &vtProp1, 0, 0);
Regards,
Supriya Tonape
|
|
|
|
|
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;
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;
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1;
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
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;
}
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
VariantClear(&vtProp);
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)
{
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++
|
|
|
|
|
Mark )))))))))))))))))))))))))) U are genious !
I am getting the name now )thank you so much!!!!!!!
Have a gr8 day ahead !
Best Regards,
Supriya Tonape.
|
|
|
|
|
You're welcome.
You have a great day as well!
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am still wondering what exact mistake i was doing, all flags were set properly.. let me check in detail so I wont make it in future.
Thx so much again. !
Regards,
Supriya Tonape.
|
|
|
|
|
Also, if you remove the "WHERE LogonType = 2 OR LogonType = 10"
from the outer query, you can see all logon sessions, not just the
interactive ones.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
yup Mark.
You really made my day U have gr8 weekend ahead!
Best Regards,
Supriya.
|
|
|
|
|
Supriya Tonape wrote: U have gr8 weekend ahead!
Thank you....you too!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Also in this code though I have kept 'logontype' as 2 and 10 to get only active user names, but it;s returning me all users those are Disc earlier...
|
|
|
|
|
Supriya Tonape wrote: but it;s returning me all users those are Disc earlier..
Which means what?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
actually i will check it.. I just need usernames who has active session.
|
|
|
|
|
Hi Mark,
I just realized that 2nd query which forms like "Associators of {Win32_LogonSession.LogonId=1121214} Where AssocClass=Win32_LoggedOnUser Role=Dependent" works for local machine but when I try to execute it on remote machine after above query executes, at next line it fails... Query execution passes but enumeration fails.. at below line,
HRESULT hr1 = pEnumerator1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn1);
I have been trying different things to check out but no luck, any idea why it cud be happening or is there any change in query while running on remote machine ?
Regards,
Supriya Tonape
|
|
|
|
|
Hi All,
For some reason, when I add the following lines, to get the StartTime property value, the get function fails. Any ideas?
VARIANT vtProp2;
HRESULT hr2 = pclsObj1->Get(L"StartTime", 0, &vtProp2, 0, 0);
if(hr1 == S_OK)
{
SYSTEMTIME t;
int res = VariantTimeToSystemTime(vtProp2.dblVal, &t);
}
|
|
|
|
|
Hello, I read the MSDN but there were no example and I didn't completely understand how to use it.
I have two threads that both send data to the other thread and receive data from it.
Should I create IoCompletionPort in both threads and use ReadFileEx and WriteFileEx t read and write to/from the i/o completion port?
I'm confused because in MSDN has said that one shuld not use ReadFileEx & WriteFileEx. It says
<quote>
"After an instance of an open file is associated with an I/O completion port, it cannot be used in the ReadFileEx or WriteFileEx function."
Then How should I read and write to I/O Completion port? I'm confused can someone explain a bit please.
thanks.
|
|
|
|
|
Read this excellent article[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
|
I don't know anything about it, nor have I heard of it.
This is the closest thing I can find in MSDN:
About MSHTML[^]
If that's completely unrelated, then please kindly disregard
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|