4

i work with c++ programming, I use an example for understanding main of my question.

Suppose, we want get current username in windows operation system, we can use follow code :

#include <windows.h>
#include <Lmcons.h>

char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);

also, we can use wmi by follow the instruction explained on here and use Win32_ComputerSystem.UserName .

so, I hope you have fully understood, what's different between wmi and using api or any other way?

tank you for your response.

  • Try asking a question ;) – YSC Sep 24 '18 at 09:46
  • I guess you can find those differences in documentation... For example `ComputerSystem.UserName` is a wrapper for `GetUserNameW`. Note that returned string is Unicode rather than single-byte string that is being retrieved in the code snippet. – user7860670 Sep 24 '18 at 09:48

4 Answers4

5

disadvantage :

  • Speed (mainly disadvantage)
  • if user turn off wmi service, wmi doesn't work.

advantage :

  • Wraps the native API
  • richer data, if you use wmi, you can get rich data
  • standardized, all the 'entities' are represented in a standardized way

These are the most important issues for using wmi.

Jack D
  • 249
  • 1
  • 12
  • What does "richer" data mean? Simply that more data is available with WMI? or something else? – user673679 Sep 24 '18 at 10:13
  • 2
    @user673679 Since programmers can write their own WMI providers, you can get data from third party software. That's what Microsoft is doing in their security center interface-getting data from third-party antivirus and firewall softwares. – Jack D Sep 24 '18 at 12:19
3

Windows Management Instrumentation (WMI) is a set of specifications from Microsoft for consolidating the management of devices and applications in a network from Windows computing systems.

It is Microsoft implementation of Web-Based Enterprise Management. WMI - Services are installed on Windows OS, but the service can be turned off. So if user disabled the service you wont get any information about the system. It is just for reporting purpose.

Whereas the APIs are ways thru which the Microsoft provides the access to the information to local Application and some how you can also manipulate the information provided.

anup kataria
  • 109
  • 5
3

WMI is query Based and its run very slow where as API are much faster to run . Ex :- if you wanna check some System specification in your application before startup you should better use APIs . This will make you app Start faster.

WMI has advantage over api call WMI information is richer and easy to read where as to get that kind of same result we have to make several api calls .

AiverReaver
  • 164
  • 2
  • 10
3

The GetUserName API is merely a call to the function exported by the Advapi32.dll wich belongs to base kernel functions.

Using Win32_ComputerSystem class you are going to query Windows Management Instrumentation which is a complex and comprehensive infrastructure services which deals with most of the administrative tasks on Windows.

Posting a query to WMI involves much more resources and execution time so, if your goal is simply getting the user name, I suggest you to rely on the GetUserName API.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74