Windows 创建N个MFC应用程序实例

Windows 创建N个MFC应用程序实例,windows,visual-c++,mfc,Windows,Visual C++,Mfc,有人能告诉我如何在MFC中创建最多N个应用程序实例吗 另外,如果N个实例正在运行,并且一个实例关闭,则可以创建一个新实例(但一次运行的实例不能超过N个) 先谢谢你。 a、 我会使用锁文件。在CMyApp::InitInstance()中添加: CString Path; // better get the path to the global app data or local user app data folder, // depending on if you want to allow

有人能告诉我如何在MFC中创建最多N个应用程序实例吗

另外,如果N个实例正在运行,并且一个实例关闭,则可以创建一个新实例(但一次运行的实例不能超过N个)

先谢谢你。
a、 我会使用锁文件。在
CMyApp::InitInstance()中添加:

CString Path;
// better get the path to the global app data or local user app data folder, 
// depending on if you want to allow the three instances machine-wide or per user.
// Windows' file system virtualization will make GetModuleFileName() per user:
DWORD dw = GetModuleFileName(m_hInstance, 
                Path.GetBuffer(MAX_PATH), MAX_PATH); 
Path.ReleaseBuffer();
// strip "exe" from filename and replace it with "lock"
Path = Path.Left(Path.GetLength()-3) + T("lock"); 
int i;
// better have the locking file in your class and do a clean Close on ExitInstance()!
CFile *pLockingFile = NULL; 
for (i = 0; i < 3; i++) // restrict to three instances
{
    CString Counter;
    Counter.Format(T("%d"), i);
    TRY
    {
        pLockingFile = new CFile(Path + Counter, 
            CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive);
        pLockingFile.Close();
        break; // got an instance slot
    }
    CATCH( CFileException, e )
    {
        // maybe do something else here, if file open fails
    }
    END_CATCH

    if (i >= 3)
        return TRUE;    // close instance, no slot available
}

注意:这仅适用于上的Vista。如果您有XP,写入全局目录是一项简单的任务,例如C:\Windows\Temp。我把函数放在一个助手dll中,我只在操作系统是Vista或更高版本时加载它。否则,由于系统DLL中存在未解析的引用,您的软件将无法启动。

您可以创建一个全局信号量,最多可以输入n个进程实例。进程的第n+1个实例将无法输入信号量。当然,您应该为锁定操作选择一个较短的超时,以便向用户提供有意义的反馈


关于信号量的内容,您可以看一下。

您知道如何创建一个实例吗?对不起,我不明白。我想限制实例的数量。你的问题没有提到限制实例的数量。您想知道如何创建多个实例。请将问题修改得更清楚。嗨,雷蒙德-我想让应用程序最多运行N个实例。允许更少的实例。不应该有更多的实例。如果某些实例已关闭,则可以创建新实例,但每个用户、桌面、计算机或网络最多可创建N.N个实例?您好,Thomiel-感谢您的回复。我已经实现了您所写内容的一个版本。但是,没有什么可以阻止某人将可执行文件复制到另一个目录,然后再运行N个可执行文件实例。是否有任何方法可以锁定整个计算机最多N个实例(不考虑目录/路径和用户)?提前谢谢。另一个编辑:请在代码中将sizeof(MAX_PATH)替换为MAX_PATH!嗨,托米尔-谢谢你。在机器范围的N个实例限制中:我已经尝试了您的第2个代码,但是不得不将char*path修改为wchar\u t*path——现在已经编译了。但是,SHGetSpecialFolderPath(NULL,path,CSIDL_COMMON_APPDATA,TRUE)返回FALSE(因此GetCommonAppDataPath返回FALSE)。我正在使用Visual Studio 2013和Windows 7 Home Premium。你能帮帮我吗?不,不行。但请记住:GetLastError()是您的朋友!;)
#pragma warning(disable: 4996) // no risk, no fun
BOOL GetCommonAppDataPath(char *path)
{
    *path = '\0';

    if (SHGetSpecialFolderPath(NULL, path, CSIDL_COMMON_APPDATA, TRUE))
    {
        strcat(path, T("\\MyApplication")); // usually found under C:\ProgramData\MyApplication
        DWORD dwFileStat = GetFileAttributes(path);
        if (dwFileStat == 0xffffffff)   // no MyApplication directory yet?
            CreateDirectory(path, NULL);    // create it

        dwFileStat = GetFileAttributes(path);   // 2nd try, just to be sure
        if (dwFileStat == 0xffffffff || !(dwFileStat & FILE_ATTRIBUTE_DIRECTORY))
            return FALSE;

        return TRUE;
    }

    return FALSE;
}