共用方式為


How to: Generate an MI Provider from a MOF File

Once you have configured the MI development environment and created a Microsoft Visual Studio project for your MI provider, the next step is to generate the MI provider source code files from your MOF file. This topic presents step-by-step instructions on performing that task using the Convert-MofToProvider tool.

Note

If you're new to MI provider development and/or just want to learn how to develop an MI provider and don't have a MOF file, the following steps provide a simple MOF file example.

  1. Open a command prompt.

  2. Change directories to the source directory of the Visual Studio created in How to: Create a Visual Studio Project for an MI Provider. If you are in the correct directory, you will see two files: projectName.vcxproj and projectName.vcxproj.filters.

  3. Copy your MOF file to the location located in the previous step. If you don't have a MOF file, the following MOF will suffice for a simple test. If that's the case, copy and paste the following contents into a file called process.mof and save it to the project's source code directory.

    #pragma include ("cim_schema_2.26.0.mof")
    #pragma include ("MSFT_Qualifiers.mof")
    
    // MSFT_WindowsProcess class derives from CIM_Process class,
    // which defines the schema for the processes running on windows OS.
    [ClassVersion("1.0.0")]
    class MSFT_WindowsProcess : CIM_Process
    {
     string CommandLine;
    
     [Description("This instance method demonstrates modifying the "
      "priority of a given process."
      "The method returns an integer value of 0 if the "
      "operation was successfully completed,"
      "and any other number to indicate an win32 error code.")]
     uint32 SetPriority([In] uint32 Priority);
    
     [static,
     Description("This static method demonstrates creating a process "
      "by supplying commandline to start a new process."
      "It will output the reference to the newly created process."
      "The method returns an integer value of 0 if the process "
      "was successfully created, and any other number to "
      "indicate an win32 error code.")]
     uint32 Create([In] string CommandLine, [Out] CIM_Process ref Process);
    };
    
  4. Run the Convert-MofToProvider tool to generate the source code files for your provider. In the following example, the tool is being called using values - such as for the MofFile and ClassList parameters - that represent the sample MOF file introduced in the previous step. If you're not using the sample MOF file, you'll need to adjust these values to represent the MOF file and its defined classes that you are using.

    Note

    The following call to the Convert-MofToProvider tool is split across multiple lines for display purposes. You should enter this as a single line.

    convert-moftoprovider -MofFile process.mof -ClassList MSFT_WindowsProcess 
    -IncludePath %CIM2260DIR% %MIINCLUDEDIR% -ExtraClass Cim_Error -SkipLocalize
    

    The following tables lists the files generated and their purpose.

    File(s) Purpose
    CIM_ManagedElement.h
    CIM_ManagedSystemElement.h
    CIM_LogicalElement.h
    CIM_Job.h
    CIM_Error.h
    CIM_ConcreteJob.h
    CIM_EnabledLogicalElement.h
    CIM_Process.h
    MSFT_WindowsProcess.h
    The header (.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.
    module.c
    Facilitates the following tasks:
    • Implements the MI provider entry function - MI_Main.
    • Implements the Load function that is called when the provider is being loaded.
    • Implements the UnLoad function that is called once the provider is being unloaded.
    MSFT_WindowsProcess.c
    Implements the intrinsic and extrinsic methods of the MSFT_WindowsProcess class that are defined in the MOF file.
    Intrinsic methods include Enumerate, Get, Delete, Modify, and Create.
    Extrinsic methods include SetPriority, Create, and RequestStateChange.
    [!Note]
    The RequestStateChange method is defined only in the parent class - CIM_EnabledLogicalElement - but not in the MSFT_WindowsProcess schema. Therefore, it is treated as not implemented. If the developer wants to implement this method within the MSFT_WindowsProcess, the RequestStateChange method must be redefined in the MSFT_WindowsProcess schema. To see an example of this, download the [MI API Samples](/samples/browse/?redirectedfrom=MSDN-samples) and look at how the MSFT_WindowsService::StopService method is implemented.

    schema.c
    Includes the full schema of all related classes. This file should never be modified. To change the schema, modify the original MOF and re-generate the code.
    WMIAdapter.c
    Contains the DLL entry point - DllMain - as well as other standard DLL functions.
    provider.def
    Defines list of exported APIs from MI provider DLL.
    strings.rc
    This resource file is generated only when you run the Convert-MofToProvider tool without specifying the SkipLocalize parameter and contains the localized strings for MI status and error messages. You should never modify this file directly as it is overwritten each time the Convert-MofToProvider tool is run without the --SkipLocalize parameter. If you need to define additional resources, create a second resource file and use the #include directive to merge the contents of the strings.rc files into the new file. For more information about resource files and using the #include directive, see the [About Resource Files](/windows/win32/menurc/about-resource-files) topic (Http://Go.Microsoft.Com/FWLink/p/?LinkID=308943).
  5. Once you've generated the MI provider from your MOF file, the next step is to build an MI provider using Visual Studio.