Re: Sharing a semaphore between users
"Phil" <pbruyant@yahoo.com> wrote in message
news:040638eb-bebf-4bcf-b4e0-e533ce9ec7ae@k13g2000hse.googlegroups.com...
SECURITY_ATTRIBUTES SecAtt;
SECURITY_DESCRIPTOR SecDesc;
SecAtt.bInheritHandle=FALSE;
InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION);
SecAtt.lpSecurityDescriptor=&SecDesc;
// This should grant read/write/execute accesses to authenticated
users
ConvertStringSecurityDescriptorToSecurityDescriptor(
TEXT("(A;OICI;GRGWGX;;;AU)"),
SDDL_REVISION_1,
&(SecAtt.lpSecurityDescriptor),
NULL);
SecAtt.nLength=sizeof(SECURITY_DESCRIPTOR);
if ((g_hSem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,"Global\
\MyApp"))==NULL)
{
g_hSem = CreateSemaphore(&SecAtt,3,3,"Global\\MyApp");
}
If the error is still related to security, I'm not sure the
SECURITY_ATTRIBUTES is correct. I'm no expert at this at all, but this code
I have used to create a mutex (not semaphore) that could be accessed when
fast-user switching was invoked:
PSID pEveryoneSID = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
SECURITY_ATTRIBUTES sa;
// Create a well-known SID for the Everyone group.
if(! AllocateAndInitializeSid( &SIDAuthWorld, 1,
SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0,
&pEveryoneSID) )
{
return FALSE;
}
EXPLICIT_ACCESS ea;
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance= NO_INHERITANCE;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea.Trustee.ptstrName = (LPTSTR) pEveryoneSID;
// Create a new ACL that contains the new ACE.
PACL pACL = NULL;
dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
if (ERROR_SUCCESS != dwRes)
{
goto Cleanup;
}
// Initialize a security descriptor.
pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);
if (pSD == NULL)
{
goto Cleanup;
}
if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
{
goto Cleanup;
}
// Add the ACL to the security descriptor.
if (!SetSecurityDescriptorDacl(pSD,
TRUE, // fDaclPresent flag
pACL,
FALSE)) // not a default DACL
{
goto Cleanup;
}
// Initialize a security attributes structure.
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;
// Create your semaphore using 'sa'
cleanup:
...
Hope this helps,
David