Visual Studio Development Bookmark and Share   
 index > Visual C++ Express Edition > How to get the status of Win32_OperatingSystem class using C++
 

How to get the status of Win32_OperatingSystem class using C++

Hi,

Anyone can give me solutions onhow to get the status of Win32_OperatingSystem class using C++.

Thanks,

Dodi

DondonDodi  Monday, May 12, 2008 7:46 PM
You can access WMI objects from C++ like you would access any COM object from C++. There are several samples on the MSDN showing just that: http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx

Sasha Goldshtein  Tuesday, May 13, 2008 7:28 PM
You can access WMI objects from C++ like you would access any COM object from C++. There are several samples on the MSDN showing just that: http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx

Sasha Goldshtein  Tuesday, May 13, 2008 7:28 PM

Thanks Sasha! I've done it. But there could be shorter process in getting the status of win32_operatingSystem class?

DondonDodi  Tuesday, May 13, 2008 9:37 PM
There is no shorter way if you want to access the WMI object itself from C++. If you want some specific bits of information from it, like the OS version, there are some Win32 APIs for that. What information are you looking for?
Sasha Goldshtein  Wednesday, May 14, 2008 10:29 AM

I see. I am getting the OS status firstbefore I can run the other functions. BTW, Sasha, I was wondering if I canreturn the OS status as process exit code? Because this information will be used by a third party for automation purposes. If you can picture, this is how I do it: from the main(), I made first the WMI to access the win32_OperatingSystem get the details up to step 7 and clean-up process. This is followed by my application before the main() exits.

main() { WMI Steps 1 to 7,cleanup,MyApplication}

Thanks.

DondonDodi  Wednesday, May 14, 2008 1:43 PM

What do you mean by "OS status"?

Sasha Goldshtein  Wednesday, May 14, 2008 2:32 PM

Operating System status from Win32_OperatingSystem.

DondonDodi  Wednesday, May 14, 2008 5:44 PM
If you mean the Status field (string) of the Win32_OperatingSystem class (http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx), then you can only query it through WMI AFAIK. If by status you mean the OS version, platform, build etc. then there are other APIs.

Sasha Goldshtein  Wednesday, May 14, 2008 7:08 PM

Yes, the Status field (string) of the Win32_OperatingSystem class. I can access now the Win32_OperatingSystem class and display the status.By the way, how would I insert my function? First, I need to connect inorder to check the status. if the status is ok/ready then terminate program else check if it isa timeout. If timeout return something else run my function then check the status again until successful. Thanks.

DondonDodi  Friday, May 16, 2008 1:11 PM

I'm not sure what you expect as an answer. Do you expect us to write code for you?

Sasha Goldshtein  Saturday, May 17, 2008 3:42 PM

No need, I have figured it out. Well, thanks for the help.

DondonDodi  Monday, May 19, 2008 1:40 PM

Hi Sasha,

I already know how to get the status of Win32_OperatingSystem. What command or how do I make conditional statements to check whether it maches the compared statement? Based from the msdn.microsoft.com:

Code Snippet IWbemClassObject *pclsObj;
ULONG uReturn = 0;

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

if(0 == uReturn)
{
break;
}

VARIANT vtStat;

// Get the value of the status property
hr = pclsObj->Get(L"Status", 0, &vtStat, 0, 0);

VariantClear(&vtStat);

//this is my condition.

if(hr == ___) //if status is OK

return 0;

else//when status is not OK

return 1;

}


I put blank because I'm still searching for the command to use. Is the format correct? Thanks.

DondonDodi  Wednesday, May 21, 2008 2:54 PM

hr is the COM error code that indicates whether you actually succeeded at retrieving the Status property. It doesn't convey the contents of the Status property, just the fact that you have or haven't successfully retrieved it.

vtStat in your code is the variant that contains the property value. Check out the documentation for the VARIANT structure to see how to read a string value from it.

Sasha Goldshtein  Wednesday, May 21, 2008 5:09 PM

Hi Sasha,

I have revised my code below:

//getting data

IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
int nResult = 0;


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

if(0 == uReturn)
{
break;
}

for (;Wink
{
VARIANT vtStat;

// Get the value of the status property
BSTR strClsProperty = SysAllocString(L"Status");
hr = pclsObj->Get(strClsProperty, 0, &vtStat, 0, 0);
SysFreeString(strClsProperty);


_bstr_t bstrStat = &vtStat;
char * strStat = (char *) bstrStat;

if (strStat = "OK"){
nResult;
break;}

DWORD dwStart = GetTickCount();
DWORD dwTimeout = num1;//num1 will be from thechar **argv in the int main()

if (GetTickCount() - dwStart > dwTimeout){
nResult = 3;
break;}

Sleep(200);

VariantClear(&vtStat);
}
}

//clearing

I have observed that when i run this code in the batchfile, it will terminate until the status is ok or timeout even though i entered a certain time to sleep. It also depends on the workstation how fast it boot up. The faster the computer boots up the lesser the timeout will reflect. how would i synchronize this from the input or this will terminate until the OS is in OK status?

Thanks.

DondonDodi  Friday, May 30, 2008 6:28 PM

strStat = "OK" doesn't compare the strings. In fact, == doesn't compare the strings either, it only compares pointers. You need to use a string comparison function like strcmp to compare strings.

There are other issues with this code. I suggest that you review C and C++ programming. I'm not trying to be rude, but WMI is not the biggest of your concerns here.

Sasha Goldshtein  Saturday, May 31, 2008 9:59 AM

Hi Sasha,

I have questions regarding getting WMI data from local computer. I have read in the MSDN how to get the WMI data, but when I check the time frame it takes time before it terminate the program. It was located in step 4, connectServer(). Is there other way to get rid of this time delay? Or are there way to get rid of this delay?

Thanks,

Dodi

DondonDodi  Friday, June 06, 2008 4:52 PM

You can use google to search for other answers

Custom Search

More Threads

• Returning References
• I Can't get cout to work
• Opening Form2 when a button is clicked ...
• Executing a program
• I am unable to create a new project using visual c++ 2008 express
• Windows Platform SDK
• Noob to VC++ looking to get started...
• using server and a client with udp
• how do i share my program?
• Detecting Windows File System Activity