.Net CF에서 WM_COPYDATA 사용하기 Posted by 빵빵빵 2009/07/16 11:01 전산(컴퓨터)/Mobile-CE&PPC 전산(컴퓨터)/Mobile-CE&PPC using Microsoft.WindowsCE.Forms; public partial class MsgForm : Form { [DllImport("coredll.dll", EntryPoint = "RegisterWindowMessage", SetLastError = true)] private static extern uint RegisterWindowMessage(string lpString); private uint msgUid = RegisterWindowMessage("MESSAGE_UID"); public static int MSG_BROADCAST = 0xFFFF; private void SendMessage(object sender) { Message msg = Message.Create((IntPtr)MSG_BROADCAST, (int)msgUid , IntPtr.Zero, IntPtr.Zero); MessageWindow.SendMessage(ref msg); } } } using Microsoft.WindowsCE.Forms; public class MsgWindow : MessageWindow { [DllImport("coredll.dll", EntryPoint = "RegisterWindowMessage", SetLastError = true)] private static extern uint RegisterWindowMessage(string lpString); private uint msgUid = RegisterWindowMessage("MESSAGE_UID"); protected override void WndProc(ref Message msg) { if (msg.Msg == msgUid ) { //handle the message. } } } public partial class MsgForm : Form { private MsgWindow MsgWin; public MsgForm() { //pass the form reference to messagewindow this.MsgWin = new MsgWindow(this); } } public class MsgWindow : MessageWindow { private MsgForm msgForm; public MsgWindow(MsgForm msgForm) { this.msgForm= msgForm; } protected override void WndProc(ref Message msg) { if (msg.Msg == msgUid ) { //call form to handle the message. msgForm.HandleMsg(); } } } public struct COPYDATASTRUCT { public int dwData; public int cbData; public IntPtr lpData; } class cMsgStrings { const int LMEM_FIXED = 0x0000; const int LMEM_ZEROINIT = 0x0040; const int LPTR = (LMEM_FIXED | LMEM_ZEROINIT); const int WM_COPYDATA = 0x004A; [DllImport("coredll.dll")] public static extern IntPtr LocalAlloc(int flag, int size); [DllImport("coredll.dll")] public static extern IntPtr LocalFree(IntPtr p); [DllImport("coredll.dll")] public static extern int SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); public static IntPtr AllocHGlobal(int cb) { IntPtr hMemory = new IntPtr(); hMemory = LocalAlloc(LPTR, cb); return hMemory; } public static void FreeHGlobal(IntPtr hMemory) { if (hMemory != IntPtr.Zero) LocalFree(hMemory); } public static void SendMsgString(IntPtr hWndDest, string sScript) { COPYDATASTRUCT oCDS = new COPYDATASTRUCT(); oCDS.cbData = (sScript.Length + 1) * 2; oCDS.lpData = LocalAlloc(LPTR, oCDS.cbData); Marshal.Copy(sScript.ToCharArray(), 0, oCDS.lpData, sScript.Length); oCDS.dwData = 1; IntPtr lParam = AllocHGlobal(oCDS.cbData); Marshal.StructureToPtr(oCDS, lParam, false); SendMessageW(hWndDest, WM_COPYDATA, IntPtr.Zero, lParam); LocalFree(oCDS.lpData); FreeHGlobal(lParam); } } //send the message with string private void button1_Click(object sender, EventArgs e) { unsafe { cMsgStrings.SendMsgString((IntPtr)MSG_BROADCAST, textBoxMsg.Text); } } public class MsgWindow : MessageWindow { const int WM_COPYDATA = 0x004A; private FormReceiver msgForm; public MsgWindow(FormReceiver msgForm) { this.msgForm = msgForm; } protected override void WndProc(ref Message msg) { if (msg.Msg == WM_COPYDATA) { string str = GetMsgString(msg.LParam); msgForm.HandleMsg(str); } } public static string GetMsgString(IntPtr lParam) { if (lParam != IntPtr.Zero) { COPYDATASTRUCT st = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT)); string str = Marshal.PtrToStringUni(st.lpData); return str; } else { return null; } } } http://www.cnblogs.com/procoder/archive/2009/03/16/1413176.htmlhttp://iwoohaha.tistory.com/90 2009/07/16 11:01 2009/07/16 11:01 Tags .Net CF, Compact Framework, SendMessage, WM_COPYDATA Trackback: 0 Comment: 0 이 글에는 트랙백을 보낼 수 없습니다