精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>C/C++>>技术精解:内存、进程、线程等>>判别一个应用程序是否已经运行中的方法

主题:判别一个应用程序是否已经运行中的方法
发信人: tsingxiao()
整理人: wenbobo(2002-12-06 23:12:36), 站内信件
WIN16程序中,通过检查WinMain(... HINSTANCE hinstExecPrev)
的值来确定是否已经有程序的另一个实例正在运行
if (hinstExecPrev == NULL) {
  // no previous running
} else {
  // already running
}

在Win32下,上述方法失效,原因是: WIn32传给hinstExecPrev的永远是
NULL。解决方法:
1)
用::FindWindow()查找窗口标题或者窗口类,如果返回NULL表示没有该程
序的实例在运行。这种方法有个弊端:很多程序的TOPIC是改变的,事先无
法准确获知。

2)
设法在同一个程序的所有实例中共享一个全局变量,每次程序启动时只要检
查该变量的值就可以知道有多少个实例在运行中。

#pragma data_seg("Shared")
LONG g_iUsageCount = -1;
#pragma data_seg()

#pragma comment(linker, "/section:Shared,rws")

...

WinMain(...)
{
 BOOL fFirstInst = (InterlockedIncrement(&g_iUsageCount) == 0);

...
 if (!fFirstInst) {
 // already running 
} else {
 // first time running
}
 InterlockedDecrement(&g_iUsageCount);
 return 0;
}

--

  既不能达而兼善天下
                    只好穷而独善自身
  青山处处  斯民如土矣……

※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.101.3.150]
======================================================================
发信人: tjam (tjam), 信区: CLanguage
标  题: Re: 判别一个应用程序是否已经运行中的方法
发信站: 网易虚拟社区 (Sun Aug 22 19:01:42 1999), 站内信件
【 在 tsingxiao (脑袋) 的大作中提到: 】
: WIN16程序中,通过检查WinMain(... HINSTANCE hinstExecPrev)
: 的值来确定是否已经有程序的另一个实例正在运行
: if (hinstExecPrev == NULL) {
:   // no previous running
:    .......
Windows Topic虽然会变,但你只要::RegisterClass一下,就可以调用
::FindWindow("XXXX",NULL)这样无论Title怎样变化,注册过的class总
能找到的而且是唯一的.
For example:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs
    WNDCLASS m_class;
    BOOL bRes = CMDIFrameWnd::PreCreateWindow(cs);
    HINSTANCE hInst = AfxGetInstanceHandle();
        if (!::GetClassInfo(hInst, "JmailClass", &m_class))
        {
           ::GetClassInfo(hInst, cs.lpszClass, &m_class);
           m_class.lpszClassName = "JmailClass";
           m_class.hIcon = ::LoadIcon(hInst, MAKEINTRESOURCE(IDR_MAINF
RAME));
           ::RegisterClass(&m_class);
        }
        // see if the class already exists
        cs.lpszClass = "JmailClass";
        return bRes;
}
再在CxxApp里的InitInstance()写
if (::FindWindow("JmailClass",NULL)!=NULL)
{
        AfxMessageBox(_T("Jmail 已经运行!"));
        return FALSE;
}
--
-- 
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
^v^ Name:James Tan                ^o^
^v^ E-mail:[email protected]  ^o^
^v^ URL:http://yam.cccc.com/~sst/ ^o^
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.101.14.205]


==============================================================================
发信人: wenbobo (Sirius), 信区: CLanguage
标  题: Re: 判别一个应用程序是否已经运行中的方法
发信站: 网易虚拟社区 (Sun Aug 22 19:56:00 1999), 站内信件
【 在 tsingxiao (脑袋) 的大作中提到: 】
: WIN16程序中,通过检查WinMain(... HINSTANCE hinstExecPrev)
: 的值来确定是否已经有程序的另一个实例正在运行
: if (hinstExecPrev == NULL) {
:   // no previous running
:    .......
不彻底不彻底……我的方法是:
1.取得自己的路径文件名
2.使用tool help函数CreateToolhelp32Snapshot,Process32First,
  Process32Next...查找找同样名字的process
3.没有什么可说的了.找到了几个说明有几个实例...
--
苍天不公,为何我不是--
                     高个文静的男孩
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.104.163.218]



[关闭][返回]