protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == PInvoke.WM_COPYDATA) { PInvoke.COPYDATASTRUCT cps = (PInvoke.COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(PInvoke.COPYDATASTRUCT)); byte[] buffer = new byte[cps.cbData]; Marshal.Copy(cps.lpData, buffer, 0, cps.cbData); var data = Encoding.UTF8.GetString(buffer, 0, buffer[buffer.Length - 1] == 0 ? buffer.Length - 1 : buffer.Length); var root = JObject.Parse(data); if (root.Value <string>("type") == "created-window") { if (long.TryParse(root.Value <string>("window-id"), out var hwnd)) { this.chatterinoHandle = new IntPtr(hwnd); this.updateChatterinoDpi(); Task.Run(async() => { await Task.Delay(100); this.BeginInvoke((MethodInvoker) delegate { this.resizeControls(); }); }); } } } }
private void setChatterinoChannel(string twitchChannelName) { // Currently only support twitch channels. dynamic payload = new Dictionary <string, string> { { "type", "set-channel" }, { "provider", "twitch" }, { "channel-name", twitchChannelName }, }; var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)); var payloadPtr = Marshal.AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, payloadPtr, bytes.Length); var data = new PInvoke.COPYDATASTRUCT { lpData = payloadPtr, cbData = bytes.Length, }; var dataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(data)); Marshal.StructureToPtr(data, dataPtr, false); PInvoke.SendMessage(this.chatterinoHandle, PInvoke.WM_COPYDATA, 0, dataPtr); Marshal.FreeHGlobal(payloadPtr); Marshal.FreeHGlobal(dataPtr); }