Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NamedMutex with SECURITY_ATTRIBUTES

196 views
Skip to first unread message

Michael

unread,
Apr 14, 2004, 10:20:21 AM4/14/04
to
I created a unmanaged memory map file classes in C# and
everything works fine until I use it in my service, which
got Access Denied, which I figured it would because it
didn't have any SECURITY_ATTRIBUTES. I couldn't find a
way to add a SECURITY_ATTRIBUTES to the Mutex class
in .NET so I made my own. The trouble i'm having is to
cast SECURITY_DESCRIPTOR to a IntPtr. Here is the code
i'm talking about. it's kind of long.

sealed public class NamedMutex : WaitHandle
{
public NamedMutex(bool bInitialOwner, string strName)
{
InternalCreateMutex(bInitialOwner, strName);
}
private void InternalCreateMutex(bool bInitialOwner,
string strName)
{
try
{
lock(padlock)
{
SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES
();
//This is a static member of another class
SecurityDescriptor.GetNullDaclSecurityDescriptor
(out sd);
//this is the line where I can't
//cast the SECURITY_DESCRIPTOR to
//the IntPtr. Maybe lpSecurityDescriptor
//should be a different type?
sa.lpSecurityDescriptor = (IntPtr)sd;
sa.bInheritHandle = false;
sa.iLength = Marshal.SizeOf(sa);

Handle = CreateMutexW(ref sd, bInitialOwner,
strName);
if(Handle == InvalidHandle)
{
throw new Win32Exception
(Marshal.GetLastWin32Error());
}
}
}
catch{throw;}
}
public bool ReleaseMutex()
{
return InternalReleaseMutex();
}
private bool InternalReleaseMutex()
{
return ReleaseMutex(Handle);
}

[DllImport("Kernel32.dll", SetLastError=true,
CharSet=CharSet.Unicode)]
private static extern IntPtr CreateMutexW(ref
SECURITY_DESCRIPTOR sd, bool bInitialOwner, string
strName);

[DllImport("Kernel32.dll", SetLastError=true)]
private static extern bool ReleaseMutex(IntPtr hMutex);

private object padlock = new object();
private string m_strMutexName = null;
}

[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_DESCRIPTOR
{
public byte Revision;
public byte Sbz1;
public ushort Control;
public uint Owner;
public uint Group;
public uint Sacl;
public uint Dacl;
}

[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_ATTRIBUTES
{
public int iLength;
//I am trying to cast a
// SECURITY_DESCRIPTOR into this
//which is not working
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}

I am hoping someone can help me with this so I can move
onto new stuff, i've been working on trying to do this
for the last day and a half.
Michael

anon...@discussions.microsoft.com

unread,
Apr 14, 2004, 11:56:27 AM4/14/04
to
Just an update, I found Marshal.StructureToPtr but still
having trouble getting it to work. Here is what i have

SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();

SecurityDescriptor.GetNullDaclSecurityDescriptor(out sd);
sa.lpSecurityDescriptor = Marshal.AllocHGloba
(Marshal.SizeOf(sd));
Marshal.StructureToPtr(sd, sa.lpSecurityDescriptor,
false);

a.bInheritHandle = false;
sa.iLength = Marshal.SizeOf(sa);
Handle = CreateMutexW(ref sd, bInitialOwner,

strMutexName);

I get a handle of 0 back and calling
Marshal.GetLastWin32Error() I get error code 5(Access
Denied), so I am still not sure what i'm doing wrong?
Michael

>.
>

anon...@discussions.microsoft.com

unread,
Apr 14, 2004, 12:51:18 PM4/14/04
to
I also don't think I made this clear the first time. My
service is able to create the mutex but my user app gets
the Access Denied when it tries to create a mutex with
the same name.

>.
>

Willy Denoyette [MVP]

unread,
Apr 14, 2004, 4:51:40 PM4/14/04
to
Could you please post the code in GetNullDaclSecurityDescriptor.
It's crucial to see how you create a NULL DACL.

Willy.


<anon...@discussions.microsoft.com> wrote in message
news:19b5b01c42240$b4d229e0$a601...@phx.gbl...

Michael

unread,
Apr 15, 2004, 1:41:22 PM4/15/04
to
Holy crap! I CAN'T BELIEVE I DID NOT CATCH THAT!!!!!!!!! I knew I was supposed to pass SECURITY_ATT into the function and not SECURITY_DESC. I guess I was just getting so frustrated that I overlooked it. I still can't believe I’ve been looking over that piece of code for the last day and never once caught that myself!!! I also can't believe it didn't throw an exception either(it was just giving me Access Denied when calling from the user app.). The other weird thing I thought was when calling CreateMutexW from the user app; it was giving me a handle of 0 rather than -1(Invalid handle). I noticed the 0 and called getlasterror and noticed it was giving me Access Denied. Thank you for helping me and noticing that small detail that I overlooked a million times! I will also check out the links on security, because you are right, it should be more secure. Also, just to let you know how slack some people are on security, I was on IRC talking about it, and one person said that I should just have the service log in as administrator to work around it, now that is kind of slack. Anyway, thank you once again.
Michael

0 new messages