Solved

Shared DLL problem with Mutex

Posted on 1999-01-19
18
648 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA "SendKeys" Syntax for Multiple Keystrokes 7 98
post4 challenge 28 83
NotAlone Challenge 20 70
wordmultiple challenge 12 90
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video discusses moving either the default database or any database to a new volume.

706 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

13 Experts available now in Live!

Get 1:1 Help Now