The Standard ML Basis Library


The Windows structure


Synopsis

signature WINDOWS  (* OPTIONAL *)
structure Windows :> WINDOWS  (* OPTIONAL *)

The Windows structure provides a high-level interface to various system features based on the Microsoft Microsoft Windows operating system model. These functions include the ability to create and communicate with separate processes, as well as to interact with the registry and file subsystems. In particular, using this module, a program can invoke a separate process and obtain input and output streams connected to the standard output and input streams, respectively, of the other process. The functions provide a richer and more detailed interface than the comparable functions provided by the substructures in OS.


Interface

structure Key : sig
    include BIT_FLAGS
    
    val allAccess : flags
    val createLink : flags
    val createSubKey : flags
    val enumerateSubKeys : flags
    val execute : flags
    val notify : flags
    val queryValue : flags
    val read : flags
    val setValue : flags
    val write : flags
  end

structure Reg : sig
    eqtype hkey
    
    val classesRoot     : hkey
    val currentUser     : hkey
    val localMachine    : hkey
    val users           : hkey
    val performanceData : hkey
    val currentConfig   : hkey
    val dynData         : hkey
    
    datatype create_result
      = CREATED_NEW_KEY of hkey
      | OPENED_EXISTING_KEY of hkey
    val createKeyEx : hkey * string * Key.flags
                        -> create_result
    val openKeyEx : hkey * string * Key.flags -> hkey
    val closeKey : hkey -> unit
    val deleteKey : hkey * string -> unit
    val deleteValue : hkey * string -> unit
    val enumKeyEx : hkey * int -> string option
    val enumValueEx : hkey * int -> string option
    
    datatype value
      = SZ of string
      | DWORD of SysWord.word
      | BINARY of Word8Vector.vector
      | MULTI_SZ of string list
      | EXPAND_SZ of string
    val queryValueEx : hkey * string -> value option
    val setValueEx : hkey * string * value -> unit
  end

structure Config : sig
    val platformWin32s       : SysWord.word
    val platformWin32Windows : SysWord.word
    val platformWin32NT      : SysWord.word
    val platformWin32CE      : SysWord.word
    val getVersionEx : unit
                         -> {
                           majorVersion : SysWord.word,
                           minorVersion : SysWord.word,
                           buildNumber : SysWord.word,
                           platformId : SysWord.word,
                           csdVersion : string
                         }
    val getWindowsDirectory : unit -> string
    val getSystemDirectory : unit -> string
    val getComputerName : unit -> string
    val getUserName : unit -> string
  end

structure DDE : sig
    type info
    val startDialog : string * string -> info
    val executeString : info * string * int * Time.time
                          -> unit
    val stopDialog : info -> unit
  end

val getVolumeInformation : string
                             -> {
                               volumeName : string,
                               systemName : string,
                               serialNumber : SysWord.word,
                               maximumComponentLength : int
                             }

val findExecutable : string -> string option
val launchApplication : string * string -> unit
val openDocument : string -> unit
val simpleExecute : string * string -> OS.Process.status

