Home > front end >  Get current desktop wallpaper in ctypes
Get current desktop wallpaper in ctypes

Time:02-12

I try to get path to current wallpaper in Python using ctypes module. But as a result program returns value 1.

import ctypes

SPI_GETDESKWALLPAPER = 0x0073

path = ctypes.create_unicode_buffer(260)
a = ctypes.windll.user32.SystemParametersInfoW(SPI_GETDESKWALLPAPER, 100, path, 0)
print(a)

CodePudding user response:

The value is returned in path. a in your example is the return code (1=success). It's also a good habit to define the argument types and return type so ctypes doesn't have to guess when converting parameters from Python to C and vice versa.

import ctypes as ct
from ctypes import wintypes as w

SPI_GETDESKWALLPAPER = 0x0073

dll = ct.WinDLL('user32')
dll.SystemParametersInfoW.argtypes = w.UINT,w.UINT,w.LPVOID,w.UINT
dll.SystemParametersInfoW.restype = w.BOOL

path = ct.create_unicode_buffer(260)
result = dll.SystemParametersInfoW(SPI_GETDESKWALLPAPER, ct.sizeof(path), path, 0)
print(result, path.value)

CodePudding user response:

Try this code if you want, maybe it will help you :)

from os import path, getenv, getcwd
from ctypes import windll
import ctypes
from shutil import copyfile
from PIL import Image
from tempfile import NamedTemporaryFile


SPI_GETDESKWALLPAPER = 0x0073

path = ctypes.create_unicode_buffer(260)
returnImgObj = ctypes.windll.user32.SystemParametersInfoW(SPI_GETDESKWALLPAPER, 100, path, 0)

currentWallpaper = getenv(
            'APPDATA')   "\\Microsoft\\Windows\\Themes\\TranscodedWallpaper"   #this is path manuel 

if returnImgObj == True:
        
        a= Image.open(currentWallpaper)
        a.show()
        print(str(currentWallpaper))
        
else:
        tempFile = NamedTemporaryFile(mode="wb", suffix='.jpg').name
        copyfile(currentWallpaper, tempFile)
        a= Image.open(currentWallpaper)
        a.show()
        print(str(currentWallpaper))
    
  • Related