locked
SystemParametersInfo returns 0 RRS feed

  • Question

  • Hello Dear:

    I use this code to change the desktop wallpaper in C#, but SPI returns 0.

    Here is the simple code:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);

            public const int SPI_SETDESKWALLPAPER = 20;
            public const int SPIF_SENDCHANGE = 0x2;

            int value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "C:\\OwnService\\Sunset.bmp",
                              SPIF_SENDCHANGE);
                
    Please help me to approach the solution.
    Thanks. 
    Tuesday, January 4, 2011 10:42 AM

Answers

  • Thank you Louis and venkataram ana, for guiding me towards the right direction:

    I used both Exception and GetLastError, from both it display the same Error:

     

    This operation requires an interactive window station.

    of Error Code 1459.

    Actually, I created my own windows service in C# and use this chunk of code in it, There was no problem with this code. i just put a check on "Allow service to interact with desktop" in service manager and then it worked fine :).

    Thanks again :)

     

     

     

    • Marked as answer by Mio_Miao Tuesday, January 11, 2011 8:25 AM
    Tuesday, January 4, 2011 1:01 PM

All replies

  • Use the following to identify the problem:

      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern bool SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);
    
      public const int SPI_SETDESKWALLPAPER = 20;
      public const int SPIF_SENDCHANGE = 0x2;
      
      if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "C:\\OwnService\\Sunset.bmp",
               SPIF_SENDCHANGE))
          throw new System.ComponentModel.Win32Exception();
    
    
    Tuesday, January 4, 2011 11:13 AM
  • I would probably this is an interesting question.

     

      class Program
      {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(
          int uAction, int uParam, string lpvParam, int fuWinIni);
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 GetLastError();
    
        public const int SPI_SETDESKWALLPAPER = 20;    
        public const int SPIF_UPDATEINIFILE = 0x01;
        public const int SPIF_SENDWININICHANGE = 0x02;
    
        static void Main(string[] args)
        {
          if ( 0 == SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, @"C:\\blue hills.bmp", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE))
          {
            throw new System.ComponentModel.Win32Exception();
          }
    
               
                
        }

    when i tried to execute the above code it always throwed me an Execption indicating the Operation completed successfully.

          int iRetVal = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, @"C:\\blue hills.bmp" , SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE );
    
          int iError = Program.GetLastError();
    

    Even when i tried the above code , both iRetVal and iError returned to me are Zero.

    iRetVal = 0 meaning The function failed. To get the information , we should call on GetLastError , where as it's return value i.e., iError = 0 meaning , there is no error.

    Strange...!

    Still , from the following link , they are trying to change background of Form , where it is succeeding.

    http://blogs.msdn.com/b/coding4fun/archive/2006/10/31/912569.aspx

    Please have look at the above link.

     

    Thanks,

    Venkataramana M

    Tuesday, January 4, 2011 12:26 PM
  • Thank you Louis and venkataram ana, for guiding me towards the right direction:

    I used both Exception and GetLastError, from both it display the same Error:

     

    This operation requires an interactive window station.

    of Error Code 1459.

    Actually, I created my own windows service in C# and use this chunk of code in it, There was no problem with this code. i just put a check on "Allow service to interact with desktop" in service manager and then it worked fine :).

    Thanks again :)

     

     

     

    • Marked as answer by Mio_Miao Tuesday, January 11, 2011 8:25 AM
    Tuesday, January 4, 2011 1:01 PM
  • iRetVal = 0 meaning The function failed. To get the information , we should call on GetLastError , where as it's return value i.e., iError = 0 meaning , there is no error.

    If you don't set the SetLastError=true in the DllImport attribute, you cannot retrieve the last error.

    Tuesday, January 4, 2011 1:41 PM