473,554 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ATTRIB UTES SecAttr;
static SECURITY_DESCRI PTOR SecDesc;

Mutex *Utility::Creat eOpenAccessMute x(BOOL InitialyOwn, String *Name, BOOL
*CreatedNew)
{
Mutex *Ret = __gc new Mutex();

HANDLE OldHandle = (HANDLE) Ret->Handle.ToInt32 ();

CloseHandle(Old Handle);

// Security settings must be explicitly set to allow full access to the
mutex.
InitializeSecur ityDescriptor(& SecDesc,SECURIT Y_DESCRIPTOR_RE VISION);
SetSecurityDesc riptorDacl(&Sec Desc,TRUE,(PACL ) NULL,FALSE);

Ret->Handle = CreateMutex(&Se cAttr,InitialyO wn, CString(Name));

if (GetLastError() == ERROR_ALREADY_E XISTS)
{
*CreatedNew = TRUE;
}
else
{
*CreatedNew = FALSE;
}

return Ret;
}
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 17 '05 #1
7 1805
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.Threadin g.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.nos pam>
wrote in message news:ej******** *****@TK2MSFTNG P15.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.Threadin g.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******** ******@TK2MSFTN GP12.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_ATTRIB UTES SecAttr;
static SECURITY_DESCRI PTOR SecDesc;

Mutex *Utility::Creat eOpenAccessMute x(BOOL InitialyOwn, String *Name, BOOL
*CreatedNew)
{
Mutex *Ret = __gc new Mutex();

HANDLE OldHandle = (HANDLE) Ret->Handle.ToInt32 ();

CloseHandle(Old Handle);

// Security settings must be explicitly set to allow full access to the
mutex.
InitializeSecur ityDescriptor(& SecDesc,SECURIT Y_DESCRIPTOR_RE VISION);
SetSecurityDesc riptorDacl(&Sec Desc,TRUE,(PACL ) NULL,FALSE);

Ret->Handle = CreateMutex(&Se cAttr,InitialyO wn, CString(Name));

if (GetLastError() == ERROR_ALREADY_E XISTS)
{
*CreatedNew = TRUE;
}
else
{
*CreatedNew = FALSE;
}

return Ret;
}
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


IMO This:
Ret->Handle = CreateMutex(&Se cAttr,InitialyO wn, 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(CreateMu tex(&SecAttr,In itialyOwn, ....
Willy.
Nov 17 '05 #5

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message news:%
IMO This:
Ret->Handle = CreateMutex(&Se cAttr,InitialyO wn, 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(CreateMu tex(&SecAttr,In itialyOwn, ....
Willy.


Forget about this, there is an op_explicit(voi d*) 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.nos pam>
wrote in message news:%2******** ********@TK2MSF TNGP09.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
3764
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 under the administrator logon, while the asp.net application runs under the standard asp.net user account. The problem relates to permissions on creation of the mutex. If the asp.net application...
1
2233
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 m_Mutex; << in c# I assume that when the methods are static, so are the private members public static void Run(Form mainForm) { if(IsFirstInstance()) {
4
329
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 still allows two instances. using System; using System.Threading; namespace MutexTest {
2
18750
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 duration the action they performed must persist before any other threads can be allowed to perform some action. My object exposes a Mutex which is locked via a IDisposable-Sentry class. I want to...
20
6459
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 4.12 in C# Programmer's Cookbook), I found that if the mutex is in a local variable in Main(), and my program launches any windows before Application.Run(), it will lose the mutex upon doing so. It...
1
442
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 mutex using the C# mutex class. I am logging into Windows XP as "User1" and creating and locking the mutex from "User1" using the 1st form. I then use the "switch users" function to log in as "User2"...
16
3137
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 MainForm class. In a debug build, first instance got ownership, second did not and terminated. In a release build, the second instance *also* got ownership. I "fixed" it by making the mutex a static...
2
2051
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 = null; const string name = "xyz"; try
0
7600
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7889
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3560
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2020
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
841
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.