• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2706
  • Last Modified:

CreateMutex problem

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
carlpaddick
Asked:
carlpaddick
  • 7
  • 5
1 Solution
 
jkrCommented:
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
 
carlpaddickAuthor Commented:
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
 
jkrCommented:
>>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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
carlpaddickAuthor Commented:
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
 
jkrCommented:
>>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
 
carlpaddickAuthor Commented:
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
 
carlpaddickAuthor Commented:
jkr, just leaving work for the day...please keep me posted with any news and I'll speak to you tomorrow.
0
 
carlpaddickAuthor Commented:
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
 
jkrCommented:
>>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
 
carlpaddickAuthor Commented:
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
 
jkrCommented:
>>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
 
carlpaddickAuthor Commented:
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now