在VC中如何使一个程序只能运行一个实例?

dzc 2001-04-19 04:34:00
...全文
396 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dzc 2001-04-21
  • 打赏
  • 举报
回复
谢了
dzc 2001-04-21
  • 打赏
  • 举报
回复
谢了!
111222 2001-04-20
  • 打赏
  • 举报
回复
if(FindWindow(null,自己的窗口类))
退出;
else
初始化;
hyqryq 2001-04-20
  • 打赏
  • 举报
回复
在<<thinking in C++>>中有.
bighead 2001-04-20
  • 打赏
  • 举报
回复
FindWindow
Mutex....
dzc 2001-04-19
  • 打赏
  • 举报
回复
谢了!兄弟们!
dzc 2001-04-19
  • 打赏
  • 举报
回复
谢了!兄第们!
cornemuse 2001-04-19
  • 打赏
  • 举报
回复
这个问题已经有很多人问过

在APP的InitInstance中开头加入如下代码
HANDLE hMutex=::CreateMutex(NULL,TRUE,"FirstName");//FirstName可以随便取一个唯一的名字
if (hMutex!=NULL)
{
if (GetLastError()==ERROR_ALREADY_EXISTS)
{
AfxMessageBox("已经有一个程序运行");
return FALSE;
}
}
以下为该函数的其余部分,略去一部份
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));

LoadStdProfileSettings(); // Load standard INI file options (including MRU)

// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CTestView));
AddDocTemplate(pDocTemplate);

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
.........//略
fd 2001-04-19
  • 打赏
  • 举报
回复
创建一个命名信号量,然后 GetLastError 返回 表示"已经存在" 的那个值.表示已经有个实例
在运行,此时退出即可.
CreateSemaphore
GetLastError
fd 2001-04-19
  • 打赏
  • 举报
回复
创建一个命名信号量,
liu_feng_fly 2001-04-19
  • 打赏
  • 举报
回复
用一个静态变量纪录App(应用程序)的个数,超过一个就返回就行了,具体看你怎么处理了
cinlan 2001-04-19
  • 打赏
  • 举报
回复
Findwindow
panda_w 2001-04-19
  • 打赏
  • 举报
回复
这个问题太老了,你搜索一下

16,550

社区成员

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

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

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