By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,056 Members | 472 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,056 IT Pros & Developers. It's quick & easy.

C++11 version of Rabbit's outstanding WMI tutorial

SwissProgrammer
P: 98
Here is the 2013 tutorial https://bytes.com/topic/access/insig...n-wmi-tutorial

It looks a lot like what I used to do in VB6.

I am now learning C++11 on a 32 bit Windows computer with CodeBlocks 17.12 .

I would like to see Rabbit's article about Windows Management Instrumentation (WMI) in straight C++11.

Someone, please post this in C++11 (or an earlier version if you like).

Thank you.
1 Week Ago #1
Share this Question
Share on Google+
9 Replies

Banfa
Expert Mod 5K+
P: 8,980
Apparently your Google-fu is weak 😛

Does this help Creating a connection to a WMI namespace
1 Week Ago #2

SwissProgrammer
P: 98
Thank you Banfa.

Without my having explained more, I see logic in your answer, but; it is specifically my C++11-fu that what is weak. docs.Microsoft.com/etc - been there, done that, read lots of the linked pages, returned here to search 'here' (since I get some very clear answers 'here') and found Rabbit's outstanding tutorial which looked a lot like what I used to be able to do years ago.

As a beginner at C++11, I have sometimes had difficulty understanding and using various explanations by Microsoft on their docs pages. Years ago I used WMI a lot in VB6. Now I have a project that I am coding in C++11 and I seem to get about one line of code per maybe 3 days of reading and studying.

I took a chance that maybe someone that actually knows (like you) C++ at expert level might post an article for beginners (like me) that is in the same format and ease of understanding as the Rabbit tutorial.

Again, thank you Banfa. Your answer was logical, concise, and addressed a hole in my original question, which was my fault.

Now, if I have correctly filled in a missing part of my original question better, would you or someone post that article as I asked?

I and future readers might like to see and reference it.

Please.

Thank you.
1 Week Ago #3

dev7060
Expert 100+
P: 313
I remember achieving some of the results by simply passing batch commands.

system("tasklist");
system("sc query");
system("taskkill -im xxxx"); ...etc.
1 Week Ago #4

SwissProgrammer
P: 98
I am attempting to answer my original question myself.

The following is within that attempt:

I went to Banfa's link and now I have another two more questions.

The use of Synchronous vs Asynchronous in getting WMI data.

This stopped me in the past, and after lots of study, I still did not grasp exactly what is the best choice for my current application.

(1)
If I have a program, an exe made with C++11, and it is running on a local computer, and all that it is doing is getting local WMI data, should I get WMI Data from the Local Computer synchronously or Asynchronously? And why? I think that I should use synchronous, but that is just a guess, and to guess is not good enough. I want to know.


(2)
If I have a program, an exe made with C++11, and it is running on a local computer, and I have some intense graphics being done in threads and buffers, and other stuff being done in threads, should I get WMI Data from the Local Computer synchronously or Asynchronously? And why? I think that I should again use synchronous, because it seems to get the data in one complete process, and maybe not use Asynchronous because it might allow a WMI data set to be changed before the entire process of getting the data is complete, but that is just a guess, and to guess is not good enough. I want to know.


Now I have read at https://blogs.iis.net/gholman/group-...d-logon-impact what I think is saying that for some WMI processes running in the foreground synchronously, that "A few long-running queries can extend the amount of time it takes to finish processing" etc. and slow down my program doing other things. Maybe I should be using WMI Asynchronously whenever I have more than a WMI running in the same program. Help!

I found another article which seems to say that sometimes synchronous processing can be "harmful" https://mcsimm.blogspot.com/2009/

It looks like I will be attempting to answer my own original question with Asynchronous WMI.

But, I am still not certain about this.

Comments and guidance from the learned? Please.
1 Week Ago #5

SwissProgrammer
P: 98
dev7060,

Thank you.

I have been using a CLI (accessed from within my C++11 program which in itself is using a GUI for other things) to get the local computer's information. It works, but I want to learn WMI.

But, what I was doing in the CLI slowed down my program and so I put that into a thread. It seemed to speed up my program, but now I am thinking about using WMI and I do not know it in C++11. So, I am asking, studying, and attempting to learn.

Thank you dev7060.
1 Week Ago #6

