1 static class Program
2 {
3
4 [DllImport("Toolhelp.dll")]
5 public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
6 [DllImport("Coredll.dll")]
7 public static extern int CloseHandle(IntPtr handle);
8 [DllImport("Toolhelp.dll")]
9 public static extern int Process32First(IntPtr handle, ref PROCESSENTRY32 pe);
10 [DllImport("Toolhelp.dll")]
11 public static extern int Process32Next(IntPtr handle, ref PROCESSENTRY32 pe);
12
13 public static int OpenCount = 0;
14 /// <summary>
15 /// 应用程序的主入口点。
16 /// </summary>
17 [MTAThread]
18 static void Main()
19 {
20 OpenCount = 0;
21
22 IntPtr handle = CreateToolhelp32Snapshot((uint)SnapShotFlags.TH32CS_SNAPPROCESS, 0);
23 if ((int)handle != -1)
24 {
25 PROCESSENTRY32 pe32 = new PROCESSENTRY32();
26 pe32.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
27 int bMore = Process32First(handle, ref pe32);
28 PROCESSENTRY32 pe;
29 while (bMore == 1)
30 {
31 IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
32 Marshal.StructureToPtr(pe32, temp, true);
33 pe = (PROCESSENTRY32)Marshal.PtrToStructure(temp, typeof(PROCESSENTRY32));
34 Marshal.FreeHGlobal(temp);
35 //MessageBox.Show(pe32.szExeFile);
36 if (pe32.szExeFile == "ContourErrorPreventionPDA.exe")
37 {
38 OpenCount++;
39 }
40 bMore = Process32Next(handle, ref pe32);
41 }
42 }
43 CloseHandle(handle);
44 if (OpenCount == 1)
45 {
46 Application.Run(new ContourInfo());
47 }
48 else
49 {
50 MessageBox.Show("请勿重复打开程序!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
51 return;
52 }
53 }
54 }