Solved

Shared DLL problem with Mutex

Posted on 1999-01-19
18
678 Views
Last Modified: 2013-11-20
I'm writing a shared DLL with mutex.

DWORD MyDLLFunction(int param1, int param2)
{
   HANDLE            hMutex ;

   hMutex = OpenMutex(SYNCHRONIZE,false,MUTEX_NAME_INIT) ;
   dwEvent = WaitForSingleObject(hMutex, MUTEX_TIMEOUT) ;
   if ((dwEvent == WAIT_TIMEOUT) ||
      (dwEvent == WAIT_FAILED))
   {
     MAINSetLastError(OPNS_MUTEX_TIMEOUT) ;
     CloseHandle(hMutex) ;
     return MAINlLastError ;
   }

   // do some treatment ...

   ReleaseMutex(hMutex) ;
   CloseHandle(hMutex) ;
   return tValRet ;
}

The first application works fine. When I launched the second, I received a WAIT_TIMEOUT from the WaitSingleObject().
What do I need to do ?
Does anyone know where I can find some info about DLL shared between applications.

Thanks in advance
0
Comment
Question by:kharandir
  • 11
  • 4
  • 2
  • +1
18 Comments
 

Expert Comment

by:koskia
ID: 1327936
comment #1:
   where do you create the mutex with CreateMutex???
   OpenMutex fails if the mutex is not created previously

comment #2:
   look comment #1

show us your whole code please
0
 

Author Comment

by:kharandir
ID: 1327937
This is the initialization of the DLL :

BOOL CPCXMFCApp::InitInstance()
{
      // misc init ...

        Secu.nLength = sizeof(SECURITY_ATTRIBUTES) ;
      Secu.lpSecurityDescriptor = NULL ;
      Secu.bInheritHandle = TRUE ;

      g_hMutexLocal = CreateMutex(&Secu,                                                                                                                                                                                    TRUE,
                            MUTEX_NAME_INIT) ;                                                              if (g_hMutexLocal == NULL)
      {
        return FALSE ;
      }

      return CWinApp::InitInstance();
}

The OpenMutex returns an handle correctly.
Then, the application calls a function.
The functions of the DLL are declared in .DEF file and generate a library file.
0
 
LVL 1

Expert Comment

by:mrgentry
ID: 1327938
Try using CreateMutex(&Secu, FALSE, MUTEX_NAME_INIT) instead.  If that application doesn't call ReleaseMutex before a 2nd application tries to call your DLL function it will block and eventually timeout.

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:kharandir
ID: 1327939
I created the mutex with false but I still have a timeout in the second application.
0
 

Author Comment

by:kharandir
ID: 1327940
I created the mutex with false but I still have a timeout in the second application.
0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 1327941

What exactly is the reason for which you need mutex in DLL. DLL by definition is
meant to be shared by application and Mutex by definition is to inhibit or prevent
sharing. Mutex is short-form of "Mutual exclusion". You use mutex when you don't want
two different process doing mutually damaging tasks at the same time. For e.g.
Withdrawal and deposit for an account at the same precise time are big problem,
so most banking s/w will use Mutexes or some of its form there.

0
 

Author Comment

by:kharandir
ID: 1327942
Why I use Mutex is that I have shared global data and two applications accessing to it may cause some problems.
Imagine that a DLL is used for withdrawal and deposit ...
0
 

Author Comment

by:kharandir
ID: 1327943
Why I use Mutex is that I have shared global data and two applications accessing to it may cause some problems.
Imagine that a DLL is used for withdrawal and deposit ...
0
 
LVL 1

Expert Comment

by:mrgentry
ID: 1327944
I made myself a small test DLL which contains your function exactly as above that does nothing but add two numbers together for the 'do some treatment', and created one small dialog based application that creates the mutex exactly as you do and which calls the DLL function when you hit a button.  Also a second dialog based application that similarly calls the function when you hit a button.  If the mutex is created with CreateMutex(&Secu, TRUE, MUTEX_NAME_INIT), then the 2nd application always times out.  However, if it's created with CreateMutex(&Secu, FALSE, MUTEX_NAME_INIT), the 2nd application obtains ownership of the mutex with no problems.

Since you said that you tried creating the mutex with the 2nd argument set to FALSE and it still didn't work, the only other thing I can think of that would cause the problem would be if the first application calls the DLL function before the 2nd application does, but returns from the function without calling ReleaseMutex.
0
 

Author Comment

by:kharandir
ID: 1327945
Why I use Mutex is that I have shared global data and two applications accessing to it may cause some problems.
Imagine that a DLL is used for withdrawal and deposit ...
0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 1327946
OK


In that case I would construct DLL code to look like following. experiment around
with the time out value till it is acceptable.

class CBankAccount
{
      HANDLE m_hMutex;
      LPSECURITY_ATTRIBUTES m_Secu;

