wince单实例启动

Posted ives

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wince单实例启动相关的知识,希望对你有一定的参考价值。

static class Program
    {
        [DllImport("Toolhelp.dll")]
        public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
        [DllImport("Toolhelp.dll")]
        public static extern int Process32First(IntPtr handle, ref PROCESSENTRY32 pe);
        [DllImport("Toolhelp.dll")]
        public static extern int Process32Next(IntPtr handle, ref PROCESSENTRY32 pe);

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [MTAThread]
        static void Main()
        {
            IntPtr handle = CreateToolhelp32Snapshot((uint)SnapShotFlags.TH32CS_SNAPPROCESS, 0);
            int count = 0;
            if ((int)handle != -1)
            {
                PROCESSENTRY32 pe32 = new PROCESSENTRY32();
                pe32.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
                int bMore = Process32First(handle, ref pe32);
                while (bMore == 1)
                {
                    if (pe32.szExeFile == "WareHouse.exe")//需要与程序保持一致
                        count++;
                    bMore = Process32Next(handle, ref pe32);
                }
            }
            if (count > 1)
            {
                MessageBox.Show("程序已经运行!");
                return;
            }
            else
                Application.Run(MainFram.GetInstance());
            
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESSENTRY32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]//注意,此处为宽字符
            public string szExeFile;
            public uint th32MemoryBase;
            public uint th32AccessKey;
        }

        public enum SnapShotFlags : uint
        {
            TH32CS_SNAPHEAPLIST = 0x00000001,
            TH32CS_SNAPPROCESS = 0x00000002,
            TH32CS_SNAPTHREAD = 0x00000004,
            TH32CS_SNAPMODULE = 0x00000008,
            TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE),
            TH32CS_GETALLMODS = 0x80000000
        }
    }

 

以上是关于wince单实例启动的主要内容,如果未能解决你的问题,请参考以下文章

windows mobile ,wince 系统,用代码启动cab文件安装

Wince常见操作

WinCE compact 7 VS2008 调试模式启动应用错误

WinCE在启动界面无法进入系统

WinCE 系统刚启动时运行应用,在应用启动时偶尔出现异常

WiX 自定义引导程序 - 单实例检查