Skip to content

SystemParametersInfo Example Usage

Djent- edited this page Jan 22, 2016 · 4 revisions

SystemParametersInfo

SystemParametersInfo is located in user32.go and has many uses.

Arguments

SystemParametersInfo takes four arguments:

uiAction uint32
uiParam  uint32
pvParam  unsafe.Pointer
fWinIni  uint32

uiAction dictates what action SystemParametersInfo will take. Some uiActions use the uiParam argument. If the specific uiAction you are using does not use uiParam, pass uiParam as 0. The fWinIni argument is used to send or update the Windows ini file after performing the uiAction.

pvParam

pvParam takes an unsafe.Pointer. Depending on the uiAction, it may either be written to or read from by SystemParametersInfo. For example, SPI_GET... uiActions will write the result to the pointer passed as pvParam, while SPI_SET... uiActions will use the value at the pointer passed as pvParam as input to whatever Windows parameter you are setting.

Example Usage

SPI_GETDESKWALLPAPER

This is a SPI_GET... type uiAction, and as such, it will write to the pointer passed in as pvParam.

wallpaper := "0123456789012345678901234567890123456789012345678901"
wallpaperutf, err := syscall.UTF16PtrFromString(wallpaper)
if err != nil {
	panic(err)
}
success = w32.SystemParametersInfo(w32.SPI_GETDESKWALLPAPER, 55, unsafe.Pointer(wallpaperutf), 0)
fmt.Printf("SPI_GETDESKWALLPAPER success: %t : ", success)
fmt.Printf("%s\n", w32.UTF16PtrToString(wallpaperutf))

In order to properly create a pointer to a string you want to write to, you need to declare and initialize a string variable, and make sure the length is at least equal or longer than the resulting string you expect SystemParametersInfo to mutate. (Your Go program will fault if SystemParametersInfo tries to assign a string larger than the one you initialize!)

In the above code, we:

  • declare and initialize a string variable named wallpaper
  • use the function UTF16PtrFromString from the syscall package to get a pointer to wallpaper
  • check for errors from syscall.UTF16PtrFromString
  • execute w32.SystemParametersInfo passing:
    • w32.SPI_GETDESKWALLPAPER to get the current path to the image being used as the desktop wallpaper
    • 55 is the uiParam argument, which in the case of SPI_GETDESKWALLPAPER is the MAX_LENGTH of the null-terminated string being assigned to unsafe.Pointer(wallpaperutf)
    • unsafe.Pointer(wallpaperutf) is the pointer where we want the wallpaper filename to be set
    • 0 is the fWinIni argument because the Windows ini file does not need to be updated
  • print out whether our test was a success

Note: Every uiAction will use uiParam for different purposes. Read the Microsoft documentation for what you are trying to do.

SPI_SETDESKWALLPAPER

This is a SPI_GET... type uiAction, and as such, it will write to the pointer passed in as pvParam.

filename := "C:\\Users\\Owner\\Desktop\\coolwallpaper.jpg"
filenameutf16, _ := syscall.UTF16PtrFromString(filename)
success = w32.SystemParametersInfo(w32.SPI_SETDESKWALLPAPER, 0, unsafe.Pointer(filenameutf16), w32.SPIF_SENDCHANGE&w32.SPIF_UPDATEINIFILE)
fmt.Printf("SPI_SETDESKWALLPAPER success: %t : ", success)
fmt.Printf("%s\n", w32.UTF16PtrToString(filenameutf16))

In the above code, we:

  • declare and initialize a variable named filename to the string "C:\\Users\\Owner\\Desktop\\coolwallpaper.jpg"
  • use the function UTF16PtrFromString from the syscall package to get a pointer to filename
  • execute w32.SystemParametersInfo passing:
    • w32.SPI_SETDESKWALLPAPER is the uiAction
    • 0 is the uiParam as SPI_SETDESKWALLPAPER does not use uiParam at all
    • unsafe.Pointer(filenameutf16) as the pvParam, a pointer to a properly escaped string
    • w32.SPIF_SENDCHANGE&w32.SPIF_UPDATEINI uses the arithmatic and operator to send the change and update the ini to produce a live result of the desktop wallpaper changing. I've included in the w32 defined constants SPIF_UPDATEANDSENDINI which is a shorthand for this common value.
  • print out whether our test was a success

Note: if SPI_SETDESKWALLAPER results in failure, the current wallpaper will be unset!

Clone this wiki locally
You can’t perform that action at this time.