all groups > dotnet interop > april 2004 >
dotnet interop :
NamedMutex with SECURITY_ATTRIBUTES
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
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 [quoted text, click to view] >-----Original Message----- >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 > >.
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. [quoted text, click to view] >-----Original Message----- >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 > >.
Here is all the code for both classes and the structures. I hope you can help because i'm getting really frustrated at this Michae sealed public class NamedMutex : WaitHandl public NamedMutex(bool bInitialOwner, string strName InternalCreateMutex(bInitialOwner, strName) public string MutexNam get{return m_strMutexName; private string InternalMutexNam get{return m_strMutexName; se if(value == null throw new ArgumentNullException("strName", "Parameter strName cannot be null") m_strMutexName = value private void InternalCreateMutex(bool bInitialOwner, string strName tr lock(padlock InternalMutexName = strName SECURITY_DESC sd SECURITY_ATT sa = new SECURITY_ATT() SecurityDescriptor.GetNullDaclSecurityDescriptor(out sd) sa.lpSecurityDescriptor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SECURITY_DESC))) Marshal.StructureToPtr(sd, sa.lpSecurityDescriptor, false); sa.bInheritHandle = false sa.nLength = Marshal.SizeOf(typeof(SECURITY_ATT)) Handle = CreateMutexW(ref sd, bInitialOwner, InternalMutexName) 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_DESC 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 internal class SecurityDescripto internal static void GetNullDaclSecurityDescriptor(out SECURITY_DESC sd if(InitializeSecurityDescriptor(out sd, 1) { if(!SetSecurityDescriptorDacl(ref sd, true, IntPtr.Zero, false) throw new Win32Exception(Marshal.GetLastWin32Error()) els throw new Win32Exception(Marshal.GetLastWin32Error()) [DllImport("Advapi32.dll", SetLastError=true)] private static extern bool SetSecurityDescriptorDacl(ref SECURITY_DESC sd, bool bDaclPresent, IntPtr Dacl, bool bDaclDefaulted) [DllImport("Advapi32.dll", SetLastError=true) private static extern bool InitializeSecurityDescriptor(out SECURITY_DESC sd, int dwRevision); [StructLayout(LayoutKind.Sequential) internal struct SECURITY_DESC private byte Revision private byte Sbz1 private ushort Control private IntPtr Owner private IntPtr Group private IntPtr Sacl private IntPtr Dacl [StructLayout(LayoutKind.Sequential) internal struct SECURITY_AT public int nLength; public IntPtr lpSecurityDescriptor; public bool bInheritHandle
Could you please post the code in GetNullDaclSecurityDescriptor. It's crucial to see how you create a NULL DACL. Willy. [quoted text, click to view] <anonymous@discussions.microsoft.com> wrote in message news:19b5b01c42240$b4d229e0$a601280a@phx.gbl... >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. > > >>-----Original Message----- >>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 >> >>. >>
Don't see what you're looking for? Try a search.
|
|
|