/// <summary> /// Send message to Snarl. /// </summary> /// <param name="msg"></param> /// <returns>Return zero on failure.</returns> private Int32 Send(SnarlMessage msg) { Int32 nReturn = 0; // Failure localError = 0; IntPtr hWnd = GetSnarlWindow(); if (!IsWindow(hWnd)) { localError = SnarlStatus.ErrorNotRunning; return(0); } IntPtr nSendMessageResult = IntPtr.Zero; IntPtr ptrToSnarlMessage = IntPtr.Zero; IntPtr ptrToCds = IntPtr.Zero; try { COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.dwData = (IntPtr)0x534E4C02; // "SNL",2 cds.cbData = Marshal.SizeOf(typeof(SnarlMessage)); ptrToSnarlMessage = Marshal.AllocHGlobal(cds.cbData); Marshal.StructureToPtr(msg, ptrToSnarlMessage, false); cds.lpData = ptrToSnarlMessage; ptrToCds = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(COPYDATASTRUCT))); Marshal.StructureToPtr(cds, ptrToCds, false); if (SendMessageTimeout(hWnd, (uint)WindowsMessage.WM_COPYDATA, (IntPtr)GetCurrentProcessId(), ptrToCds, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG | SendMessageTimeoutFlags.SMTO_NOTIMEOUTIFNOTHUNG, 500, out nSendMessageResult) == IntPtr.Zero) { // return zero on failure localError = SnarlStatus.ErrorTimedOut; return(0); } // return result and cache LastError nReturn = unchecked ((Int32)nSendMessageResult.ToInt64()); // Avoid aritmetic overflow error localError = (SnarlStatus)GetProp(hWnd, "last_error"); } finally { Marshal.FreeHGlobal(ptrToCds); Marshal.FreeHGlobal(ptrToSnarlMessage); } return(nReturn); }
/// <summary> /// Returns the version of Snarl running. /// </summary> /// <returns></returns> public Int32 GetVersion() { localError = 0; IntPtr hWnd = GetSnarlWindow(); if (!IsWindow(hWnd)) { localError = SnarlStatus.ErrorNotRunning; return(0); } return(GetProp(hWnd, "_version").ToInt32()); }
public CallbackEventArgs(SnarlStatus snarlEvent, UInt16 parameter, int msgToken) { SnarlEvent = snarlEvent; MessageToken = msgToken; Parameter = parameter; }
/// <summary> /// Send message to Snarl. /// </summary> /// <param name="msg"></param> /// <returns>Return zero on failure.</returns> private Int32 Send(SnarlMessage msg) { Int32 nReturn = 0; // Failure localError = 0; IntPtr hWnd = GetSnarlWindow(); if (!IsWindow(hWnd)) { localError = SnarlStatus.ErrorNotRunning; return 0; } IntPtr nSendMessageResult = IntPtr.Zero; IntPtr ptrToSnarlMessage = IntPtr.Zero; IntPtr ptrToCds = IntPtr.Zero; try { COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.dwData = (IntPtr)0x534E4C02; // "SNL",2 cds.cbData = Marshal.SizeOf(typeof(SnarlMessage)); ptrToSnarlMessage = Marshal.AllocHGlobal(cds.cbData); Marshal.StructureToPtr(msg, ptrToSnarlMessage, false); cds.lpData = ptrToSnarlMessage; ptrToCds = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(COPYDATASTRUCT))); Marshal.StructureToPtr(cds, ptrToCds, false); if (SendMessageTimeout(hWnd, (uint)WindowsMessage.WM_COPYDATA, (IntPtr)GetCurrentProcessId(), ptrToCds, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG | SendMessageTimeoutFlags.SMTO_NOTIMEOUTIFNOTHUNG, 500, out nSendMessageResult) == IntPtr.Zero) { // return zero on failure localError = SnarlStatus.ErrorTimedOut; return 0; } // return result and cache LastError nReturn = unchecked((Int32)nSendMessageResult.ToInt64()); // Avoid aritmetic overflow error localError = (SnarlStatus)GetProp(hWnd, "last_error"); } finally { Marshal.FreeHGlobal(ptrToCds); Marshal.FreeHGlobal(ptrToSnarlMessage); } return nReturn; }
/// <summary> /// Returns the version of Snarl running. /// </summary> /// <returns></returns> public Int32 GetVersion() { localError = 0; IntPtr hWnd = GetSnarlWindow(); if (!IsWindow(hWnd)) { localError = SnarlStatus.ErrorNotRunning; return 0; } return GetProp(hWnd, "_version").ToInt32(); }