15,979
社区成员
发帖
与我相关
我的任务
分享
CString CSamePathTestDlg::GetMypath()
{
// TODO: Add your control notification handler code here
//用程序所在路径
CString m_FilePath;
GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
m_FilePath.ReleaseBuffer();
int m_iPosIndex;
m_iPosIndex = m_FilePath.ReverseFind('\\');
m_FilePath = m_FilePath.Left(m_iPosIndex);
return m_FilePath+"\\SamePathTest.exe";
}
BOOL CSamePathTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//如果该程序已经运行,提示只允许运行一个该程序
cur_exe_path = GetMypath();
HANDLE hMutex;
hMutex= CreateMutex(NULL,TRUE, "SamePathTest.exe");
if(hMutex && GetLastError()==ERROR_ALREADY_EXISTS)
{
//请问:如果在已经找到了运行的SamePathTest.exe程序的HANDLE了
//怎么根据HANDLE找这些已经运行程序的绝对路径
//如有同一路径下则提示
AfxMessageBox("该程序已经运行!");
ReleaseMutex(hMutex);
exit(0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
HRESULT CGlobalTool::CheckRunInSameDirectory( BOOL bExitIfExist )
{
CFilePath filepath;
CString strFullFileName = filepath.GetFullPathName();
strFullFileName.Replace( _T('\\'), _T('.') );
HANDLE hInstance = ::CreateMutex( NULL, TRUE, strFullFileName );
DWORD error = GetLastError();
if( bExitIfExist && error == ERROR_ALREADY_EXISTS)
{
CloseHandle(hInstance);
::MessageBox(NULL, _T("该程序已被打开! 禁止打开两个以上的相同目录下的程序!"), _T("提示"), MB_OK );
return E_FAIL;
}
return S_OK;
}