      BOOL SecureMutex();

      public:

      CBankAccount();
      ~CBankAccount();

      void Withdrawal();
      void Deposit();
};

CBankAccount::CBankAccount()
{
      m_Secu.nLength = sizeof(SECURITY_ATTRIBUTES) ;
      m_Secu.lpSecurityDescriptor = NULL ;
      m_Secu.bInheritHandle = TRUE ;

      m_hMutex = CreateMutex(&Secu, TRUE, MUTEX_NAME_INIT) ;
}


CBankAccount::~CBankAccount()
{
      if (m_hMutex)
      {
            ReleaseMutex(m_hMutex) ;
            CloseHandle(m_hMutex) ;
      }
}


BOOL CBankAccount::SecureMutex()
{
      if (hMutex)
      {
            DWORD dwEvent = WaitForSingleObject(hMutex, MUTEX_TIMEOUT) ;
            if (dwEvent == WAIT_OBJECT_0)
            {
                  return TRUE;
            }
       }

       return FALSE;
}

void CBankAccount::Withdrawal()
{
      if (SecureMutex())
      {
            // Perform withdrawal
            ReleaseMutex(m_hMutex) ;
      }
}

void CBankAccount::Deposit()
{
      if (SecureMutex())
      {
            // Perform deposit
            ReleaseMutex(m_hMutex) ;
      }
}


0
 

Author Comment

by:kharandir
ID: 1327947
Why I use Mutex is that I have shared global data and two applications accessing to it may cause some problems.
Imagine that a DLL is used for withdrawal and deposit ...
0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 1327948
you typed in same comment 3 times? I think If you are trying to stress some point,
Iam not getting it, please be bit more elaborate.

What Iam suggesting to you is to construct your DLL in such a way that it keeps
track of who is using it and who is not, by localizing mutex operations functions
only in DLL. Clients of DLL will only see the end functions like in my example class
functions Withdrawal and Deposit. Both of them rely on function SecureMutex
to ensure consistency. In case of withdrawal, I would have code wait for Mutex
infinitely since I preety much have a top handle on time limit it will take to
perform the task. In your case, you have to make that decision yourselves.



0
 

Author Comment

by:kharandir
ID: 1327949
I'm sorry but I dont send several times the same comment. I see that when I post a comment, it is automatically "doubled". Maybe it's my browser (Opera is sometimes very strange).

b.t.w. I made as you said and for the moment I have no more problem.

Answer and you'll get the points.

Thanks
0
 

Author Comment

by:kharandir
ID: 1327950
I'm sorry but I dont send several times the same comment. I see that when I post a comment, it is automatically "doubled". Maybe it's my browser (Opera is sometimes very strange).

b.t.w. I made as you said and for the moment I have no more problem.

Answer and you'll get the points.

Thanks
0
 

Author Comment

by:kharandir
ID: 1327951
See, the comment appears twice.
0
 

Author Comment

by:kharandir
ID: 1327952
See, the comment appears twice.
0
 
LVL 2

Accepted Solution

by:
SamratAshok earned 110 total points
ID: 1327953
Here is my claim for points
for future reference as PAQ, Iam re-pasting answer




       In that case I would construct DLL code to look like following. experiment around
       with the time out value till it is acceptable.

       class CBankAccount
       {
       HANDLE m_hMutex;
       LPSECURITY_ATTRIBUTES m_Secu;

       BOOL SecureMutex();

       public:

       CBankAccount();
       ~CBankAccount();

       void Withdrawal();
       void Deposit();
       };

       CBankAccount::CBankAccount()
       {
       m_Secu.nLength = sizeof(SECURITY_ATTRIBUTES) ;
       m_Secu.lpSecurityDescriptor = NULL ;
       m_Secu.bInheritHandle = TRUE ;

       m_hMutex = CreateMutex(&Secu, TRUE, MUTEX_NAME_INIT) ;
       }


       CBankAccount::~CBankAccount()
       {
       if (m_hMutex)
       {
       ReleaseMutex(m_hMutex) ;
       CloseHandle(m_hMutex) ;
       }
       }


       BOOL CBankAccount::SecureMutex()
       {
       if (hMutex)
       {
       DWORD dwEvent = WaitForSingleObject(hMutex, MUTEX_TIMEOUT) ;
       if (dwEvent == WAIT_OBJECT_0)
       {
       return TRUE;
       }
       }

       return FALSE;
       }

       void CBankAccount::Withdrawal()
       {
       if (SecureMutex())
       {
       // Perform withdrawal
       ReleaseMutex(m_hMutex) ;
       }
       }

       void CBankAccount::Deposit()
       {
       if (SecureMutex())
       {
       // Perform deposit
       ReleaseMutex(m_hMutex) ;
       }
       }



0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 57
Host to IP 7 78
Smart Camera scanning and reading information 3 106
abstract class with all non abstract mentods 6 76
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question