Solved

CreateMutex problem

Posted on 2001-07-25
12
2,569 Views
Last Modified: 2013-11-20
I have a COM dll that is run inprocess by two separate executables - one of the executables is run as a service.  The OS that both executables are running on is NT4.  That said, now here's the problem:

In the dll, there is an interface that has a call to CreateMutex which succeeds if the service executable is not running and the other executable is just accessing the dll standalone.

However if the service executable is started, the call to CreateMutex fails if the non service executable tries to call on the dlls interface.  This stops the non service excutable program from working properly.

Can anyone give me the solution to this problem, so that both executables can access the dlls methods at the same time?

Thanks

0
Comment
Question by:carlpaddick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6317155
You'll have to provide tha appropriate security settings in order for this to work:

HANDLE                      hMutex;

PSID                        psidWorldSid    =   NULL;
SECURITY_DESCRIPTOR         sd;
SECURITY_ATTRIBUTES         sa;

SID_IDENTIFIER_AUTHORITY    siaWorldSidAuthority    =   SECURITY_WORLD_SID_AUTHORITY;

    psidWorldSid    =   ( PSID) LocalAlloc  (   LPTR,
                                                GetSidLengthRequired    (   1)
                                            );

    InitializeSid   (   psidWorldSid,   &siaWorldSidAuthority,  1);

    *(  GetSidSubAuthority  (   g_psidWorldSid, 0)) =   SECURITY_WORLD_RID;

    InitializeSecurityDescriptor    (   &sd,    SECURITY_DESCRIPTOR_REVISION);

    SetSecurityDescriptorGroup      (   &sd,    g_psidWorldSid, TRUE);

    ZeroMemory  (   &sa,    sizeof  (   SECURITY_ATTRIBUTES));

    sa.nLength              =   sizeof  (   SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor =   &sd;
    sa.bInheritHandle       =   FALSE;

    //  set this SD on the mutex..
    hMutex  =   CreateMutex (   &sa,
                                FALSE,
                                MUTEX_NAME
                            );


Feel free to ask if you need more information!

(BTW: This snippet is taken from 'production' code :o)
0
 

Author Comment

by:carlpaddick
ID: 6317363
jkr thanks for your reply and your code.  Hmm, think I might need some help with this one!  I have no idea how NT4 security works behind the scenes.

Basically I want to grant access to everyone so that any executable can use the dll.  So do I have to assign a null DACL in the security setting?  If so, how do I do this?  If not, could you help me grant this access please?  Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 6317411
>>Basically I want to grant access to everyone so that any
>>executable can use the dll.  So do I have to
>>assign a null DACL in the security setting?

A NULL DACL would also be an option, but a 'WORLD' grop SID serves the same purpose.

>>I have no idea how NT4 security works behind the scenes.

I recommend http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_seccpp.asp ('Windows NT Security in Theory and Practice') and the articles linked from this one as a lecture :o)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:carlpaddick
ID: 6317668
I've just added your code from above jkr, but the problem in the dll still remains.  My hMutex HANDLE value is still null after the CreateMutex call is made.  I really don't understand what is happening here.  I'm using the same login account/password and session to stop and start the service, and run the stand alone executable.  All the service .exe does at startup is instantiate an instance of the dll for later use, but it doesn't call any of it's methods.  Running the standalone .exe from either the command line or within Developer Studio produces the same problem in the DLL.  Any ideas?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6317944
>>My hMutex HANDLE
>>value is still null after the CreateMutex call is made

Aaah, I think I got the idea what might be wrong - there's another reason why that call might fail:

    hMutex  =   CreateMutex (   &sa,
                                FALSE,
                                MUTEX_NAME
                            );

    if  (   INVALID_HANDLE_VALUE    ==  hMutex)
        {
            if  (   ERROR_ALREADY_EXISTS    ==  GetLastError    ())
                {
                    hMutex  =   OpenMutex   (   MUTEX_ALL_ACCESS,
                                                FALSE,
                                                MUTEX_NAME
                                            );

                    if  (   INVALID_HANDLE_VALUE    ==  g_hDbgFileLock)
                        {
                            //  error
                        }
                }
        }
0
 

Author Comment

by:carlpaddick
ID: 6318126
Just added your testing code above jkr.  GetLastError() returns 5 - "access denied" so it doesn't drop into the OpenMutex test.

I don't know if this would have any effect or not but in the Services utility, the executable file running as a service is started using the following startup parameters:

Log on as:

System Account (selected)
Allow service to interact with desktop (checked)
0
 

Author Comment

by:carlpaddick
ID: 6318139
jkr, just leaving work for the day...please keep me posted with any news and I'll speak to you tomorrow.
0
 

Author Comment

by:carlpaddick
ID: 6321521
Hi jkr,

Just going back to your most recent comment posted, I think you might have been thinking that the executable running as a service, might have called the Interface method within the dll that makes the call to CreateMutex when it initially starts up.

This however isn't the case.  In fact it never makes a call on this interface in its entire running lifetime.  It's only the separate excutable (which is run from the command line) that makes this call.

Hope this may help.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6321688
>>I think you might have been thinking that the executable
>>running as a service, might have called the Interface
>>method within the dll that makes the call to CreateMutex
>>when it initially starts up.

Yes, that's what I actually thought - the code shown above is used by a DLL that is called by either a service or a user mode app, and the mutex' purpose actually is to guard access to a log file...
0
 

Author Comment

by:carlpaddick
ID: 6321932
So I wonder why I'm still getting error code 5 on the CreateMutex call?  The same user (me logged in as a user who has administrator rights) starts the service .exe and runs the command line .exe.

Can you think of anything else that the service or Windows NT might be doing to this dll file to stop me using it?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6322783
>>Can you think of anything else that the service or
>>Windows NT might be doing to this dll file to stop
>>me using it?

Actually, no. This code should work fine, regardless whether it's called within a DLL or an executable...
0
 

Author Comment

by:carlpaddick
ID: 6337586
jkr - hi,

I'm going to accept your original proposed answer, as I'm sure it works in most cases.  However for mine this hasn't been the case, and I still have been getting the error code 5 on the CreateMutex call.

My only workaround has been to take out the interface method and put it into its own normal Win32 dll.  This of course now works fine, and cures the problem.

Thanks anyway for you help,

Carl
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 71
ORA-01403: no data found 43 111
ffmpeg - "rtsp://...... Operation not permitted" 4 89
ShiftLeft challenge 21 88
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 …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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