Mutex::OpenExisting Method (String)
Opens an existing named mutex.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
- Type: System::String
The name of a system-wide named mutex object.
| Exception | Condition |
|---|---|
| ArgumentException | name is a zero-length string. -or- name is longer than 260 characters. |
| ArgumentNullException | name is nullptr. |
| WaitHandleCannotBeOpenedException | The named mutex does not exist. |
| IOException | A Win32 error occurred. |
| UnauthorizedAccessException | The named mutex exists, but the user does not have the security access required to use it. |
The OpenExisting method attempts to open an existing named mutex. If the system mutex does not exist, this method throws an exception instead of creating the system object.
Two calls to this method with the same value for name do not necessarily return the same Mutex object, even though they represent the same named system mutex.
This method overload is equivalent to calling the OpenExisting(String, MutexRights) method overload and specifying MutexRights::Synchronize and MutexRights::Modify rights, combined using bitwise or.
MutexRights::Synchronize allows a thread to wait on the mutex, and MutexRights::Modify allows a thread to call the ReleaseMutex method.
The following code example demonstrates the cross-process behavior of a named mutex with access control security. The example uses the OpenExisting(String) method overload to test for the existence of a named mutex.
If the mutex does not exist, it is created with initial ownership and access control security that denies the current user the right to use the mutex, but grants the right to read and change permissions on the mutex.
If you run the compiled example from two command windows, the second copy will throw an access violation exception on the call to OpenExisting(String). The exception is caught, and the example uses the OpenExisting(String, MutexRights) method overload to open the mutex with the rights needed to read and change the permissions.
After the permissions are changed, the mutex is opened with the rights required to enter and release it. If you run the compiled example from a third command window, it runs using the new permissions.
using namespace System; using namespace System::Threading; using namespace System::Security::AccessControl; using namespace System::Security::Permissions; public ref class Example { public: [SecurityPermissionAttribute(SecurityAction::Demand,Flags=SecurityPermissionFlag::UnmanagedCode)] static void Main() { String^ mutexName = L"MutexExample4"; Mutex^ m = nullptr; bool doesNotExist = false; bool unauthorized = false; // The value of this variable is set by the mutex // constructor. It is true if the named system mutex was // created, and false if the named mutex already existed. // bool mutexWasCreated = false; // Attempt to open the named mutex. try { // Open the mutex with (MutexRights.Synchronize | // MutexRights.Modify), to enter and release the // named mutex. // m = Mutex::OpenExisting( mutexName ); } catch ( WaitHandleCannotBeOpenedException^ ) { Console::WriteLine( L"Mutex does not exist." ); doesNotExist = true; } catch ( UnauthorizedAccessException^ ex ) { Console::WriteLine( L"Unauthorized access: {0}", ex->Message ); unauthorized = true; } // There are three cases: (1) The mutex does not exist. // (2) The mutex exists, but the current user doesn't // have access. (3) The mutex exists and the user has // access. // if ( doesNotExist ) { // The mutex does not exist, so create it. // Create an access control list (ACL) that denies the // current user the right to enter or release the // mutex, but allows the right to read and change // security information for the mutex. // String^ user = String::Concat( Environment::UserDomainName, L"\\", Environment::UserName ); MutexSecurity^ mSec = gcnew MutexSecurity; MutexAccessRule^ rule = gcnew MutexAccessRule( user, static_cast<MutexRights>( MutexRights::Synchronize | MutexRights::Modify), AccessControlType::Deny ); mSec->AddAccessRule( rule ); rule = gcnew MutexAccessRule( user, static_cast<MutexRights>( MutexRights::ReadPermissions | MutexRights::ChangePermissions), AccessControlType::Allow ); mSec->AddAccessRule( rule ); // Create a Mutex object that represents the system // mutex named by the constant 'mutexName', with // initial ownership for this thread, and with the // specified security access. The Boolean value that // indicates creation of the underlying system object // is placed in mutexWasCreated. // m = gcnew Mutex( true,mutexName, mutexWasCreated,mSec ); // If the named system mutex was created, it can be // used by the current instance of this program, even // though the current user is denied access. The current // program owns the mutex. Otherwise, exit the program. // if ( mutexWasCreated ) { Console::WriteLine( L"Created the mutex." ); } else { Console::WriteLine( L"Unable to create the mutex." ); return; } } else if ( unauthorized ) { // Open the mutex to read and change the access control // security. The access control security defined above // allows the current user to do this. // try { m = Mutex::OpenExisting( mutexName, static_cast<MutexRights>( MutexRights::ReadPermissions | MutexRights::ChangePermissions) ); // Get the current ACL. This requires // MutexRights.ReadPermissions. MutexSecurity^ mSec = m->GetAccessControl(); String^ user = String::Concat( Environment::UserDomainName, L"\\", Environment::UserName ); // First, the rule that denied the current user // the right to enter and release the mutex must // be removed. MutexAccessRule^ rule = gcnew MutexAccessRule( user, static_cast<MutexRights>( MutexRights::Synchronize | MutexRights::Modify), AccessControlType::Deny ); mSec->RemoveAccessRule( rule ); // Now grant the user the correct rights. // rule = gcnew MutexAccessRule( user, static_cast<MutexRights>( MutexRights::Synchronize | MutexRights::Modify), AccessControlType::Allow ); mSec->AddAccessRule( rule ); // Update the ACL. This requires // MutexRights.ChangePermissions. m->SetAccessControl( mSec ); Console::WriteLine( L"Updated mutex security." ); // Open the mutex with (MutexRights.Synchronize // | MutexRights.Modify), the rights required to // enter and release the mutex. // m = Mutex::OpenExisting( mutexName ); } catch ( UnauthorizedAccessException^ ex ) { Console::WriteLine( L"Unable to change permissions: {0}", ex->Message ); return; } } // If this program created the mutex, it already owns // the mutex. // if ( !mutexWasCreated ) { // Enter the mutex, and hold it until the program // exits. // try { Console::WriteLine( L"Wait for the mutex." ); m->WaitOne(); Console::WriteLine( L"Entered the mutex." ); } catch ( UnauthorizedAccessException^ ex ) { Console::WriteLine( L"Unauthorized access: {0}", ex->Message ); } } Console::WriteLine( L"Press the Enter key to exit." ); Console::ReadLine(); m->ReleaseMutex(); } }; int main() { Example::Main(); }
- SecurityCriticalAttribute
Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.