|
CmdLineConsumer
The CmdLineConsumer sample demonstrates
a generic event consumer. Based upon
the event received, this sample will spawn a user-defined application. Specifically, if an instance creation event
is detected, the CmdLineConsumer sample will invoke the Windows Notepad
application. This behavior is defined
in the sample MOF file, cmdline.mof.
Naturally, different event criteria can be defined and other applications
can be invoked by modifying the MOF file.
Building the CmdLineConsumer Sample
The application 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:
NMAKE /f "Makefile"
From Microsoft Visual
C++:
1.
Select File + Open Workspace
2.
Select the CmdLineConsumer.dsp file
Registering the Consumer to Receive Events
In order to register the event
consumer, the MOF must be compiled using the MOFCOMP program at the command line in the sample installation
directory:
mofcomp
cmdline.mof
In order to explicitly
register the consumer with COM, you may specify the /RegServer command line
switch when running the executable at
the command line in the sample installation directory:
Cmdlineconsumer.exe /RegServer
It should be noted,
however, that the executable would register itself automatically without
specifying this switch.
Notes About CmdLine.MOF
Within the MOF file, the
logical consumer is defined as:
class CmdLineEventConsumer
: __EventConsumer
{
[key] string Name;
[read, write]
string cmdLine;
[read, write]
uint8 showWindow;
};
Where:
·
Name is any convenient string.
·
cmdLine is the shell command line to be executed when this consumer
instance is called.
·
showWindow is a value used to control the command prompt window in
which the cmdLine will be run. These values are defined by the
ShowWindow() function in the Win32 API. For reference, they are:
#define SW_HIDE 0
#define SW_SHOWNORMAL 1
#define SW_NORMAL 1
#define SW_SHOWMINIMIZED 2
#define SW_SHOWMAXIMIZED 3
#define SW_MAXIMIZE 3
#define SW_SHOWNOACTIVATE 4
#define SW_SHOW 5
#define SW_MINIMIZE 6
#define SW_SHOWMINNOACTIVE 7
#define SW_SHOWNA 8
#define SW_RESTORE 9
#define SW_SHOWDEFAULT 10
NOTE: The showWindow
property of the CmdLineEventConsumer class only accepts numerical values and
will not be able to resolve the Win32 symbolic names (e.g., SW_MINIMIZE).
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 wbem\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.