Solved

Shared DLL problem with Mutex

Posted on 1999-01-19
18
666 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
Define unique primary key 9 89
CRON statement to run every 3 minutes except on Monday morning 12am to 2am 7 89
scoreUp challenge 14 57
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now