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
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
>.
>
>.
>
Willy.
<anon...@discussions.microsoft.com> wrote in message
news:19b5b01c42240$b4d229e0$a601...@phx.gbl...