Implementing MI Provider (3) - Generate Code
In blog Implementing MI Provider (2), we discussed schema definition of MI provider. Upon the schema ready, you can run Convert-MofToProvider.exe tool to generate MI provider skeleton code.
Where to install Convert-MofToProvider.exe tool?
Install windows 8 SDK first, then you could find the tool under SDK installation directory. For example, the file path on my desktop is "C:\Program Files (x86)\Windows Kits\8.0\bin\x64\convert-moftoprovider.exe"
Usage of Convert-MofToProvider.exe
C:\Program Files (x86)\Windows Kits\8.0\bin\x64>convert-moftoprovider.exe /? Generates provider source code (C) from MOF class definitions. Usage: Convert-MofToProvider.exe -MofFile <Mof files> -ClassList <Class list> -IncludePath <Path list> -InputParameterSet <Parameter Filename> [Other options] -MofFile <Mof files> Specifies list of mof files that contains the MOF definitions (or includes them), which are seperated by space. It must include any dependent MOF defintions as well (such as the CIM schema). -ClassList <Class list> Specifies list of the names of the MOF classes to be generated. If no class specified, it will generate all classes. -IncludePath <Path list> Specifies list of the directories, from which to search for included MOF files. If no include path specified, it will search current directory by default. -InputParameterSet <Parameter Filename> Specifies the parameter filename, which defines the input parameters. This option cannot be used with other options. Example: Convert-MofToProvider.exe -InputParameterSet param.txt Example of content for the parameter file, -MofFile a.mof -ClassList MSFT_A MSFT_B -IncludePath C:\stdmof [Other options] ...... EXAMPLES: The following example generates a 'MSFT_ComputerSystem' class, which is defined in schema.mof. Convert-MofToProvider -MofFile schema.mof -ClassList MSFT_ComputerSystem Convert-MofToProvider -MofFile schema.mof -ClassList MSFT_ComputerSystem -IncludePath C:\mof\cim222 |
NOTE: [Other options] part was omitted, you can run "convert-moftoprovider.exe /?" to get the detail usage.
Generate code
Here are the steps to generate provider code skeleton for "process" provider, one of the sample provider inside MI API sample.
First, setup up a working directory used to generate source code files, say C:\MIProvider
Second, make sure both sample.mof & MSFT_Qualifiers.mof present under working directory, you can copy both files from MI API sample to C:\MIProvider
Third, download DMTF standard schema, which is a zip file contains list of standard mof files. Say CIM Schema 2.26.0, unzip the standard schema to a directory, say C:\DMTF2260
Finally, run the following command line from an elevated command prompt console (cmd.exe)
Convert-moftoprovider.exe -MofFile C:\MIProvider\sample.mof -ClassList MSFT_WindowsProcess MSFT_WindowsServiceProcess -IncludePath C:\DMTF2260 C:\MIProvider -ExtraClass Cim_Error -OutPath C:\MIProvider -SkipLocalize
This command will,
- use the MOF file called sample.Mof that in the folder C:\MIProvider
- use the CIM 2.26 schema located in the folder identified by C:\DMTF2260
- use MSFT_Qualifiers.MOF located in the folder identified by C:\MIProvider
- generate skeleton code for the 2 classes following -Classlist
- include generated schema & types for the Cim_Error class
- place all output in the folder identified by the -OutPath parameter, i.e., C:\MIProvider
- skip the generation of resource IDs used for localization
- Generated provider will implement 2 classes, MSFT_WindowsProcess and MSFT_WindowsServiceProcess.
Also, the command line generates provider contains schema definition of parent classes and one extra class CIM_Error. Generated code files can be found under C:\MIProvider.
To better understand the generated files and its content, Figure 3.1 shows generated files' list for any MI provider.
Figure 3.1 Generated Files
While Strings.rc is optional , if -SkipLocalize option was specified, then Strings.rc will not be generated. <class name>.h and <class name>.c are generated for all involved classes for MI Provider. Take above command line for example, it is going to generate following classes specific files,
CIM_ConcreteJob.h
CIM_EnabledLogicalElement.h
CIM_Error.h
CIM_Job.h
CIM_LogicalElement.h
CIM_ManagedElement.h
CIM_ManagedSystemElement.h
CIM_Process.h
CIM_Service.h
CIM_ServiceProcess.h
MSFT_WindowsProcess.h
MSFT_WindowsServiceProcess.h
MSFT_WindowsProcess.c
MSFT_WindowsServiceProcess.c
Basically, <class name>.c files are only generated for the class name specified with -ClassList option, while <class name>.h files are generated for all class names specified with -ClassList option, -ExtraClass option, and all of their ancestor classes.
Next blog will discuss the content of each generated file and its purpose. Thanks.
Haowei Qin
Senior SDE
Standards Based Management