wince 防止程序重复打开

 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 }
View Code
 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     }
View Code

 

转载于:https://www.cnblogs.com/wwwwxxxxffffblog/p/7196009.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值