一个关于“一次只能启动一个实例”程序的问题

rengo 2002-08-04 10:24:54
我找到一个一次只能启动一个程序实例的源代码,下面是它里面的一个函数,用来判断自己的程序是否已经启动了,我弄不明白FindWindow(_T("#32770"),"Instance")里面的_T("#32770")是哪里来的,按道理_T("#32770")是表示window class name,可是该程序的作者是怎么知道自己窗口的class name呢?(这个程序只有一个Dialog窗口),我把_T("#32770")随便改成其他数字,发现不行,希望大家帮帮忙,谢谢了?还有,要怎么知道自己窗口的class name呢?

BOOL CInstanceApp::FirstInstance()
{
CWnd *pWndPrev , *pWndChild;

// Determine if another window with our class name and Window title exists...
// The title "Instance " is set up latter, in the InitDialog function.

if (pWndPrev = CWnd::FindWindow(_T("#32770"),"Instance"))//
{
pWndChild = pWndPrev->GetLastActivePopup(); // if so, does it have any popups?

if (pWndPrev->IsIconic())
pWndPrev->ShowWindow(SW_RESTORE); // If iconic, restore the main window

pWndChild->SetForegroundWindow(); // Bring the main window or it's popup to
// the foreground
// and we are done activating the previous one.
return FALSE;
}
else
return TRUE; // First instance. Proceed as normal.
}
还有其他“一次只能启动一个实例”的方法吗
...全文
194 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rengo 2002-08-05
  • 打赏
  • 举报
回复
用SPY++确实看到了,谢谢大家,英语还没过12级看不懂上面的English
papaya_stone 2002-08-04
  • 打赏
  • 举报
回复
用FindWindow()找到窗口的句柄,再用GetClassName()就可以得到窗口的类名。
xuying 2002-08-04
  • 打赏
  • 举报
回复
HOWTO: Limit 32-bit Applications to One Instance Using C++
ID: Q243953



------------------------------------------------------------------------
--------
The information in this article applies to:

Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1, 4.2, 5.0, 6.
0

------------------------------------------------------------------------
--------


SUMMARY
This article shows how to limit an application to one instance. It
does not rely on any creation of windows, so the method used in this
article could be used for about any 32-bit application that is developed
in C++. This includes console applications, WinCE applications,
dialog box based applications, applications without a graphical user
interface and many others.



MORE INFORMATION
The method used in this article is the one described in MSDN under the
WinMain topic. It uses the CreateMutex() function to create a named
mutex that can be checked across processes. Instead of duplicating the
same code for every application that intends to be used as a single
instance, the needed code is in a C++ wrapper class that can be reused
across each application.

In order to use this functionality, you can use the following steps:



Create a new header file with the name LimitSingleInstance.h and add
it to your project.


Copy the following code into the LimitSingleInstance.h file and save the
file.



#ifndef LimitSingleInstance_H
#define LimitSingleInstance_H

#include <windows.h>

//this code is from Q243953 in case you lose the article and wonder
//where this code came from...
class CLimitSingleInstance
{
protected:
DWORD m_dwLastError;
HANDLE m_hMutex;

public:
CLimitSingleInstance(TCHAR *strMutexName)
{
//be sure to use a name that is unique for this application
otherwise
//two apps may think they are the same if they are using same name
for
//3rd parm to CreateMutex
m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
m_dwLastError = GetLastError(); //save for use later...
}

~CLimitSingleInstance()
{
if (m_hMutex) //don't forget to close handles...
{
CloseHandle(m_hMutex); //do as late as possible
m_hMutex = NULL; //good habit to be in
}
}

BOOL IsAnotherInstanceRunning()
{
return (ERROR_ALREADY_EXISTS == m_dwLastError);
}
};
#endif
#include the LimitSingleInstance.h file where the entry point of the
program is located. If this is to be used in an MFC Application, it is
the file where the InitInstance() function for the application is
located. In a Win32 SDK application, it is where the WinMain()
function is located. In a console application, it is where the main()
function is located.



#include "LimitSingleInstance.H"
Create a global instance of the CLimitSingleInstance class before the
entry point function. If this is being used in a MFC application, create
the instance before the InitInstance() function.


Pass a unique name to the constructor of the global CLimitSingleInstance
instance. It is recommended to use a unique name so another application
that may be using this article will not conflict when doing the
duplicate checking. An easy way to get a unique name that no one else
will have is to use the GUIDGEN tool. You can access the tool by
typing GUIDGEN from the Start button and then select Run in Windows.
If for some reason you do not have the tool, the tool is provided as a
sample in MSDN. Type in GUIDGEN in the MSDN index to find it. Be sure to
use the Registry Format option in the GUIDGEN tool.



#include "LimitSingleInstance.H"

// The one and only CLimitSingleInstance object
// Change what is passed to constructor. GUIDGEN Tool may be of help.
CLimitSingleInstance
g_SingleInstanceObj(TEXT("{719967F0-DCC6-49b5-9C61-DE91175C3187}"));
In your entry point function, call the IsAnotherInstanceRunning() method
on the global instance of the CLimitSingleInstance class and check
the return value. If the function returns TRUE, return from the entry
point function. Otherwise, continue execution as normal.

In an MFC Application, you could do something like the following:

#include "LimitSingleInstance.H"

// The one and only CLimitSingleInstance object
CLimitSingleInstance
g_SingleInstanceObj(TEXT("{05CA3573-B449-4e0b-83F5-7FD612E378E9}"));

BOOL CSingleInstDlg5App::InitInstance()
{
if (g_SingleInstanceObj.IsAnotherInstanceRunning())
return FALSE;

//rest of code
}


In a Console Application, you could do something like the following:

#include "LimitSingleInstance.H"

// The one and only CLimitSingleInstance object
CLimitSingleInstance
g_SingleInstanceObj(TEXT("{9DA0BEED-7248-450a-B27C-C0409BDC377D}"));

int main(int argc, char* argv[])
{
if (g_SingleInstanceObj.IsAnotherInstanceRunning())
return 0;
//rest of code
}


In a Win32 SDK Application, you could do something like the following:


#include "LimitSingleInstance.H"

// The one and only CLimitSingleInstance object
CLimitSingleInstance
g_SingleInstanceObj(TEXT("{2194ABA1-BFFA-4e6b-8C26-D191BB16F9E6}"));

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int cmdShow)
{
if (g_SingleInstanceObj.IsAnotherInstanceRunning())
return FALSE;
//rest of code
}



After following the above steps, the application will not allow more
than one instance to remain active at one time.
xuying 2002-08-04
  • 打赏
  • 举报
回复
有三种办法:
1。注册自己的特有类,找出它即可。
2。用mutex
3。用volatile
Wargod2002 2002-08-04
  • 打赏
  • 举报
回复
u can look over it use spy++,or use the api GetClassName.
there are several ways else to archieve you purpose.
the simplest one is to create a name object using CreateMutext.
javanew 2002-08-04
  • 打赏
  • 举报
回复
先把程序起来,再用spy看看这个窗口的一些信息,其中就有class name
javanew 2002-08-04
  • 打赏
  • 举报
回复
你用visual studio 带的spy看一下不就知道窗口的class名了吗?
spy会不会用?

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