Solved

CreateMutex problem

Posted on 2001-07-25
12
2,533 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
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 177
bigDiff challenge 17 106
modThree challenge 4 90
how to split multiple lines delimiter : 8 99
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…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

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