I created a dll using C#. Here is the C# code for it:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);
private static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
private static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;
public void SetWallpaper(string path, ref string err)
{
try
{
if (File.Exists(path))
{
SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
err = string.Empty;
}
else
{
throw new Exception("Path '" + path + "' does not exist!");
}
}
catch (Exception ex)
{
err = ex.Message;
}
}
The function above accepts a file path of a wallpaper image as an argument along with a variable passed by reference that will return an error message in case of a failure. An empty string will be returned in case of a success.
No comments:
Post a Comment