NamedMutex with SECURITY_ATTRIBUTES

anonymous_at_discussions.microsoft.com
Date: 04/14/04


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
>
>.
>



Relevant Pages

  • NamedMutex with SECURITY_ATTRIBUTES
    ... > public NamedMutex(bool bInitialOwner, string strName) ... > private bool InternalReleaseMutex() ... > private static extern bool ReleaseMutex(IntPtr ... > public uint Owner; ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Text Prediction how to turn off
    ... If the call wants a BOOL, ... private static extern int SHSetInputContext(IntPtr hwnd, ... prediction is displayed even with the standard keyboard SIP. ... You can Platform Invoke this API with the following declarations: ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Single Instance Form
    ... Why not use a singleton pattern, its specifically for single instance only ... probably slower than a Mutex but restoring the minimized form is nice. ... private static extern bool ShowWindowAsync (IntPtr hWnd, ...
    (microsoft.public.dotnet.languages.csharp)
  • Single Instance Form
    ... slower than a Mutex but restoring the minimized form is nice. ... private static extern bool ShowWindowAsync (IntPtr hWnd, ... private const int WS_SHOWNORMAL = 1; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: using UpdateResource() with PInvoke
    ... Win32 Resource: ... private static extern IntPtr BeginUpdateResource(String pFileName, ... private static extern int EndUpdateResource(IntPtr hUpdate, bool fDiscard); ... //UpperCase String. ...
    (microsoft.public.dotnet.languages.csharp)

Loading