百度空间 | 百度首页 
 
查看文章
 
在WinCe中判断进程是否已启动
2009-02-26 12:26

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;

        }
    }
}


类别:Win Ce | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2009-09-03 17:31 | 回复
hehe
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu