====================================================================================
SERVICE SAMPLE
====================================================================================
The current directory contains source code demonstrating an end to end scenario of
the advanced features of MIAPI.
NOTE: Please see ..\ReadMe.txt (Section 2. PREPARATION) for instructions on how to
create environment variables which will be used in this sample.
====================================================================================
INSTRUCTIONS
====================================================================================
There are multiple sections below that correspond to each of the sub-folders in this
folder. The sections and folders are
1. Provider
2. NativeClient
3. ManagedClient
4. CIM-BasedCmdlet\WindowsService
5. CIMCmdlet
1. Provider
------------------------------------------------------------------------------------
This directory contains the provider code, most of which are generated by
Convert-MofToProvider.exe tool from SDK.
Following shows you how to generate provider code, implement the provider,
compile the provider, and how to deploy the provider to a Windows 8 or Windows
Server 2012 machine.
1) Generate provider code skeleton
Run the following command line from an elevated command prompt (cmd.exe)
%SDKBINDIR%\convert-moftoprovider.exe -MofFile %MISAMPLEDIR%\sample.mof -ClassList MSFT_WindowsService MSFT_WindowsServiceManager -IncludePath %CIM2260DIR% %MISAMPLEDIR% -ExtraClass Cim_Error -OutPath %MISAMPLEDIR%\service\provider\temp
This command will:
- use the MOF file called Sample.Mof that in the folder identified by %MISAMPLEDIR%
- generate skeleton code for the 2 classes following -Classlist
- use the CIM 2.26 schema located in the folder identified by %CIM2260DIR%
- use MSFT_Qualifiers.MOF located in the folder identified by %MISAMPLEDIR%
- include generated schema & types for the Cim_Error class
- place all output in the folder identified by the -OutPath parameter
- Generate generation of resource IDs (strings.rc) used for localization
Generated code can be found under %MISAMPLEDIR%\service\provider\temp.
Following lists the generated files, with notes for each file.
GENERATED FILES
---------------
NOTE: All .h files define class types and helper functions to
manipulate instances of the class, such as set/clear the property
value, construct, clone and destruct.
CIM_ConcreteJob.h
CIM_EnabledLogicalElement.h
CIM_Error.h
CIM_Job.h
CIM_LogicalElement.h
CIM_ManagedElement.h
CIM_ManagedSystemElement.h
CIM_Service.h
MSFT_WindowsService.h
MSFT_WindowsServiceManager.h
module.c
Defines and implements entry function (MI_Main) of the provider.
Implements function (Load) which will be called once the provider
is being loaded.
Implements function (UnLoad) which will be called once the provider
is being unloaded.
MSFT_WindowsService.c
Implements the intrinsic and extrinsic methods of MSFT_WindowsService.
Intrinsic methods include Enumerate/Get
Extrinsic methods include StopService/StartService.
MSFT_WindowsServiceManager.c
Implements extrinsic static method GetWindowsServices, which
demonstrates stream feature.
schema.c
Includes full schema of all related classes. This file should not be
modified. To change the schema, modify the original MOF (in this case
sample.mof) and re-generate the code.
WMIAdapter.c
To enable this provider build based on MI API running with WMI,
this file implements several adapter functions.
Provider.DEF
Defines list of exported APIs from service provider.
strings.rc
String table contains all localizable strings
OTHER FILES USED BY THIS SAMPLE
-----------
WindowsService.c
This is provided as part of the sample code, and was created to show
the most efficient way of working with the generated code. The business logic
for the elements in MSFT_WindowsService.c are implemented in WindowsService.c.
If the schema is extended or updated, a new version of MSFT_WindowsService.c
will need to be generated. By placing the business logic in a separate file,
the work to align the updates is significantly reduced.
WindowsService.h
service.vcxproj
Visual studio project file for service provider.
2) Implement & compile the provider
See *.c file for the implementation, especially MSFT_WindowsService.c and
MSFT_WindowsServiceManager.c files.
Open %MISAMPLEDIR%\misample.sln file with visual studio 2012 and
make sure following settings are correct, then you will be able to build.
- Click menu Build->Configuration Manager, select desired Active solution platform,
"x64" for example.
- Right Click "service" project and select properties.
- Goto 'Configuration Properties' -> Linker -> Input -> Additional Dependencies
Make sure "mi.lib;%(AdditionalDependencies)" was added there.
NOTE: If the target windows 8 is 32bit (x86), then you need to set Active solution platform as Win32.
3) Generate MUI file
Once the project above is built, you will have created a new provider called service.DLL.
The compiled service.dll contains language specific content and language neutral binary.
Language resource (strings) are defined in strings.rc file, which contained auto generated
resource string for localizable qualifier's value and self defined resource string used by
service provider.
In order to support localization of the resource string, Register-CimProvider tool request
supplying both language neutral provider binary and resoure MUI file for that provider.
Run following command line to split service.dll into neutral DLL and mui file
%SDKBINDIR%\muirct.exe -q %MISAMPLEDIR%\service\provider\mui.rcconfig -v 2 -x 0x0409 -g 0x0407 service.dll .\temp\service.dll .\temp\service.dll.mui
4) Register provider
Copy the .\temp\service.DLL and .\temp\service.dll.mui files to a Windows 8
or Windows Server 2012 machine. Suppose service.dll was copied to c:\temp folder,
then service.dll.mui should be copied to c:\temp\en-us folder. Then invoke
the following command line from elevated commandline prompt (cmd.exe):
Register-CimProvider.exe -Namespace Root/Standardcimv2/sample -ProviderName service -Path service.dll -verbose -ForceUpdate -Localize en-us
Once that completes, service.dll should be successfully registered into system with
localized qualifiers.
NOTE: Register-CimProvider.exe is shipped in Windows 8 and Windows Server 2012, in
%windir%\system32.
2. NativeClient
------------------------------------------------------------------------------------
See ReadMe.txt for details.
3. ManagedClient
------------------------------------------------------------------------------------
See ReadMe.txt for details.
4. CIM-BasedCmdlet\WindowsService
------------------------------------------------------------------------------------
This directory contains a set of files that defines CIM-Based cmdlets.
The cmdlets interact with MSFT_WindowsService and MSFT_WindowsServiceManager
implemented by the service provider.
- WindowsService.cdxml defines CIM-based cmdlets for MSFT_WindowsService class
- WindowsService.format.ps1xml defines how to display MSFT_WindowsService instances
- WindowsService.psd1 is the module manifest to package all of the above into a PowerShell module
To use the sample
- Make sure that you have built and registered the Service provider using the
instructions above.
- Copy all the files to a local folder of the targeted Windows 8 or Windows Server 2012 machine
for example c:\temp\CIM-BasedCmdlet\WindowsService
- Open a powershell.exe console and invoke the following commands:
Import-Module C:\temp\CIM-BasedCmdlet\WindowsService
Get-WindowsService
5. CIMCmdlet
------------------------------------------------------------------------------------
This directory contains a ps1 script file, which demonstrate how to use CimCmdlets
to consume the data from the service provider.
To run the script, just open powershell.exe, and paste content of the PS1 file
to PowerShell console.