Click here to Skip to main content
15,912,578 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problem with sub menus Pin
tim63520-Mar-05 21:56
tim63520-Mar-05 21:56 
GeneralRe: Problem with sub menus Pin
KaЯl20-Mar-05 23:11
KaЯl20-Mar-05 23:11 
GeneralRe: Problem with sub menus Pin
tim63521-Mar-05 2:59
tim63521-Mar-05 2:59 
GeneralShareable objects... Pin
BlackSmith12-Feb-03 9:05
BlackSmith12-Feb-03 9:05 
GeneralRe: Shareable objects... Pin
palbano12-Feb-03 9:26
palbano12-Feb-03 9:26 
GeneralRe: Shareable objects... Pin
BlackSmith12-Feb-03 9:48
BlackSmith12-Feb-03 9:48 
GeneralRe: Shareable objects... Pin
palbano12-Feb-03 11:58
palbano12-Feb-03 11:58 
GeneralRe: Shareable objects... Pin
Chris Richardson12-Feb-03 11:58
Chris Richardson12-Feb-03 11:58 
Indeed, some kernel objects can be queried using WaitForSingleObject (events, semaphores, mutexes ...). Here's a sample that I didn't bother to build or test, but it should be pretty close to what you need:

class CSharableObject
{
public:
   CSharableObject( const TCHAR * p_pszName ) :
      c_hMutex( NULL )
   {
      c_hMutex = OpenMutex( SYNCHRONIZE, FALSE, p_pszName );
      if( c_hMutex == NULL )
      {
         c_hMutex = CreateMutex( NULL, FALSE, p_pszName );
         if( c_hMutex == NULL )
         {
            // Handle the error somehow...
         }
      }
   }

   ~CSharableObject()
   {
      if( c_hMutex != NULL )
      {
         CloseHandle( c_hMutex );
         c_hMutex = NULL;
      }
   }

   bool IsLocked()
   {
      // Test if some other thread has the mutex already...
      DWORD a_dwReason = WaitForSingleObject( c_hMutex, 0 );
      if( a_dwReason == WAIT_TIMEOUT )
         return true;
      return false;
   }

   unsigned long Lock( unsigned long p_ulTimeout = INFINITE )
   {
      // Wait for another thread to give up the mutex.
      return WaitForSingleObject( c_hMutex, p_ulTimeout );
   }

   bool Unlock()
   {
      // Give up the mutex.
      return ReleaseMutex( c_hMutex ) != 0;
   }

protected:
   HANDLE c_hMutex;
};


Now, whereever you need to use the shared resource:

CSharableObject a_oLocker( _T("SomeUniqueName") );
 
// Lock the object.
a_oLocker.Lock();
 
// ... use the precious resource here...
 
// And when we are done we unlock it.
a_oLocker.Unlock();
 
// OR
if( !a_oLocker.IsLocked() )
{
   // Take one course of action.
}
else
{
   // Take another course of action.
}


It would be a good idea to make this more robust etc, but it's a start.


Chris Richardson

C/C++ Include Finder[^]
QuestionCan I add a menu to a dialog based application? Pin
work_to_live12-Feb-03 8:48
work_to_live12-Feb-03 8:48 
AnswerRe: Can I add a menu to a dialog based application? Pin
Chris Losinger12-Feb-03 8:52
professionalChris Losinger12-Feb-03 8:52 
GeneralRe: Can I add a menu to a dialog based application? Pin
work_to_live12-Feb-03 9:05
work_to_live12-Feb-03 9:05 
GeneralCImage & Transparency Pin
loading12-Feb-03 8:01
loading12-Feb-03 8:01 
QuestionReliable way to convert float to a double? Pin
Member 9612-Feb-03 7:40
Member 9612-Feb-03 7:40 
AnswerRe: Reliable way to convert float to a double? Pin
Chris Losinger12-Feb-03 7:51
professionalChris Losinger12-Feb-03 7:51 
GeneralRe: Reliable way to convert float to a double? Pin
Member 9612-Feb-03 8:10
Member 9612-Feb-03 8:10 
GeneralRe: Reliable way to convert float to a double? Pin
Chris Losinger12-Feb-03 8:20
professionalChris Losinger12-Feb-03 8:20 
GeneralRe: Reliable way to convert float to a double? Pin
Member 9612-Feb-03 8:30
Member 9612-Feb-03 8:30 
GeneralRe: Reliable way to convert float to a double? Pin
Alvaro Mendez12-Feb-03 9:01
Alvaro Mendez12-Feb-03 9:01 
GeneralRe: Reliable way to convert float to a double? Pin
Member 9612-Feb-03 8:41
Member 9612-Feb-03 8:41 
AnswerRe: Reliable way to convert float to a double? Pin
Tim Smith12-Feb-03 7:52
Tim Smith12-Feb-03 7:52 
GeneralRe: Reliable way to convert float to a double? Pin
Member 9612-Feb-03 7:55
Member 9612-Feb-03 7:55 
GeneralRe: Reliable way to convert float to a double? Pin
Stuart Dootson12-Feb-03 8:29
professionalStuart Dootson12-Feb-03 8:29 
GeneralRe: Reliable way to convert float to a double? Pin
Member 9612-Feb-03 8:37
Member 9612-Feb-03 8:37 
GeneralRe: Reliable way to convert float to a double? Pin
Stuart Dootson12-Feb-03 21:55
professionalStuart Dootson12-Feb-03 21:55 
AnswerThis will work, while ugly... Pin
Abin12-Feb-03 16:58
Abin12-Feb-03 16:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.