K
Ken Varn
I am working in managed C++. I have a Mutex object in which I need to
replace the Handle property with a new handle. The new handle is being
constructed using Win32 CreateMutex call. I need to call the Win32 version
in order to set the security descriptor for the mutex, which is not natively
supported in .NET Framework 1.1.
I always get a little nervous about resource leaks when trying to bridge
Win32 with .NET, so I want to make sure there is not the possibility of
calamity here.
The following code is a snippet of one of the managed class methods that I
am using to return a Mutex object with the replaced handle. Will the new
handle be released by the finalizer of the Mutex object, or do I have to do
this manually?
Also, I am not even sure if the code below will work. Can someone verify
please?
static SECURITY_ATTRIBUTES SecAttr;
static SECURITY_DESCRIPTOR SecDesc;
Mutex *Utility::CreateOpenAccessMutex(BOOL InitialyOwn, String *Name, BOOL
*CreatedNew)
{
Mutex *Ret = __gc new Mutex();
HANDLE OldHandle = (HANDLE) Ret->Handle.ToInt32();
CloseHandle(OldHandle);
// Security settings must be explicitly set to allow full access to the
mutex.
InitializeSecurityDescriptor(&SecDesc,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&SecDesc,TRUE,(PACL) NULL,FALSE);
Ret->Handle = CreateMutex(&SecAttr,InitialyOwn, CString(Name));
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
*CreatedNew = TRUE;
}
else
{
*CreatedNew = FALSE;
}
return Ret;
}
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
-----------------------------------
replace the Handle property with a new handle. The new handle is being
constructed using Win32 CreateMutex call. I need to call the Win32 version
in order to set the security descriptor for the mutex, which is not natively
supported in .NET Framework 1.1.
I always get a little nervous about resource leaks when trying to bridge
Win32 with .NET, so I want to make sure there is not the possibility of
calamity here.
The following code is a snippet of one of the managed class methods that I
am using to return a Mutex object with the replaced handle. Will the new
handle be released by the finalizer of the Mutex object, or do I have to do
this manually?
Also, I am not even sure if the code below will work. Can someone verify
please?
static SECURITY_ATTRIBUTES SecAttr;
static SECURITY_DESCRIPTOR SecDesc;
Mutex *Utility::CreateOpenAccessMutex(BOOL InitialyOwn, String *Name, BOOL
*CreatedNew)
{
Mutex *Ret = __gc new Mutex();
HANDLE OldHandle = (HANDLE) Ret->Handle.ToInt32();
CloseHandle(OldHandle);
// Security settings must be explicitly set to allow full access to the
mutex.
InitializeSecurityDescriptor(&SecDesc,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&SecDesc,TRUE,(PACL) NULL,FALSE);
Ret->Handle = CreateMutex(&SecAttr,InitialyOwn, CString(Name));
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
*CreatedNew = TRUE;
}
else
{
*CreatedNew = FALSE;
}
return Ret;
}
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
-----------------------------------