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 ///
15 /// 应用程序的主入口点。
16 ///
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 }
1 [StructLayout(LayoutKind.Sequential)]
2 public struct PROCESSENTRY32
3 {
4 public uint dwSize;
5 public uint cntUsage;
6 public uint th32ProcessID;
7 public IntPtr th32DefaultHeapID;
8 public uint th32ModuleID;
9 public uint cntThreads;
10 public uint th32ParentProcessID;
11 public int pcPriClassBase;
12 public uint dwFlags;
13 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]//注意,此处为宽字符
14 public string szExeFile;
15 public uint th32MemoryBase;
16 public uint th32AccessKey;
17 }
18
19 public enum SnapShotFlags : uint
20 {
21 TH32CS_SNAPHEAPLIST = 0x00000001,
22 TH32CS_SNAPPROCESS = 0x00000002,
23 TH32CS_SNAPTHREAD = 0x00000004,
24 TH32CS_SNAPMODULE = 0x00000008,
25 TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE),
26 TH32CS_GETALLMODS = 0x80000000
27 }