How to call WMI using MinGW

2019-05-07 05:11发布

问题:

After days of searching the web (Including MSDN) for a way to get Hardware unique IDs using C++, I found that I need to use WMI. Actually I could find some good examples as well like this one. The problem is: I cannot compile it using MinGW however it rund pretty well on Visual Studio using Microsoft compiler, but with MinGW (which my application is compiled with) it gives me dozens of errors. This could sound an old question but unfortunately I couldn't find a solution so far after days of searching the web. I am using MinGW on Windows 7.

回答1:

MinGW should allow you to access the Win32 api's without needing Visual Studio.

The following should compile and run with MinGW.

#include <windows.h>
#include <stdio.h>

int main()
{
   SYSTEM_INFO siSysInfo;

   // Copy the hardware information to the SYSTEM_INFO structure. 

   GetSystemInfo(&siSysInfo); 

   // Display the contents of the SYSTEM_INFO structure. 

   printf("Hardware information: \n");  
   printf("  OEM ID: %u\n", siSysInfo.dwOemId);
   printf("  Number of processors: %u\n", 
      siSysInfo.dwNumberOfProcessors); 
   printf("  Page size: %u\n", siSysInfo.dwPageSize); 
   printf("  Processor type: %u\n", siSysInfo.dwProcessorType); 
   printf("  Minimum application address: %lx\n", 
      siSysInfo.lpMinimumApplicationAddress); 
   printf("  Maximum application address: %lx\n", 
      siSysInfo.lpMaximumApplicationAddress); 
   printf("  Active processor mask: %u\n", 
      siSysInfo.dwActiveProcessorMask); 

   return 0;
}

Additional reading to help you get started.



回答2:

MinGW has no support for WMI. There are some efforts in MinGW64 but it still can't be called complete (for example, wbemuuid.lib is missed).

Some projects solve this problem by extending MinGW with missed WMI libraries from Microsoft WMI SDK or MinGW64 (see this project or this thread for examples).

Another solution for getting device UIDs for your concrete task may be using of Windows Setup API that is well supported by MinGW. A great example of using it may be found on CodeProject site.



回答3:

Check out the Microsoft Reference

Win API System Information