NamedMutex with SECURITY_ATTRIBUTES
anonymous_at_discussions.microsoft.com
Date: 04/14/04
- Next message: Aaron Prohaska: "Excel automation problem."
- Previous message: Paul Clement: "Re: Equivalent of App.path"
- In reply to: Michael: "NamedMutex with SECURITY_ATTRIBUTES"
- Next in thread: Willy Denoyette [MVP]: "Re: NamedMutex with SECURITY_ATTRIBUTES"
- Reply: Willy Denoyette [MVP]: "Re: NamedMutex with SECURITY_ATTRIBUTES"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 14 Apr 2004 09:51:18 -0700
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
>
>.
>
- Next message: Aaron Prohaska: "Excel automation problem."
- Previous message: Paul Clement: "Re: Equivalent of App.path"
- In reply to: Michael: "NamedMutex with SECURITY_ATTRIBUTES"
- Next in thread: Willy Denoyette [MVP]: "Re: NamedMutex with SECURITY_ATTRIBUTES"
- Reply: Willy Denoyette [MVP]: "Re: NamedMutex with SECURITY_ATTRIBUTES"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|