public void SimpleMessageWindowInput(System.IntPtr hWnd, int msg, System.IntPtr wParam, System.IntPtr lParam)
    {
        RawInput ri        = new RawInput();
        int      blen      = 0;
        int      hlen      = System.Runtime.InteropServices.Marshal.SizeOf(typeof(RAWINPUTHEADER));
        int      bytesRead = -1;

        bytesRead = GetRawInputData(lParam, RawInputCommand.Input, System.IntPtr.Zero, ref blen, hlen);
        if (((bytesRead == -1) || (blen < 1)))
        {
            System.Diagnostics.Debug.WriteLine("GetRawInputData Error Retreiving Buffer size.");
        }
        else
        {
            System.IntPtr riBuffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(blen);
            bytesRead = GetRawInputData(lParam, RawInputCommand.Input, riBuffer, ref blen, hlen);
            if (bytesRead != blen)
            {
                System.Diagnostics.Debug.WriteLine("GetRawInputData Error Retreiving Buffer data.");
            }
            else
            {
                byte[] bb = new byte[bytesRead];
                try
                {
                    ri = (RawInput)System.Runtime.InteropServices.Marshal.PtrToStructure(riBuffer, typeof(RawInput));
                    System.Runtime.InteropServices.Marshal.Copy(riBuffer, bb, 0, bb.Length);
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
                if (ri.Equals(null))
                {
                    System.Diagnostics.Debug.WriteLine("Error Casting Marshalled Buffer into RawInput structure.");
                }
                else
                {
                    //string deviceType = null;
                    switch (ri.Header.dwType)
                    {
                    case 0:
                        //deviceType = "MOUSE";
                        if ((((OnRawInputFromMouse != null)) && (OnRawInputFromMouse.GetInvocationList().Length > 0)))
                        {
                            RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnRawInputFromMouse, new object[] {
                                ri.Header,
                                ri.Data.Mouse
                            });
                        }
                        break;

                    case 1:
                        //deviceType = "KEYBOARD";
                        if ((((OnRawInputFromKeyboard != null)) && (OnRawInputFromKeyboard.GetInvocationList().Length > 0)))
                        {
                            RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnRawInputFromKeyboard, new object[] {
                                ri.Header,
                                ri.Data.Keyboard
                            });
                        }
                        break;

                    case 2:
                        //deviceType = "HID";
                        if ((((OnRawInputFromHID != null)) && (OnRawInputFromHID.GetInvocationList().Length > 0)))
                        {
                            RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnRawInputFromHID, new object[] {
                                ri.Header,
                                ri.Data.HID
                            });
                        }
                        break;

                    default:
                        //deviceType = "UNKNOWN";
                        if ((((OnRawInputFromUnknown != null)) && (OnRawInputFromUnknown.GetInvocationList().Length > 0)))
                        {
                            RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnRawInputFromUnknown, new object[] { ri });
                        }
                        break;
                    }
                }
            }
            try
            {
                System.Runtime.InteropServices.Marshal.FreeHGlobal(riBuffer);
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error Freeing Marshalled Buffer for RawInput data.");
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
    }
 private void m_BackgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
 {
     if (((e.UserState != null)))
     {
         int iRet = e.ProgressPercentage;
         if ((object.ReferenceEquals(e.UserState.GetType(), typeof(MSG))))
         {
             m_messageCount += 1;
             MSG msg = (MSG)e.UserState;
             if ((((OnNativeGetMessage != null)) && (OnNativeGetMessage.GetInvocationList().Length > 0)))
             {
                 RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnNativeGetMessage, new object[] {
                     this,
                     m_messageCount,
                     iRet,
                     msg
                 });
             }
         }
         else if ((object.ReferenceEquals(e.UserState.GetType(), typeof(OnWindowMessageParams))))
         {
             OnWindowMessageParams msgParams = (OnWindowMessageParams)e.UserState;
             if (iRet == 31)
             {
                 if ((((OnWindowsMessageCreate != null)) && (OnWindowsMessageCreate.GetInvocationList().Length > 0)))
                 {
                     RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnWindowsMessageCreate, new object[] {
                         msgParams.hWnd,
                         msgParams.msg,
                         msgParams.wParam,
                         msgParams.lParam
                     });
                 }
             }
             else if (iRet == 32)
             {
                 if ((((OnWindowsMessageInput != null)) && (OnWindowsMessageInput.GetInvocationList().Length > 0)))
                 {
                     RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnWindowsMessageInput, new object[] {
                         msgParams.hWnd,
                         msgParams.msg,
                         msgParams.wParam,
                         msgParams.lParam
                     });
                 }
             }
             else if (iRet == 33)
             {
                 if ((((OnWindowsMessageClose != null)) && (OnWindowsMessageClose.GetInvocationList().Length > 0)))
                 {
                     RaiseEventUtility.RaiseEventAndExecuteItInTheTargetThread(OnWindowsMessageClose, new object[] {
                         msgParams.hWnd,
                         msgParams.msg,
                         msgParams.wParam,
                         msgParams.lParam
                     });
                 }
             }
         }
     }
 }