Variables / pointers

Hi everybody I have only recently started programming in C++ and I am writing a program that queries multiple items from the WMI service. The query part I am ok with but when it comes to storing my results into variables, I seem to be getting some wierd results...
can anyone help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	char* WS_Name = WMIQuery("Computer Name", "Win32_ComputerSystem", "Name", ""); // Computer Name
	char* WS_Domain = WMIQuery("Domain Name", "Win32_ComputerSystem", "Domain", ""); // Domain Name
	char* WS_Manufacturer = WMIQuery("Computer Manufacturer", "Win32_ComputerSystem", "Manufacturer", ""); // Computer Manufacturer
	char* WS_Model = WMIQuery("Computer Model Number", "Win32_ComputerSystem", "Model", ""); // Computer Model Number
	char* WS_OS = WMIQuery("OS Name", "Win32_OperatingSystem", "Caption", ""); // OS Name
	char* WS_Arch = WMIQuery("OS Architecture", "Win32_OperatingSystem", "OSArchitecture", ""); // Architecture
	char* WS_HDDSize = WMIQuery("HDD Capacity", "Win32_LogicalDisk", "Size", " where drivetype = 3"); // HDD Capacity
	char* WS_HDDFree = WMIQuery("HDD Free Space", "Win32_LogicalDisk", "FreeSpace", " where drivetype = 3"); // HDD Free Space
	char* WS_RAM0 = WMIQuery("Ram 0 Size", "Win32_PhysicalMemory", "Capacity", " where tag = 'Physical Memory 0'"); // Ram 0
	char* WS_RAM1 = WMIQuery("Ram 1 Size", "Win32_PhysicalMemory", "Capacity", " where tag = 'Physical Memory 1'"); // Ram 1
	char* WS_RAM2 = WMIQuery("Ram 2 Size", "Win32_PhysicalMemory", "Capacity", " where tag = 'Physical Memory 2'"); // Ram 2
	char* WS_RAM3 = WMIQuery("Ram 3 Size", "Win32_PhysicalMemory", "Capacity", " where tag = 'Physical Memory 3'"); // Ram 3
	char* WS_User = WMIQuery("User Name", "Win32_ComputerSystem", "UserName", ""); // User logged in at time, if...
	
	// Display Variables to confirm
	printf("Display ALL Results...\n");
	printf("Machine Name: %ls\n", WS_Name);
	printf("Computer Name: %ls\n", WS_Domain); // Domain Name
	printf("Computer Manufacturer: %ls\n", WS_Manufacturer); // Computer Manufacturer
	printf("Computer Model Number: %ls\n", WS_Model); // Computer Model Number
	printf("OS Name: %ls\n", WS_OS); // OS Name
	printf("OS Architecture: %ls\n", WS_Arch); // Architecture
	printf("HDD Capacity: %ls\n", WS_HDDSize); // HDD Capacity
	printf("HDD Free Space: %ls\n", WS_HDDFree); // HDD Free Space
	printf("RAM #0: %ls\n", WS_RAM0); // Ram 0/1/2/3
	printf("RAM #1: %ls\n", WS_RAM1); // Ram 0/1/2/3
	printf("RAM #2: %ls\n", WS_RAM2); // Ram 0/1/2/3
	printf("RAM #3: %ls\n", WS_RAM3); // Ram 0/1/2/3
	printf("User logged in: %ls\n", WS_User); // User logged in at time, if... 

and my query function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
char* WMIQuery(char* QueryName, char* ClassName, char* PropertyName, char* QueryArgument)
{
	char* Result = "";
	// Display Current Objective
	printf("Looking for %s...\n", QueryName);

	// Create BSTR variable containing query
	CComBSTR querystr(OLESTR("SELECT * FROM "));
	querystr.Append(ClassName);
	querystr.Append(QueryArgument);
	BSTR WQLQuery = querystr.Detach();

	// Create BSTR variable containt propertyname
	CComBSTR propertystr;
	propertystr.Append(PropertyName);
	BSTR NameQuery = propertystr.Detach();
	
	// Use the IWbemServices pointer to make requests of WMI
	printf("Query %s Class...\n", ClassName);
    IEnumWbemClassObject* pEnumerator = NULL;
    hr = pSvc->ExecQuery(
        BSTR(L"WQL"), 
        BSTR(WQLQuery),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);
    
    if (FAILED(hr))  // Program has failed.
    {
        cout << "Query failed. " << "Error code = 0x" << hex << hr << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
    }

    else
    { 
		printf("Getting Value of %s property from class...",PropertyName);
        IWbemClassObject *pclsObj;
        ULONG uReturn = 0;
   
        while (pEnumerator)
        {
            hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

            if(0 == uReturn)
            {
				break;
            }

            VARIANT vtProp;

            // Get the value of the Name property from class
            hr = pclsObj->Get(NameQuery, 0, &vtProp, 0, 0);

			// Display Resuly
            wcout << "Result: " << vtProp.bstrVal << endl;
			
			// Copy result into a usable variable
			Result = vtProp.pcVal;
			
			// Clear vtprop
            VariantClear(&vtProp);
        }

    }
	printf("\n");
	return Result;
}
I think %ls means wide character string (wchar_t*). Try %s instead.
I tried that, but got only the first letter from each string
my results are as follows: :-s

Display ALL Results...
Machine Name: Domain
Computer Name: Manufacturer
Computer Manufacturer: Size
Computer Model Number: OSArchitecture
OS Name: SELECT * FROM Win32_MemoryArray
OS Architecture: simon
HDD Capacity: 2036916
HDD Free Space: 2037156
RAM #0: 2037396
User logged in: simon

if i display the variable after each run of the function, it gives me the right result, but for some reason loses the info at the end of the file :-s
Function WMIQuery shall be declared as

const char* WMIQuery( const char* QueryName, const char* ClassName, const char* PropertyName, const char* QueryArgument);

because you are using string literals as arguments of the function which in C++ have type const char[] and though in C they have type char[] they may not be changed. So always declare such parameters that accept string literals as const char *

I think that the problem is the result of such incorrect use of pointers to string literals. Also maybe somewhere in the program you use a pointer to a local variable that is destroyed and the value of the pointer becames invalid.
Last edited on
Topic archived. No new replies allowed.