Banfa
Expert Mod 5K+
P: 8,980
OK here is a start for 10 (but not fully working throwing some strange windows error

You need to call CoInitialize and CoUninitialize as your program starts/stops

Expand|Select|Wrap|Line Numbers
  1. #ifndef WMIACCESS_H
  2. #define WMIACCESS_H
  3.  
  4. #define _WIN32_DCOM
  5. #include <wbemidl.h>
  6.  
  7. #include <vector>
  8. #include <string>
  9.  
  10. class WMIAccess
  11. {
  12.     public:
  13.         WMIAccess(BSTR ns = BSTR(L"ROOT\\DEFAULT"));
  14.         virtual ~WMIAccess();
  15.  
  16.         WMIAccess(const WMIAccess&) = delete;
  17.         const WMIAccess& operator=(const WMIAccess&) = delete;
  18.  
  19.         std::vector<std::string> QueryDisks();
  20.  
  21.     protected:
  22.  
  23.     private:
  24.         IWbemLocator*   _Locator;
  25.         IWbemServices*  _Service;
  26. };
  27.  
  28. #endif // WMIACCESS_H
  29.  
Expand|Select|Wrap|Line Numbers
  1. #include "WMIAccess.h"
  2.  
  3. #include <iostream>
  4. #include <sstream>
  5. #include <stdexcept>
  6.  
  7. WMIAccess::WMIAccess(BSTR ns) :
  8.     _Locator(nullptr),
  9.     _Service(nullptr)
  10. {
  11.     HRESULT hr;
  12.  
  13.     hr = CoCreateInstance(CLSID_WbemLocator, nullptr,
  14.         CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &_Locator);
  15.  
  16.     if (FAILED(hr))
  17.     {
  18.         std::ostringstream oss;
  19.         oss << "Failed to create IWbemLocator object. Err code = 0x" << std::hex << hr;
  20.         std::cerr << oss.str() << std::endl;
  21.         CoUninitialize();
  22.         throw std::runtime_error(oss.str());
  23.     }
  24.  
  25.     // Connect to the root\default namespace with the current user.
  26.     hr = _Locator->ConnectServer(
  27.             ns,             //namespace
  28.             nullptr,        // User name
  29.             nullptr,        // User password
  30.             nullptr,        // Locale
  31.             0,              // Security flags
  32.             nullptr,        // Authority
  33.             nullptr,        // Context object
  34.             &_Service);     // IWbemServices proxy
  35.  
  36.     if (FAILED(hr))
  37.     {
  38.         std::ostringstream oss;
  39.         oss << "Could not connect. Error code = 0x" << std::hex << hr;
  40.         std::cerr << oss.str() << std::endl;
  41.         _Locator->Release();
  42.         CoUninitialize();
  43.         throw std::runtime_error(oss.str());
  44.     }
  45. }
  46.  
  47. WMIAccess::~WMIAccess()
  48. {
  49.     _Locator->Release();
  50.     CoUninitialize();
  51. }
  52.  
  53. std::vector<std::string> WMIAccess::QueryDisks()
  54. {
  55.     std::vector<std::string> results;
  56.     IEnumWbemClassObject* pEnumerator;
  57.     HRESULT hr ;
  58.  
  59.     hr = _Service->ExecQuery(BSTR(L"WQL"),
  60.                         BSTR(L"SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3"),
  61.                         WBEM_FLAG_RETURN_IMMEDIATELY|WBEM_FLAG_FORWARD_ONLY,
  62.                         nullptr,
  63.                         &pEnumerator);
  64.  
  65.     if (FAILED(hr))
  66.     {
  67.         std::ostringstream oss;
  68.         oss << "Could not execute query. Error code = 0x" << std::hex << hr;
  69.         std::cerr << oss.str() << std::endl;
  70.         throw std::runtime_error(oss.str());
  71.     }
  72.  
  73.     IWbemClassObject *pclsObj = NULL;
  74.     ULONG uReturn = 0;
  75.  
  76.     while (pEnumerator)
  77.     {
  78.         hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
  79.  
  80.         if (FAILED(hr))
  81.         {
  82.             std::ostringstream oss;
  83.             oss << "Could get next from enumerator. Error code = 0x" << std::hex << hr;
  84.             std::cerr << oss.str() << std::endl;
  85.             throw std::runtime_error(oss.str());
  86.         }
  87.  
  88.         if(0 == uReturn)
  89.         {
  90.             break;
  91.         }
  92.  
  93.         VARIANT vtProp;
  94.  
  95.         // Get the value of the Name property
  96.         hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
  97.         results.push_back(vtProp.pcVal);
  98.         VariantClear(&vtProp);
  99.  
  100.         pclsObj->Release();
  101.     }
  102.  
  103.     return results;
  104. }
  105.  
I may have used C++14 rather than C++11, sorry but I really have to stop and make my family tea now.

I dea behind this class was it created the connection as part of instantiation and then you could add functions to perform specific tasks, like enumerate disks.
1 Week Ago #7

SwissProgrammer
P: 98
Banfa,

I tried various ways, but I did not get that to work on XP.

And for your previous link: I tried that but it requires wbemidl.h which XP does not have.

Thank you for the offer.
1 Week Ago #8

Banfa
Expert Mod 5K+
P: 8,980
Ah, Windows XP, that changes the landscape a bit but the libraries and headers should be available even if they aren't automatically included. Which compiler are you using?

Have to say it's been about 15 years since I tried to write anything for Windows, apart from portable C++ that runs anywhere.
1 Week Ago #9

SwissProgrammer
P: 98
Banfa,


GNU GCC Compiler

C Compiler:
mingw32-gcc.exe
C++ compiler
mingw32-g++.exe

on XP Pro sp2 (I want my code to be compilable later on Unix and on Mac in 32 bit and in 64 bit).
1 Week Ago #10

Post your reply

Sign in to post your reply or Sign up for a free account.