com package: some examples

To get you started with using the com package to interact with COM services, here are some examples for how to use it.
More to follow..

WMI demo

The Windows Management Instrumentation (WMI) functionality in Windows OSes exposes a rich model for querying and controlling the OS programmatically. With the aid of some of the basic functions of the com package, you may also access WMI objects from Haskell. Here's an example of how to query for info on the OS of your local machine:

-- Demonstrating some of the functionality provided by the 'com'
-- package for interacting with COM/Automation servers.
module WMIDemo where

-- the WMI module provides a complete API mapping for the WMI object model;
-- (generated by HaskellDirect.)
import WMI
import System.Win32.Com
import System.Win32.Com.Automation as Auto

import Data.Word
import Data.Int
import System.IO.Unsafe ( unsafePerformIO )

wmiDemo :: IO ()
wmiDemo = coRun $ do -- Note: to interact with COM, you need to wrap your code within a 'coRun' application.
    -- create a connection to the WMI service on your local machine
  obj     <- Auto.getObject "winmgmts:\\\\.\\root\\CIMV2"
    -- get the OperatingSystem class (the rest of the args are optional.)
  is      <- obj # instancesOf "Win32_OperatingSystem" (Nothing::Maybe Int) (Nothing::Maybe (Auto.IDispatch ()))
    -- the above method returns an IEnumVARIANT interface, so unpack that sequence into a Haskell list
  (_,ls)  <- is # Auto.enumVariants
  case ls of
    [] -> fail " OS information available; expected at least one."
    (wmi_os:_) -> do
       oi <- wmi_os # buildOSInfo
       putStrLn (recShow oi)

That's not the complete module -- for a complete (working) version of this example, please see the attached WMI .zip file.

Video player


Sigbjorn Finne,
Mar 22, 2009, 11:04 PM