// Called when data has been received from YahCoLoRiZe from the message-loop in FormMsgPump void GlobalNotifier_DataReceived(COLORIZENETSTRUCT cns) { if (cns.data != null && cns.lenData > 0 && cns.clientID == ICECHAT_ID) { string newStr; int len = cns.lenData; if (len > CNS_DATALEN) len = CNS_DATALEN; if (cns.commandData == 1) newStr = Encoding.UTF8.GetString(cns.data, 0, len); else newStr = Encoding.Default.GetString(cns.data, 0, len); SendCommandToIceChat(newStr); } }
private bool SendToYahCoLoRiZe(COLORIZENETSTRUCT cns) { bool RetVal = false; IntPtr cnsMemory = IntPtr.Zero; IntPtr cdsMemory = IntPtr.Zero; int sizecns = Marshal.SizeOf(cns); try { // Allocate memory and move Struct to it cnsMemory = Marshal.AllocHGlobal(sizecns); // Note: On the first call to the StructureToPtr method after a memory block has been // allocated, fDeleteOld must be false, because there are no contents to clear. Marshal.StructureToPtr(cns, cnsMemory, false); // Populate CopyDataStruct COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.cbData = sizecns; cds.lpData = cnsMemory; cds.dwData = (IntPtr)RWM_ColorizeNet; // Allocate memory and move CopyDataStruct to it cdsMemory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(COPYDATASTRUCT))); // Note: On the first call to the StructureToPtr method after a memory block has been // allocated, fDeleteOld must be false, because there are no contents to clear. Marshal.StructureToPtr(cds, cdsMemory, false); // Find the YahCoLoRiZe main window (if app is running)... IntPtr WHnd = NativeMethods.FindWindow(YAHCOLORIZE_CLASSNAME, YAHCOLORIZE_WINDOWNAME); if (WHnd.Equals(System.IntPtr.Zero)) // Try the class-name alone... WHnd = NativeMethods.FindWindow(YAHCOLORIZE_CLASSNAME, null); if (WHnd.Equals(System.IntPtr.Zero)) // Try the window-name alone... WHnd = NativeMethods.FindWindow(null, YAHCOLORIZE_WINDOWNAME); // Send the message to target if (!WHnd.Equals(System.IntPtr.Zero)) { const uint TIMEOUT_INTERVAL = 5000; // 5 seconds //fuFlags: const uint SMTO_NORMAL = 0x0000; //Abort after specified timeout //const uint SMTO_BLOCK = 0x0001; //Prevents the calling thread from processing any other requests until the function returns. //const uint SMTO_ABORTIFHUNG = 0x0002; //The function returns without waiting for the time-out period to elapse if the receiving thread appears to not respond or "hangs." //const uint SMTO_NOTIMEOUTIFNOTHUNG = 0x0008; //The function does not enforce the time-out period as long as the receiving thread is processing messages. //const uint SMTO_ERRORONEXIT = 0x0020; //The function should return 0 if the receiving window is destroyed or its owning thread dies while the message is being processed. // Send user's input text to YahCoLoRiZe via WM_COPYDATA and pass it our // "invisible" window-form's handle to reply back to... IntPtr ret = IntPtr.Zero; NativeMethods.SendMessageTimeout(WHnd, WM_COPYDATA, msgPump.Handle, cdsMemory, SMTO_NORMAL, TIMEOUT_INTERVAL, ref ret); } RetVal = true; } finally { // Free allocated memory if (cnsMemory != IntPtr.Zero) { try { Marshal.FreeHGlobal(cnsMemory); } catch { } } if (cdsMemory != IntPtr.Zero) { try { Marshal.FreeHGlobal(cdsMemory); } catch { } } } return RetVal; }
// Set new channel or send the current version public COLORIZENETSTRUCT PopulateStruct(int commandID, string sData) { COLORIZENETSTRUCT cs = new COLORIZENETSTRUCT(commandID); int len; // Trim the string in a loop until it fits our byte array... while ((len = Encoding.UTF8.GetByteCount(sData)) > CNS_DATALEN-1) sData = sData.Substring(0, sData.Length-1); // sIn, firstIndex, charCount, byteOut[], writeIndex cs.lenData = Encoding.UTF8.GetBytes(sData, 0, sData.Length, cs.data, 0); // Convert C# string to a UTF-8 byte-array cs.data[cs.lenData] = 0; // null utf-8 char return cs; }