locked
StopPowerNotifications fails with error code 87 (The parameter is incorrect) RRS feed

  • Question

  • Hello,

      I have got the following code (in C#) on Windows CE 6.0 (with compact framework 3.5):

        [Flags]
        public enum PowerEventType {
          PBT_TRANSITION = 0x00000001,
          PBT_RESUME = 0x00000002,
          PBT_POWERSTATUSCHANGE = 0x00000004,
          PBT_POWERINFOCHANGE = 0x00000008,
        }

        [Flags]
        public enum PowerState {
          POWER_STATE_ON = (0x00010000),
          POWER_STATE_OFF = (0x00020000),
          POWER_STATE_CRITICAL = (0x00040000),
          POWER_STATE_BOOT = (0x00080000),
          POWER_STATE_IDLE = (0x00100000),
          POWER_STATE_SUSPEND = (0x00200000),
          POWER_STATE_RESET = (0x00800000),
        }

        public class MSGQUEUEOPTIONS {
          public MSGQUEUEOPTIONS()
          {
            dwSize = Marshal.SizeOf(typeof(MSGQUEUEOPTIONS));
            dwFlags = 0;
            dwMaxMessages = 20;
            cbMaxMessage = 1000;
            bReadAccess = 1;
          }
          public int dwSize;
          public int dwFlags;
          public int dwMaxMessages;
          public int cbMaxMessage;
          public int bReadAccess;
        }

        public class POWER_BROADCAST {
          public POWER_BROADCAST(int size)
          {
            m_data = new byte[size];
          }
          byte[] m_data; 
          public byte[] Data { get { return m_data; } }
          public PowerEventType Message { get { return (PowerEventType)BitConverter.ToInt32(m_data, 0); } }
          public PowerState Flags { get { return (PowerState)BitConverter.ToInt32(m_data, 4); } }
          public int Length { get { return BitConverter.ToInt32(m_data, 8); } }
          public byte[] SystemPowerState { get { byte[] data = new byte[Length]; Buffer.BlockCopy(m_data, 12, data, 0, Length); return data; } }
        }

        [DllImport("coredll.dll")]
        public static extern IntPtr CreateMsgQueue(string Name, MSGQUEUEOPTIONS Options);

        [DllImport("coredll.dll")]
        public static extern bool ReadMsgQueue(IntPtr hMsgQ, byte[] lpBuffer, int cbBufferSize, out int lpNumberOfBytesRead, int dwTimeout, out int pdwFlags);

        [DllImport("coredll.dll")]
        public static extern uint RequestPowerNotifications(IntPtr hMsgQ, PowerEventType Flags);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern bool StopPowerNotifications(IntPtr hNotif);

        [DllImport("coredll.dll")]
        public static extern bool CloseMsgQueue(IntPtr hMsgQ);

          [MTAThread]
        static int Main()
        {
            ...

          powerQueue = CreateMsgQueue("MyPowerEventQueue", new MSGQUEUEOPTIONS());
          powerNotif = RequestPowerNotifications(powerQueue, PowerEventType.PBT_TRANSITION);
       

          // There are no function called !

             
          bool bRet;
          bRet = CloseMsgQueue(powerQueue);     // bRet is true
          bRet = StopPowerNotifications(powerNotif);  //bRet is false
          int errorCode = Marshal.GetLastWin32Error();                     // errorCode is 87 - The parameter is incorrect

          ....

          }

         The last call function StopPowerNotifications fails with the error code 87 with the meaning "The parameter is incorrect". Please, why???



    • Edited by Jiri M Monday, January 14, 2013 8:24 AM
    Monday, January 14, 2013 8:22 AM

Answers

  • Looking at the source, I'd say the documented return value for StopPowerNotifications is wrong. It should return a DWORD. Success is ERROR_SUCCESS (0). Other possible values are ERROR_INVALID_PARAMETER and ERROR_FILE_NOT_FOUND.

    So, a 0 return value is a success.

    -PaulH

    • Marked as answer by Jiri M Monday, January 21, 2013 3:04 PM
    Monday, January 21, 2013 2:58 PM

All replies

  • Thanks for answer,  I changed code to

          [MTAThread]
        static int Main()
        {
            ...

          powerQueue = CreateMsgQueue("MyPowerEventQueue", new MSGQUEUEOPTIONS());
          powerNotif = RequestPowerNotifications(powerQueue, PowerEventType.PBT_TRANSITION);

          // There are no function called !

             
          bool bRet;
          bRet = StopPowerNotifications(powerNotif);  //bRet is still false
          int errorCode = Marshal.GetLastWin32Error();                     // errorCode is 87 - The parameter is incorrect

          bRet = CloseMsgQueue(powerQueue);     // bRet is true
          ....

          }

    But the result is the same !!

    Tuesday, January 15, 2013 9:16 AM
  • What did CreateMsgQueue() and RequestPowerNotifications() return?
    Thursday, January 17, 2013 7:21 PM
  • Both functions (CreateMsgQueue() and RequestPowerNotifications()) returns valid nonzero values.

    • Edited by Jiri M Friday, January 18, 2013 9:03 AM
    Friday, January 18, 2013 9:02 AM
  • Looking at the source, I'd say the documented return value for StopPowerNotifications is wrong. It should return a DWORD. Success is ERROR_SUCCESS (0). Other possible values are ERROR_INVALID_PARAMETER and ERROR_FILE_NOT_FOUND.

    So, a 0 return value is a success.

    -PaulH

    • Marked as answer by Jiri M Monday, January 21, 2013 3:04 PM
    Monday, January 21, 2013 2:58 PM
  • Ok, thank you. But one end question - why was GetLastError equal 87?

    • Edited by Jiri M Monday, January 21, 2013 3:09 PM
    Monday, January 21, 2013 3:04 PM