123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include "stdafx.h"
- #include "MyLock.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- HANDLE Lock(char* name)
- {
- try
- {
- HANDLE mutex;
-
- mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, name);
- if(NULL == mutex)
- {
- mutex = CreateMutex(NULL, TRUE, name);
- }
- else
- {
- WaitForSingleObject(mutex, INFINITE);
- }
- return mutex;
- }
- catch(...)
- {
- }
- }
- bool Unlock(HANDLE mutex)
- {
- try
- {
- if(0 == ReleaseMutex(mutex))
- {
- return false;
- }
- else
- {
- CloseHandle(mutex);
- mutex = NULL;
- return true;
- }
- }
- catch(...)
- {
- }
- }
- MyLock::MyLock(CString name)
- {
- m_handle=Lock((TCHAR*)(LPCTSTR)name);
- }
- MyLock::~MyLock()
- {
- Unlock(m_handle);
- }
|