private void bnSendMessage_Click(object sender, EventArgs e) { int nNumber; // Validate the parameter value in tbNumber if (!int.TryParse(this.tbNumber.Text, out nNumber)) { MessageBox.Show("Invalid value of nNumber!"); return; } ///////////////////////////////////////////////////////////////// // Find the target Window Handle. // IntPtr hTargetWnd = WindowNative.FindWindow(null, "CSReceiveWM_COPYDATA"); // Validate the Window Handle. if (hTargetWnd == IntPtr.Zero) { MessageBox.Show("Unable to find the target Window!"); return; } ///////////////////////////////////////////////////////////////// // Prepare the COPYDATASTRUCT struct with the data to be sent. // // Declare the MyStruct struct to hold the message MyStruct myStruct; myStruct.nNumber = nNumber; myStruct.strMessage = this.tbMessage.Text; // Marshals from the managed object to a native block of memory IntPtr ptrMyStruct = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct)); Marshal.StructureToPtr(myStruct, ptrMyStruct, true); // Declare the COPYDATASTRUCT struct for the WM_COPYDATA message. COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.cbData = Marshal.SizeOf(myStruct); cds.lpData = ptrMyStruct; ///////////////////////////////////////////////////////////////// // Send the COPYDATASTRUCT struct through the WM_COPYDATA message // to the receiving Window. // // Send the WM_COPYDATA message // The application must use SendMessage, instead of PostMessage // to send WM_COPYDATA because the receiving application must // accept while it is guaranteed to be valid. WindowNative.SendMessage( hTargetWnd, // Handle of the target Window WindowNative.WM_COPYDATA, // WM_COPYDATE message this.Handle, // Handle of the current Window ref cds // COPYDATASTRUCT structure ); // Check error of SendMessage int result = Marshal.GetLastWin32Error(); if (result != 0) { MessageBox.Show(String.Format( "SendMessage failed w/err 0x{0:X}", result)); } ///////////////////////////////////////////////////////////////// // Clean up. // // Frees the native memory allocated for MyStruct Marshal.FreeHGlobal(ptrMyStruct); }
public void Update() { WindowNative.SendMessage(Handle, 15U, (IntPtr)0, (IntPtr)0); }