Hi,
I’m just trying to test the event example provided in OSR article (http://www.osronline.com/article.cfm?id=108) in Windows 7. The event signaling is successful in windows xp but failing in windows 7.
I learnt from other osr thread that, its because of security, we need to add security descriptor while creating event, but that also failed. The following is the code snippet of application.
#include <windows.h>
#include <winioctl.h>
#include “…\inc\eventioctl.h”
#include <stdio.h>
#include <aclapi.h>
int main(void)
{
DWORD bytesReturned;
DWORD WaitStatus;
HANDLE DeviceDriver;
HANDLE SharedEvent;
// Security Descriptor
DWORD dwRes;
PSID pEveryoneSID = NULL, pAdminSID = NULL;
PACL pACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea[2];
SID_IDENTIFIER_AUTHORITY SIDAuthWorld =
SECURITY_WORLD_SID_AUTHORITY;
SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
SECURITY_ATTRIBUTES sa;
HKEY hkSub = NULL;
// Create a well-known SID for the Everyone group.
if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0,
&pEveryoneSID))
{
printf(“AllocateAndInitializeSid Error %u\n”, GetLastError());
goto Cleanup;
}
// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow Everyone read access to the key.
ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
ea[0].grfAccessPermissions = KEY_READ;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance= NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;
// Create a SID for the BUILTIN\Administrators group.
if(! AllocateAndInitializeSid(&SIDAuthNT, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdminSID))
{
printf(“AllocateAndInitializeSid Error %u\n”, GetLastError());
goto Cleanup;
}
// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow the Administrators group full access to
// the key.
ea[1].grfAccessPermissions = KEY_ALL_ACCESS;
ea[1].grfAccessMode = SET_ACCESS;
ea[1].grfInheritance= NO_INHERITANCE;
ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
ea[1].Trustee.ptstrName = (LPTSTR) pAdminSID;
// Create a new ACL that contains the new ACEs.
dwRes = SetEntriesInAcl(2, ea, NULL, &pACL);
if (ERROR_SUCCESS != dwRes)
{
printf(“SetEntriesInAcl Error %u\n”, GetLastError());
goto Cleanup;
}
// Initialize a security descriptor.
pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);
if (NULL == pSD)
{
printf(“LocalAlloc Error %u\n”, GetLastError());
goto Cleanup;
}
if (!InitializeSecurityDescriptor(pSD,
SECURITY_DESCRIPTOR_REVISION))
{
printf(“InitializeSecurityDescriptor Error %u\n”,
GetLastError());
goto Cleanup;
}
// Add the ACL to the security descriptor.
if (!SetSecurityDescriptorDacl(pSD,
TRUE, // bDaclPresent flag
pACL,
FALSE)) // not a default DACL
{
printf(“SetSecurityDescriptorDacl Error %u\n”,
GetLastError());
goto Cleanup;
}
// Initialize a security attributes structure.
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;
// SD End
//
// Open up a handle to the device driver
//
DeviceDriver = CreateFile(
“\\.\EventShare”, // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if (DeviceDriver == INVALID_HANDLE_VALUE) {
printf(“Unable to open handle to the device driver\n”);
return 1;
}
//
// Create named event that will be shared between the driver and
// application. Note that this event is actually created under
// \BaseNamedObjects\ by the object manager so we will need to add
// that to the event name when we try and open this event from kernel mode
//
SharedEvent = CreateEvent(&sa, TRUE, FALSE, “SharedEvent”);
if (SharedEvent == NULL) {
printf(“Cannot create named event!\n”);
return 1;
}
//
// Send an IOCTL to the driver to signal that the named event
// has been created and that it can now open a handle to it
//
if (!DeviceIoControl(DeviceDriver,
IOCTL_OPEN_EVENT,
NULL, 0,
NULL, 0,
&bytesReturned,
NULL)) {
printf(“The driver failed to open the named event!\n”);
return 1;
}
//
// Now we can just sit back and wait for the driver to signal our event.
// It should really happen well before 5 seconds…
//
WaitStatus = WaitForSingleObject(SharedEvent, 5000);
if (WaitStatus != WAIT_OBJECT_0) {
printf(“Driver failed to signal event! WaitForSingleObject returned 0x%8.8x\n”,
WaitStatus);
} else {
//
// Voila! We’ve successfully awoken a user mode application by signalling
// an event in kernel mode!
//
printf(“The driver has successfully signaled our unnamed event!\n”);
}
//
// Clean up any and all resources allocated
//
CloseHandle(SharedEvent);
CloseHandle(DeviceDriver);
Cleanup:
if (pEveryoneSID)
FreeSid(pEveryoneSID);
if (pAdminSID)
FreeSid(pAdminSID);
if (pACL)
LocalFree(pACL);
if (pSD)
LocalFree(pSD);
return 0;
}
Any help will be appreciated…</aclapi.h></stdio.h></winioctl.h></windows.h>