|
PingWbem
This sample
demonstrates the simplest WMI feature, connecting to the Windows Management
service.
This application is a
console application meant to be run at the command line. The code is designed to be easy to follow
and doesn't necessarily show a good practice for building 'real' WMI client
applications. Concentrate on the steps
and architect your application in a way that makes sense for you.
Using the PingWbem Sample
The PingWbem sample can
be built from the command line using NMAKE, or it can be built using Microsoft
Visual C++.
From the command line
in the sample installation directory, type the following:
NMAKE /f “Makefile”
From Microsoft Visual
C++:
1. Select File + Open Workspace
2. Select the PingWBEM.DSP file
Once built the PingWBEM sample application can be run from the command
line in the sample installation directory:
PingWBEM
The valid syntax for PingWBEM can be obtained as follows:
PingWBEM /?
With no parameters, the local machine is checked for the \\root\cimv2
namespace of the Windows Management service.
In order to check other machines, use a fully qualified namespace as
described in the /? help text.
Important Concepts in the
PingWbem Sample
Task: Connecting to the Windows Management service
Implementation:
All the code is in main.cpp. The basic steps are to create the
IwbemLocator and use it to connect to the server using the currently logged on
user's credentials. This sample just reports whether or not the Windows
Management service was found on the specific machine. The act of connecting
also tests network configuration, WinMgmt security configuration and the user's
access rights. Failure to properly configure any of these items will cause the
connection to be rejected.
General Notes
Things
to remember when you're building your own WMI client application:
1.
If you want your client to
run on NT and non-DCOM versions of Windows 95, manually load the ole32.dll and
see if CoInitializeSecurity() exists. This routine won’t exist on Windows 95
installations that don’t have DCOM installed separately. If this routine
doesn't exist, the asynchronous routines in this sample won’t work because of
mismatched security level problems. The synchronous techniques will still work.
2.
If you don’t care about
non-DCOM versions of Windows 95, you can define _WIN32_DCOM so that CoInitializeSecurity() is available for
implicit linking. Don't use _WIN32_WINNT to get this prototype since it won't compile
under the Windows 95/98 operating systems.
3.
In any case, the
CoInitializeSecurity() call (in InitInstance()) is required to work around a
security problem when WMI trying to call a Sink object but won't identify
itself. The CoInitializeSecurity() call turns off the authentication
requirement.
4.
WMI interfaces are defined
in wbemcli.h and wbemprov.h found in the wmi\include directory. You may #include both these files by
including just wbemidl.h located in the same directory.
5.
WMI interface CLSIDs are
defined in wbemuuid.lib. If you get unresolved externals in interfaces and
CLSIDs, this is what is missing.
6.
You'll need to link with
oleaut32.lib and ole32.lib to get the needed COM support.
7.
In the Link|Output
settings, specify 'wWinMainCRTStartup' as the entry point. This is per the
Unicode programming instructions.
8. If you're using the makefiles, don't forget to set the Visual C++ environment variables. This is done by running VCVARS32.BAT.