我用Mutex让系统只运行一个实例,如果发现已有一个Mutex对象(程序已在运行),怎么获得这个程序的进程ID呢?我想获得程序的进程ID后让程序显示在最前端。

解决方案 »

  1.   

    参考: HWND hMutex=FindWindow(wc.lpszClassName,NULL);//获取互斥实例的窗口句柄
    if(hMutex)
    {
    if (IsIconic(hMutex))//如果最小化 
    ShowWindow(hMutex,SW_RESTORE);//回复正常
    SetForegroundWindow(hMutex);//显示窗体到最顶端
    return FALSE;
    }
      

  2.   

    [DllImport("User32.dll", EntryPoint = "FindWindow")]
    引入FindWindow后怎么用Mutex去找已运行的实例句柄?
      

  3.   

    只要构造一个Mutex就行了, 判断第三个out参数。public Mutex(
    bool initiallyOwned,
    string name,
    out bool createdNew
    )
      

  4.   

    我的意思是想在createdNew为FALSE时找到运行程序的句柄。
      

  5.   

    其实已经告诉你方向,你再深入一下就知道了,写了个示例给你看看吧:static class Program
        {
            [DllImport("User32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);        [DllImport("User32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Process process = RuningInstance();
                if (process == null)
                {
                    Application.Run(new Form1());
                }
                else
                {
                    MessageBox.Show("应用程序已经在运行中");
                    HandleRunningInstance(process);
                }
            }        private const int SW_SHOWNOMAL = 1;
            private static void HandleRunningInstance(Process instance)
            {
                ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOMAL);//显示
                SetForegroundWindow(instance.MainWindowHandle);//当到最前端
            }
            private static Process RuningInstance()
            {
                Process currentProcess = Process.GetCurrentProcess();
                Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);
                foreach (Process process in Processes)
                {
                    if (process.Id != currentProcess.Id)
                    {
                        if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName)
                        {
                            return process;
                        }
                    }
                }
                return null;
            }
        }
      

  6.   

    还有疑问:
    bool canCreate;
    Mutex myMutex = new Mutex(true, "MutexNameString", out canCreate);
    if(canCreate)
    {
        ...
    }
    else
    {
        IntPtr hWnd = FindWindow(lpClassName, null);
        ShowWindowAsync(hWnd, 1);
        SetForegroundWindow(hWnd);
    }
    这里的lpClassName我是通过spy++查到的,有没有什么函数可以直接取得呢?
    能不能通过已经存在的名称为"MutexNameString"的Mutex对象取得hWnd呢?
      

  7.   

    http://0123.blog.163.com/blog/static/478831200710215312818/给你个链接
      

  8.   

    请参考这个
    http://www.cnblogs.com/mediar/archive/2005/08/25/223021.html
      

  9.   

    这种方法还是有问题,就是把程序改名字后运行就会出现问题。
    还是Mutex + FindWindow + spy++有效。