472,328 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Need to replace Mutex Handle. Have questions.

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_DES CRIPTOR_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
-----------------------------------
Nov 17 '05 #1
7 1724
Ken Varn wrote:
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?


I didn't try it, but looking at the .NET 1.1 source code, it looks like it
ought to be fine. The Handle property of System.Threading.WaitHandle gives
full transparent read/write access to the underlying handle, so when you
assign to the Handle property you're really changing the underlying handle.

You do need to close the old handle value, which you're doing, so I'd say
you're A-OK.

-cd

Nov 17 '05 #2
Well, I tried this out, but something doesn't seem right.

When I assign the Mutex->Handle value to a Win32 HANDLE type, the value of
the Win32 handle is always <undefined> when I look at it in the debugger. I
have even tried type casting it to an INT *, and it comes out as 0. Yet,
when I inspect the Mutex->Handle value, it is a positive number. I don't
understand what is going on here with the IntPtr. Also, when I try to
assign a new value the Matrix->Handle, and I inspect it in the debugger, it
still shows the old value.

I may be getting confused by the behavior of IntPtr and how the debugger
shows the output. Could you please clarify this behavior of IntPtr?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:ej*************@TK2MSFTNGP15.phx.gbl...
Ken Varn wrote:
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?
I didn't try it, but looking at the .NET 1.1 source code, it looks like it
ought to be fine. The Handle property of System.Threading.WaitHandle

gives full transparent read/write access to the underlying handle, so when you
assign to the Handle property you're really changing the underlying handle.
You do need to close the old handle value, which you're doing, so I'd say
you're A-OK.

-cd

Nov 17 '05 #3
Ken Varn wrote:
Well, I tried this out, but something doesn't seem right.

When I assign the Mutex->Handle value to a Win32 HANDLE type, the
value of the Win32 handle is always <undefined> when I look at it in
the debugger. I have even tried type casting it to an INT *, and it
comes out as 0. Yet, when I inspect the Mutex->Handle value, it is a
positive number. I don't understand what is going on here with the
IntPtr. Also, when I try to assign a new value the Matrix->Handle,
and I inspect it in the debugger, it still shows the old value.

I may be getting confused by the behavior of IntPtr and how the
debugger shows the output. Could you please clarify this behavior of
IntPtr?


There's no light I can shed on the subject really - it sounds like a
debugger bug to me. Perhaps someone else knows.

Other than the oddness in the debugger, does the mutex appear to behave as
you expect?

-cd
Nov 17 '05 #4

"Ken Varn" <nospam> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
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_DES CRIPTOR_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
-----------------------------------


IMO This:
Ret->Handle = CreateMutex(&SecAttr,InitialyOwn, CString(Name));
is invalid, you assign a void* to an IntPtr, you have to initialize a new
IntPtr using the void* value returned by CreateMutex like this....

Ret->Handle = IntPtr(CreateMutex(&SecAttr,InitialyOwn, ....
Willy.
Nov 17 '05 #5

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:%
IMO This:
Ret->Handle = CreateMutex(&SecAttr,InitialyOwn, CString(Name));
is invalid, you assign a void* to an IntPtr, you have to initialize a new
IntPtr using the void* value returned by CreateMutex like this....

Ret->Handle = IntPtr(CreateMutex(&SecAttr,InitialyOwn, ....
Willy.


Forget about this, there is an op_explicit(void*) for the IntPtr, so a
simple assignment should work.

Willy.
Nov 17 '05 #6
Well, I did a ToString() call on the Handle and it appears to have been
changed properly. The problem must be with the debugger output when
inspecting pointers. It seems to only want to show the value that the
pointer points to rather than the actual value of the pointer itself. Maybe
this is because of it running in the managed mode.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Ken Varn wrote:
Well, I tried this out, but something doesn't seem right.

When I assign the Mutex->Handle value to a Win32 HANDLE type, the
value of the Win32 handle is always <undefined> when I look at it in
the debugger. I have even tried type casting it to an INT *, and it
comes out as 0. Yet, when I inspect the Mutex->Handle value, it is a
positive number. I don't understand what is going on here with the
IntPtr. Also, when I try to assign a new value the Matrix->Handle,
and I inspect it in the debugger, it still shows the old value.

I may be getting confused by the behavior of IntPtr and how the
debugger shows the output. Could you please clarify this behavior of
IntPtr?


There's no light I can shed on the subject really - it sounds like a
debugger bug to me. Perhaps someone else knows.

Other than the oddness in the debugger, does the mutex appear to behave as
you expect?

-cd

Nov 17 '05 #7
Ken Varn wrote:
Well, I did a ToString() call on the Handle and it appears to have
been changed properly. The problem must be with the debugger output
when inspecting pointers. It seems to only want to show the value
that the pointer points to rather than the actual value of the
pointer itself. Maybe this is because of it running in the managed
mode.


That's probably reasonable. Despite the fact that Handle is defined as
IntPtr, and native HANDLE is defined as void*, it's really just an integer
index into a table - but there's really no way for the debugger to know
that.

-cd
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken Varn | last post by:
I have a named mutex object that is accessed by both an asp.net application and a Windows executable .net application. The Windows executable runs...
1
by: Rocky | last post by:
I am using the following piece of code to ensure that my application only runs once, however I have a few questions about it. static Mutex...
4
by: PL | last post by:
I simply cannot get this to work with my current project, if I create a test project with only the code below it works fine but in my real app it...
2
by: Ken Durden | last post by:
I'm setting up an interface where clients must perform external locking before calling certain commands. I do this to force them to specify the...
20
by: Michael A. Covington | last post by:
See: http://www.ai.uga.edu/mc/SingleInstance.html While attempting to use a mutex to allow only one instance of my app to run at a time (Recipe...
1
by: Kris | last post by:
I want to set a mutex in one windows account and allow another windows account to access this mutex. For testing I have two forms that create a...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my...
2
by: Jeroen | last post by:
Hi all, I've been trying to get my head around threading. Here's an example from the book I'm reading: /***************************/ Mutex m...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.