WinCE通信

1 using System;
2
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace Microwind.SmartRfid.MockProvider
8 {
9 publicclass WinCEApi
10 {
11 publicconstint LMEM_FIXED =0x0000;
12 publicconstint LMEM_ZEROINIT =0x0040;
13 publicconstint LPTR = (LMEM_FIXED | LMEM_ZEROINIT);
14
15 [DllImport(@"\windows\coredll.dll")]
16 publicstaticextern IntPtr FindWindow(string ClassName, string WindowName);
17
18 [DllImport(@"\windows\coredll.dll")]
19 publicstaticextern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
20
21 [DllImport("coredll.dll")]
22 publicstaticextern IntPtr LocalAlloc(int flag, int size);
23
24 [DllImport("coredll.dll")]
25 publicstaticextern IntPtr LocalFree(IntPtr p);
26
27 publicstatic IntPtr AllocHGlobal(int cb)
28 {
29 IntPtr hMemory =new IntPtr();
30 hMemory = LocalAlloc(LPTR, cb);
31 return hMemory;
32 }
33
34 publicclass MessageConsts
35 {
36 publicconstint WM_COPYDATA =0x004A;
37 privateconstint USER =0x0400;
38 }
39
40 publicstruct COPYDATASTRUCT
41 {
42 public IntPtr dwData;
43 publicint cbData;
44 public IntPtr lpData;
45 }
46 }
47 }
1 using System;
2
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Runtime.InteropServices;
10
11 namespace Microwind.SmartRfid.MockProvider
12 {
13 publicpartialclass MessageHandlerForm : Microsoft.WindowsCE.Forms.MessageWindow
14 {
15 publicconststring WindowTitle ="B5F29B02-8362-4dd5-8153-8AD8803C98B4";
16 private List<string> _tagList =new List<string>();
17 public List<string> TagList
18 {
19 get
20 {
21 return _tagList;
22 }
23 }
24 privatevoid ScanTags(string tags)
25 {
26 if (string.IsNullOrEmpty(tags)) return;
27 string[] tagArr = tags.Split(',');
28 _tagList.AddRange(tagArr);
29 }
30 protectedoverridevoid WndProc(ref Microsoft.WindowsCE.Forms.Message m)
31 {
32 switch (m.Msg)
33 {
34 case WinCEApi.MessageConsts.WM_COPYDATA:
35 WinCEApi.COPYDATASTRUCT copyDataStruct = (WinCEApi.COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(WinCEApi.COPYDATASTRUCT));
36 string str = Marshal.PtrToStringUni(copyDataStruct.lpData);
37 ScanTags(str);
38 break;
39 default:
40 base.WndProc(ref m);
41 break;
42 }
43 }
44 public MessageHandlerForm()
45 {
46 Text = WindowTitle;
47 }
48 }
49 }
1 using System;
2
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Runtime.InteropServices;
10
11 namespace Microwind.SmartRfid.MockProvider
12 {
13 publicpartialclass MainForm : Form
14 {
15 public MainForm()
16 {
17 InitializeComponent();
18 }
19
20 privatevoid btnSend_Click(object sender, EventArgs e)
21 {
22 IntPtr hWin = WinCEApi.FindWindow(null, MessageHandlerForm.WindowTitle);
23 if (hWin != IntPtr.Zero)
24 {
25 string tags = textBox1.Text;
26 if (tags.Length ==0) return;
27
28 WinCEApi.COPYDATASTRUCT copyDataStruct;
29 copyDataStruct.cbData = (tags.Length +1) *2;
30 copyDataStruct.lpData = WinCEApi.LocalAlloc(WinCEApi.LPTR, copyDataStruct.cbData);
31 Marshal.Copy(tags.ToCharArray(), 0, copyDataStruct.lpData, tags.Length);
32 copyDataStruct.dwData = (IntPtr)100;
33 IntPtr lParam = WinCEApi.AllocHGlobal(copyDataStruct.cbData);
34 Marshal.StructureToPtr(copyDataStruct, lParam, false);
35
36 WinCEApi.SendMessage(hWin, WinCEApi.MessageConsts.WM_COPYDATA, IntPtr.Zero, lParam);
37 }
38 }
39
40 privatevoid button1_Click(object sender, EventArgs e)
41 {
42 Close();
43 }
44 }
45 }
posted @ 2011-05-28 15:05  微米大大  阅读(397)  评论(0编辑  收藏  举报