Giter Club home page Giter Club logo

cwmi's Introduction

cWMI

This project is a lightweight wrapper for interacting with WMI using python/ctypes without any external dependencies. It allows a lower level of interaction with WMI, but requires a greater knowledge of WMI internals. There are several helpers included that will come in handy for those who don't need to do anything low level, or just want a quick start.

This project was not intended to be a replacement for the existing (and very good) wmi module, but as a pure python alternative without additional dependencies.

Usage

To use this module import cwmi and either use the helper functions, or the WMI APIs directly.

Easy way, using helper functions.

import cwmi


def list_processes():
    processes = cwmi.query('root\\cimv2', 'SELECT * FROM Win32_Process')
    print('NAME\t\tPROCESS ID')
    print('=' * 80)
    for process, values in processes.items():
        print('{:s}\t\t{:d}'.format(values['Name'], values['ProcessId']))

Not as easy way, directly interacting with WMI APIs.

import cwmi

def list_processes():
    with cwmi.WMI('root\\cimv2') as svc:
        with svc.ExecQuery('WQL', 'SELECT * FROM Win32_Process', 0, None) as enum:

            print('NAME\t\tPROCESS ID')
            while True:
                try:
                    with enum.Next(cwmi.WBEM_INFINITE) as obj:
                        obj.BeginEnumeration(0)
                        proc_data = {}
                        while True:
                            try:
                                prop_name, var, _, _ = obj.Next(0)
                                proc_data[prop_name] = cwmi.V_TO_TYPE(var)
                            except WindowsError:
                                break

                        obj.EndEnumeration()

                        print('{:s}\t\t{:d}'.format(proc_data['Name'], 
                                                    proc_data['ProcessId']))

                except WindowsError:
                    break

For more examples see examples.

Microsoft's WMI documentation will be helpful if directly interacting with the APIs.

Some things to note are that while the documentation specifies input pointer parameters to some methods, this will not be necessary when using the wrapper APIs. The wrappers also handle python str to BSTR conversion and resource management.

cwmi's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.