在网上找了很多方法,在wm5下都可以用,但在wm6下就有可能不能用了。网上有人说要签名,不知道这个怎么样,但后来发现,是代码上少写了几句话造成的。亮点在44行
现将代码整理如下:
TapiLib.dll的下载地址:https://files.cnblogs.com/xjb/TapiLib.rar
参考资料:http://www.cnblogs.com/jordan2009/archive/2010/10/19/1855260.html
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Collections.Generic;
4 using System.Text;
5 using OpenNETCF.Tapi;
6 namespace SmartDeviceProject4
7 {
8 public struct GeneralInfo
9 {
10 public string Manufacturer;
11 public string Model;
12 public string Revision;
13 public string SerialNumber;
14 public string SubscriberNumber;
15 }
16
17 /// <summary>
18 /// Tapi控制类
19 /// </summary>
20 public class ControlTapi
21 {
22
23 [DllImport("cellcore.dll")]
24 private static extern int lineGetGeneralInfo(IntPtr hLigne, byte[] lpLineGeneralInfo);
25
26 /// <summary>
27 /// 调用cellcore.dll获取sim卡的综合信息
28 /// </summary>
29 /// <param name="l"></param>
30 /// <returns></returns>
31 private GeneralInfo GetGeneralInfo(OpenNETCF.Tapi.Line l)
32 {
33 GeneralInfo lgi = new GeneralInfo();
34 byte[] buffer = new byte[512];
35 BitConverter.GetBytes(512).CopyTo(buffer, 0);
36
37 if (lineGetGeneralInfo(l.hLine, buffer) != 0)
38 {
39 throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "TAPI Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("X"));
40 }
41
42 int subscsize = BitConverter.ToInt32(buffer, 44);
43 int subscoffset = BitConverter.ToInt32(buffer, 48);
44 int scsize = BitConverter.ToInt32(buffer, 36);
45 int scoffset = BitConverter.ToInt32(buffer, 40);
46 lgi.SerialNumber = System.Text.Encoding.Unicode.GetString(buffer, scoffset, scsize).ToString();
47 lgi.SerialNumber = lgi.SerialNumber.Replace("\0", "");
48
49 lgi.SubscriberNumber = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
50 lgi.SubscriberNumber = lgi.SubscriberNumber.Replace("\0", "");
51 return lgi;
52
53 }
54
55
56
57 /// <summary>
58 /// 获取sim卡的IMSI
59 /// </summary>
60 /// <returns></returns>
61 public static string GetIMSINumber()
62 {
63 string result = "";
64 try
65 {
66 Tapi t = new Tapi();
67 t.Initialize();
68 OpenNETCF .Tapi . Line l = t.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
69 ControlTapi ctapi = new ControlTapi();
70 GeneralInfo gi = ctapi.GetGeneralInfo(l);
71
72 result = gi.SubscriberNumber;
73 l.Dispose();
74 t.Shutdown();
75
76 }
77 catch// (Exception ex)
78 {
79 result = "";
80 }
81
82 return result;
83
84 }
85
86 /// <summary>
87 /// 获取IMEI的号码
88 /// </summary>
89 /// <returns></returns>
90 public static string GetIMEINumber()
91 {
92 string result = "";
93 try
94 {
95 Tapi t = new Tapi();
96 t.Initialize();
97 OpenNETCF.Tapi.Line l = t.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
98 ControlTapi ctapi = new ControlTapi();
99 GeneralInfo gi = ctapi.GetGeneralInfo(l);
100 result = gi.SerialNumber;
101 l.Dispose();
102 t.Shutdown();
103
104 }
105 catch(Exception ex)
106 {
107 result = "";
108 }
109
110 return result;
111 }
112
113 }
114
115
116 }
2 using System.Runtime.InteropServices;
3 using System.Collections.Generic;
4 using System.Text;
5 using OpenNETCF.Tapi;
6 namespace SmartDeviceProject4
7 {
8 public struct GeneralInfo
9 {
10 public string Manufacturer;
11 public string Model;
12 public string Revision;
13 public string SerialNumber;
14 public string SubscriberNumber;
15 }
16
17 /// <summary>
18 /// Tapi控制类
19 /// </summary>
20 public class ControlTapi
21 {
22
23 [DllImport("cellcore.dll")]
24 private static extern int lineGetGeneralInfo(IntPtr hLigne, byte[] lpLineGeneralInfo);
25
26 /// <summary>
27 /// 调用cellcore.dll获取sim卡的综合信息
28 /// </summary>
29 /// <param name="l"></param>
30 /// <returns></returns>
31 private GeneralInfo GetGeneralInfo(OpenNETCF.Tapi.Line l)
32 {
33 GeneralInfo lgi = new GeneralInfo();
34 byte[] buffer = new byte[512];
35 BitConverter.GetBytes(512).CopyTo(buffer, 0);
36
37 if (lineGetGeneralInfo(l.hLine, buffer) != 0)
38 {
39 throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "TAPI Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("X"));
40 }
41
42 int subscsize = BitConverter.ToInt32(buffer, 44);
43 int subscoffset = BitConverter.ToInt32(buffer, 48);
44 int scsize = BitConverter.ToInt32(buffer, 36);
45 int scoffset = BitConverter.ToInt32(buffer, 40);
46 lgi.SerialNumber = System.Text.Encoding.Unicode.GetString(buffer, scoffset, scsize).ToString();
47 lgi.SerialNumber = lgi.SerialNumber.Replace("\0", "");
48
49 lgi.SubscriberNumber = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
50 lgi.SubscriberNumber = lgi.SubscriberNumber.Replace("\0", "");
51 return lgi;
52
53 }
54
55
56
57 /// <summary>
58 /// 获取sim卡的IMSI
59 /// </summary>
60 /// <returns></returns>
61 public static string GetIMSINumber()
62 {
63 string result = "";
64 try
65 {
66 Tapi t = new Tapi();
67 t.Initialize();
68 OpenNETCF .Tapi . Line l = t.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
69 ControlTapi ctapi = new ControlTapi();
70 GeneralInfo gi = ctapi.GetGeneralInfo(l);
71
72 result = gi.SubscriberNumber;
73 l.Dispose();
74 t.Shutdown();
75
76 }
77 catch// (Exception ex)
78 {
79 result = "";
80 }
81
82 return result;
83
84 }
85
86 /// <summary>
87 /// 获取IMEI的号码
88 /// </summary>
89 /// <returns></returns>
90 public static string GetIMEINumber()
91 {
92 string result = "";
93 try
94 {
95 Tapi t = new Tapi();
96 t.Initialize();
97 OpenNETCF.Tapi.Line l = t.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
98 ControlTapi ctapi = new ControlTapi();
99 GeneralInfo gi = ctapi.GetGeneralInfo(l);
100 result = gi.SerialNumber;
101 l.Dispose();
102 t.Shutdown();
103
104 }
105 catch(Exception ex)
106 {
107 result = "";
108 }
109
110 return result;
111 }
112
113 }
114
115
116 }