2025-11-28 00:35:46 +09:00

224 lines
10 KiB
Plaintext

====================================================================================
PROCESS SAMPLE
====================================================================================
The current directory contains source code demonstrating an end to end scenario of
the basic 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\WindowsProcess
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_WindowsProcess MSFT_WindowsServiceProcess -IncludePath %CIM2260DIR% %MISAMPLEDIR% -ExtraClass Cim_Error -OutPath %MISAMPLEDIR%\process\provider\temp -SkipLocalize
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
- skip the generation of resource IDs used for localization (this is covered
in another sample)
Generated code can be found under %MISAMPLEDIR%\process\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_Process.h
CIM_Service.h
CIM_ServiceProcess.h
MSFT_WindowsProcess.h
MSFT_WindowsServiceProcess.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_WindowsProcess.c
Implements the intrinsic and extrinsic methods of MSFT_WindowsProcess.
Intrinsic methods include Enumerate/Get/Delete/Modify/Create
Extrinsic methods include SetPriority/Create/RequestStateChange.
NOTE: RequestStateChange method is only defined in parent class
CIM_EnabledLogicalElement but not defined in MSFT_WindowsProcess schema,
so it is treated as NOT implemented. If MSFT_WindowsProcess wants to
implement this method, it has to refine the method in its schema, see
MSFT_WindowsService::StopService method for example.
MSFT_WindowsServiceProcess.c
Implements association class MSFT_WindowsServiceProcess.
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 process provider.
OTHER FILES USED BY THIS SAMPLE
-----------
WindowsProcess.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_WindowsProcess.c are implemented in WindowsProcess.c.
If the schema is extended or updated, a new version of MSFT_WindowsProcess.c
will need to be generated. By placing the business logic in a separate file,
the work to align the updates is significantly reduced.
WindowsProcess.h
WindowsServiceProcess.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 in the same way that
WindowsProcess.c does.
WindowsServiceProcess.h
helper.c
Implements helper functions.
Helper.h
process.vcxproj
Visual studio project file for process provider.
2) Implement & compile the provider
See *.c file for the implementation, especially MSFT_WindowsProcess.c and
MSFT_WindowsServiceProcess.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 "Process" project and select properties.
- Goto 'Configuration Properties' -> Linker -> Input -> Additional Dependencies
Make sure "psapi.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) Register provider.
Once the project above is built, you will have created a new provider called Process.DLL.
Copy the compiled process.DLL to a Windows 8 or Windows Server 2012 machine, and invoke
the following command line from elevated commandline prompt (cmd.exe):
Register-CimProvider.exe -Namespace Root/Standardcimv2/sample -ProviderName process -Path process.dll -verbose -ForceUpdate
Once that completes, process.dll should be successfully registered into system.
NOTE: Register-CimProvider.exe is shipped in Windows 8 and Windows Server 2012, in
%windir%\system32.
2. NativeClient
------------------------------------------------------------------------------------
This directory contains a console application implementation,
which demonstrates how to use the MI client API to consume the data
from a WMI provider such as the one created above.
1) Build
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 "SampleNativeMiClient" project and select properties.
- Goto 'Configuration Properties' -> Linker -> Input -> Additional Dependencies
Make sure "mi.lib;%(AdditionalDependencies)"; was added there.
2) Run
Copy compiled SampleNativeMiClient.exe to windows 8 or Windows Server 2012 machine,
run it from an elevated commandline prompt. Follow the menu to perform any of the listed
operations against the provider.
3. ManagedClient
------------------------------------------------------------------------------------
This directory contains a console application implementation,
which demonstrate how to use MI client DotNet API to consume the data
from provider.
1) Build
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.
- Click "References" node of "SampleDotNetClient" project.
- Correct reference of "Microsoft.Management.Infrastructure"
Usually you need to delete "Microsoft.Management.Infrastructure" node, and
add reference from %SDKMIDOTNETDIR%.
2) Run
Copy compiled SampleDotNetClient.exe to a Windows 8 or Windows Server 2012 machine,
and run it under elevated commandline prompt. Follow the menu to perform desired
operations against the provider.
4. CIM-BasedCmdlet\WindowsProcess
------------------------------------------------------------------------------------
This directory contains a set of files that defines CIM-Based cmdlets.
The cmdlets interact with MSFT_WindowsProcess & MSFT_WindowsServiceProcess
implemented by the process provider.
- WindowsProcess.cdxml defines CIM-based cmdlets for MSFT_WindowsProcess class
- WindowsProcess.format.ps1xml defines how to display MSFT_WindowsProcess instances
- WindowsProcess.types.ps1xml defines extended PowerShell properties for MSFT_WindowsProcess class
- WindowsProcess.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 process 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\WindowsProcess
- Open a powershell.exe console and invoke the following commands:
Import-Module C:\temp\CIM-BasedCmdlet\WindowsProcess
Get-WindowsProcess
5. CIMCmdlet
------------------------------------------------------------------------------------
This directory contains a ps1 script file, which demonstrate how to use CimCmdlets
to consume the data from the process provider.
To run the script, just open powershell.exe, and paste content of the PS1 file
to PowerShell console.