BOOL CDummyApp::InitInstance()
{
/////////// Détection
appli précédente ////////////
HANDLE hMutex = CreateMutex(NULL,
FALSE, "MonMutexUniqueAMoiToutSeul");
if(!hMutex)
{
AfxMessageBox("Erreur
à la création du mutex", MB_OK | MB_ICONSTOP);
return
FALSE;
}
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
AfxMessageBox("L'appli
tourne déjà!!", MB_OK | MB_ICONEXCLAMATION);
CloseHandle(hMutex);
return
FALSE;
}
.........
En sortie, (en fin de OnInitDialog pour une appli en boîte de dialogue, et dans ExitInstance pour une appli avec des fenêtres), ajouter:
// Ferme le mutex en sortie
CloseHandle(hMutex);
// Lancement du programme en tant que Process
PROCESS_INFORMATION Process;
STARTUPINFO StartInfo;
memset(&StartInfo, 0, sizeof(StartInfo));
StartInfo.cb = sizeof(StartInfo);
if(!CreateProcess(NULL, "ProgrammeALancer.exe",
NULL, NULL, FALSE,
CREATE_DEFAULT_ERROR_MODE,
NULL, NULL, &StartInfo, &Process))
{
// Erreur au lancement du programme
}
// Attente de la fin du programme
WaitForSingleObject(Process.hProcess, INFINITE);
// On continue après la fin du programme lancé...
int CDlgOvaleDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct)
== -1)
return
-1;
HRGN hRgn;
RECT rect;
GetClientRect(&rect);
hRgn = CreateEllipticRgn(0,0,rect.right,rect.bottom);
SetWindowRgn(m_hWnd,hRgn,TRUE);
SetProp(m_hWnd,"region",hRgn);
return 0;
}
void CDlgOvaleDlg::OnDestroy()
{
CDialog::OnDestroy();
if (GetProp(m_hWnd,"region"))
{
DeleteObject(GetProp(m_hWnd,"region"));
RemoveProp(m_hWnd,"region");
}
}
LPMALLOC pMalloc;
/* Gets the Shell's default allocator */
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
BROWSEINFO bi;
char pszBuffer[MAX_PATH];
LPITEMIDLIST pidl;
// Get help on BROWSEINFO struct
- it's got all the bit settings.
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = _T("Select a
Starting Directory");
bi.ulFlags = BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
// This next call issues the
dialog box.
if ((pidl = ::SHBrowseForFolder(&bi))
!= NULL)
{
if
(::SHGetPathFromIDList(pidl, pszBuffer))
{
// At this point pszBuffer contains the selected path */.
DoingSomethingUseful(pszBuffer);
}
//
Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
// Release the shell's allocator.
pMalloc->Release();
}