+ -
当前位置:首页 → 问答吧 → WinCE怎么判断进程是否已经存在???

WinCE怎么判断进程是否已经存在???

时间:2011-12-08

来源:互联网

在网上找到这端代码
C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    /// <summary>
    /// MobileUtils
    /// </summary>
    public class MobileUtils
    {

        /// <summary>
        /// 判断某个进程是否已经存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool IsProcessRunning(string processName)
        {
            int hMutex;
            hMutex = CreateMutex(null, false, processName);

            if (GetLastError() == ERROR_ALREADY_EXISTS)
            {
                ReleaseMutex(hMutex);
                return true;
            }
            return false;
        }

        /// <summary>
        /// GetLastError
        /// </summary>
        /// <returns></returns>
        [DllImport("coredll.Dll")]
        private static extern int GetLastError();

        /// <summary>
        /// ReleaseMutex
        /// </summary>
        /// <param name="hMutex"></param>
        /// <returns></returns>
        [DllImport("coredll.Dll")]
        private static extern int ReleaseMutex(int hMutex);

        /// <summary>
        /// CreateMutex
        /// </summary>
        /// <param name="lpMutexAttributes"></param>
        /// <param name="bInitialOwner"></param>
        /// <param name="lpName"></param>
        /// <returns></returns>
        [DllImport("coredll.Dll")]
        private static extern int CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, string lpName);

        /// <summary>
        /// 
        /// </summary>
        private const int ERROR_ALREADY_EXISTS = 0183;

        /// <summary>
        /// SECURITY_ATTRIBUTES
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        private class SECURITY_ATTRIBUTES
        {

            /// <summary>
            /// 
            /// </summary>
            public int nLength;

            /// <summary>
            /// 
            /// </summary>
            public int lpSecurityDescriptor;

            /// <summary>
            /// 
            /// </summary>
            public int bInheritHandle;

        }
    }
}



我按照这个做的,我的工程名字是AProject,可是检测时候时候即使我已经开启了这个软件,再次开启时还是检测不到开启,请问CreateMutex(null, false, processName);中得软件名字该写什么呢?

作者: nkhuangyanping   发布时间: 2011-12-08

估计我这个不支持,大家知道有什么别的办法吗?

作者: nkhuangyanping   发布时间: 2011-12-08