type ('a,'bproc

val execute : string * string -> ('a'b) proc
val textInstreamOf : (TextIO.instream, 'a) proc
                       -> TextIO.instream
val binInstreamOf  : (BinIO.instream, 'a) proc
                       -> BinIO.instream
val textOutstreamOf : ('a, TextIO.outstream) proc
                        -> TextIO.outstream
val binOutstreamOf  : ('a, BinIO.outstream) proc
                        -> BinIO.outstream
val reap : ('a'b) proc -> OS.Process.status

structure Status : sig
    type status = SysWord.word
    val accessViolation     : status
    val arrayBoundsExceeded : status
    val breakpoint          : status
    val controlCExit        : status
    val datatypeMisalignment : status
    val floatDenormalOperand  : status
    val floatDivideByZero     : status
    val floatInexactResult    : status
    val floatInvalidOperation : status
    val floatOverflow         : status
    val floatStackCheck       : status
    val floatUnderflow        : status
    val guardPageViolation : status
    val integerDivideByZero : status
    val integerOverflow     : status
    val illegalInstruction : status
    val invalidDisposition : status
    val invalidHandle      : status
    val inPageError             : status
    val noncontinuableException : status
    val pending                 : status
    val privilegedInstruction   : status
    val singleStep              : status
    val stackOverflow           : status
    val timeout                 : status
    val userAPC                 : status
  end

val fromStatus : OS.Process.status -> Status.status
val exit : Status.status -> 'a

Description

structure Key
The Key substructure contains flags for specifying security settings when opening and creating keys in the registry.

val allAccess : flags
The union of the queryValue, enumerateSubKeys, notify, createSubKey, createLink, and setValue flags.

val createLink : flags
Permission to create a symbolic link. This value is included for completeness, as the rest of the structure does not support links.

val createSubKey : flags
Permission to create subkeys.

val enumerateSubKeys : flags
Permission to enumerate subkeys.

val execute : flags
Permission for read access.

val notify : flags
Permission for change notification. This value is included for completeness, as the rest of the structure does not support notification.

val queryValue : flags
Permission to query subkey data.

val read : flags
The union of the queryValue, enumerateSubKeys, and notify flags.

val setValue : flags
Permission to set subkey data.

val write : flags
The union of the setValue and createSubKey flags.

structure Reg
This substructure provides Microsoft Windows registry functions.

eqtype hkey
Type of registry key values.

val classesRoot : hkey
val currentUser : hkey
val localMachine : hkey
val users : hkey
val performanceData : hkey
val currentConfig : hkey
val dynData : hkey
These are identifiers for top-level registry keys.

createKeyEx (hkey, skey, regsam)
opens or creates a subkey of hkey, with the name skey and security access specified by regsam.
Implementation note:

This passes REG_OPTION_NON_VOLATILE option, NULL Class, and SECURITY_ATTRIBUTE arguments to the Win32 call.



openKeyEx (hkey, skey, regsam)
opens a subkey of hkey with the name skey and security access specified by regsam.

closeKey hkey
closes the key hkey.

deleteKey (hkey, skey)
deletes the subkey skey of hkey.

deleteValue (hkey, valname)
deletes the value valname of hkey.

enumKeyEx (hkey, ind)
returns the subkey of index ind of the key hkey, where indices start from zero. The function returns SOME of a string for each defined subkey. To enumerate all the subkeys, start with the index at zero and increment it until the function returns NONE. The function raises the Subscript exception if ind is invalid.

enumValueEx (hkey, ind)
returns the value of index ind of the key hkey, where indices start from zero. The function returns SOME of a string for each defined value. To enumerate all the values, start with the index at zero and increment it until the function returns NONE. The function raises the Subscript exception if ind is invalid.

datatype value
  = SZ of string
  | DWORD of SysWord.word
  | BINARY of Word8Vector.vector
  | MULTI_SZ of string list
  | EXPAND_SZ of string
This type describes the kind of values that can be saved to the registry or extracted from it. The constructor SZ corresponds to strings, DWORD to 32-bit numbers, BINARY to arbitrary binary values, MULTI_SZ to lists of strings, and EXPAND_SZ to strings containing environment variables.

queryValueEx (hkey, name)
returns the data associated with name in the open registry key hkey. A value whose type does not correspond to a more specific instance of the value datatype is returned as a BINARY value. If the value does not exist in the key, the function returns NONE. Any other error, such as having insufficient access rights to the registry key, results in the OS.SysErr exception being raised.

A common use of a registry value is to override the default behavior of a program. The normal case is when the registry value is unset. Using an option type allows for the result of queryValueEx to indicate the presence or absence of the key.

setValueEx (hkey, name, v)
associates the value v with name in the open key hkey.

structure Config
This substructure contains functions to obtain information about the operating system.

val platformWin32s : SysWord.word
val platformWin32Windows : SysWord.word
val platformWin32NT : SysWord.word
val platformWin32CE : SysWord.word
These are values corresponding to the indicated Microsoft Windows platforms.

val getVersionEx : unit
                     -> {
                       majorVersion : SysWord.word,
                       minorVersion : SysWord.word,
                       buildNumber : SysWord.word,
                       platformId : SysWord.word,
                       csdVersion : string
                     }
This returns the major and minor versions of the operating system, the build number, platform identifier, and a supplementary version string. The platform identifier platformId can be compared with values platformWin32s, platformWin32Windows, platformWin32NT, and platformWin32CE to determine the type of platform. Note that additional values for other platforms may be returned.

The major and minor version numbers allow additional distinctions. In the case where platformId is platformWin32Windows, we have:


minorVersion System
0 Windows 95
> 0 Windows 98

In the case where platformId is platformWin32NT, we have:


majorVersion minorVersion System
4 0 Windows NT
5 0 Windows 2000
5 > 0 Windows XP



val getWindowsDirectory : unit -> string
The Windows directory, typically "C:\Windows" on Windows 95 or "C:\Winnt" on Windows NT.

val getSystemDirectory : unit -> string
The Windows system directory, typically "C:\Windows\System" or "C:\Winnt\System32".

val getComputerName : unit -> string
The name of the computer.

val getUserName : unit -> string
The name of the current user.

structure DDE
This substructure provides a high-level, client-side interface for simple dynamic data exchange (DDE) interactions. All transactions are synchronous. Advise loops and poke transactions are not supported by this interface.

startDialog (service, topic)
initiates DDE and connects to the given service and topic. It returns the info value created by these operations.

executeString (info, cmd, retry, delay)
attempts to execute the command cmd on the service and topic specified by the info value. The retry argument specifies the number of times to attempt the transaction if the server is busy, pausing for delay between each attempt.

stopDialog info
disconnects the service and topic specified by the info argument and frees the associated resources.

getVolumeInformation root
returns information about the filesystem and volume specified by the root pathname root. The volumeName field contains the name of the volume; the systemName field contains its type (e.g., "FAT" or "NTFS"); the serialNumber field contains the serial number; and the maximumComponentLength field specifies the maximum length of any component of a pathname on this system.

findExecutable name
returns the full executable name associated with name, or NONE if no such file exists.

launchApplication (file, arg)
runs the specified executable file passing it the argument arg. It raises OS.SysErr if file is not executable or if it cannot be run.
Implementation note:

This should be implemented using ShellExecute, passing SW_SHOWNORMAL to the underlying API call.



openDocument file
opens file using its associated application.
Implementation note:

This should pass SW_SHOWNORMAL to the underlying ShellExecute API call.



simpleExecute (cmd, arg)
spawns the process specified by cmd with command-line arguments represented by the string arg, redirecting standard input and standard output to the null device. It then waits for the subprocess to terminate, and returns its exit status. This is similar to OS.Process.system but it can be used in cases where the latter does not work, and its return value provides more information about the exit status of the child process.
Implementation note:

This corresponds to the use of CreateProcess.



type ('a,'b) proc
The type of a process created by execute. The type parameters are witness types for the types of streams that can be returned.

execute (cmd, arg)
spawns a process specified by cmd with command-line arguments represented by the string arg and returns a handle for the resulting process.
Implementation note:

This also corresponds to the use of CreateProcess. Redirection of the standard streams can be handled using the hStdInput and hStdOutput fields in the STARTUPINFO parameter.



textInstreamOf pr
binInstreamOf pr
These functions return a text or binary instream connected to the standard output stream of the process pr.

Note that multiple calls to these functions on the same proc value will result in multiple streams that all share the same underlying open file descriptor, which can lead to unpredictable effects because

textOutstreamOf pr
binOutstreamOf pr
These functions return a text or binary outstream connected to the standard input stream of the process pr.

Note that multiple calls to these functions on the same proc value will result in multiple streams that all share the same underlying open file descriptor, which can lead to unpredictable effects due to buffering.

reap pr
closes the standard streams associated with pr, and then suspends the current process until the system process corresponding to pr terminates. It returns the exit status given by pr when it terminated. If reap is applied again to pr, it should immediately return the previous exit status.
Implementation note:

Typically, one cannot rely on the underlying operating system to provide the exit status of a terminated process after it has done so once. Thus, the exit status probably needs to be cached. Also note that reap should not return until the process being monitored has terminated. In particular, implementations should be careful not to return if the process has only been suspended.



structure Status
The Status substructure defines the possible system-specific interpretations of OS.Process.status values.

fromStatus s
decodes the abstract exit status s into system-specific information.

exit st
executes all actions registered with OS.Process.atExit, flushes and closes all I/O streams, then terminates the SML process with termination status st.

See Also

BIT_FLAGS, OS.FileSys, OS.Process, TextIO, Time

Discussion

This structure provides a minimal view of the system calls available across Microsoft operating systems. It focuses on managing the registry, and executing programs. The function Windows.findExecutable and the facilities in the Config substructure allow the programmer to determine if and where a program can be found on a given machine.

Future extensions of the Basis Library might give access to more features, either by including additional substructures or as a separate top-level module.

Rationale:

As usual, platform identification and exit status values are not handled by datatypes to allow for future extensions.


[ Top | Parent | Contents | Index | Root ]

Generated October 02, 2003
Last Modified July 1, 2002
Comments to John Reppy.


This document may be distributed freely over the internet as long as the copyright notice and license terms below are prominently displayed within every machine-readable copy.

Copyright © 2003 AT&T and Lucent Technologies. All rights reserved.

Permission is granted for internet users to make one paper copy for their own personal use. Further hardcopy reproduction is strictly prohibited. Permission to distribute the HTML document electronically on any medium other than the internet must be requested from the copyright holders by contacting the editors. Printed versions of the SML Basis Manual are available from Cambridge University Press. To order, please visit www.cup.org (North America) or www.cup.cam.ac.uk (outside North America).