How are you?I want to share memory between application and driver by
creating a section in application and opening the section in driver.
It is so:
The first,Application creates a section:
if ((hDevice = CreateFile(“\\.\SECTION”, //driver name
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
)) == INVALID_HANDLE_VALUE)
{
printf (“Can’t get a handle to the Section driver\n”);
return 0;
}
hmapping = CreateFileMapping( (HANDLE)0xFFFFFFFF,
NULL,
PAGE_READWRITE | SEC_COMMIT,
0,
length,
sectionName //the application creates
the section name
);
if (hmapping == INVALID_HANDLE_VALUE)
{
fprintf( stderr, “Error %u: Unable to map
file.\n”,GetLastError());
return 1;
}
fileData = MapViewOfFile( hmapping,
FILE_MAP_READ | FILE_MAP_WRITE,
0,
0,
0
);
//
// A null value indicates the map operation failed.
//
if (fileData == NULL)
{
CloseHandle(hmapping );
CloseHandle(hDevice);
return 1;
}
memset(fileData, 0, length);
strcpy(fileData, “Hello! how are you?”);
if (DeviceIoControl (hDevice,
(DWORD)IOCTL_SECTION_MAP_MEMORY,
sectionName,
strlen(sectionName),
NULL,
0,
&cbReturned,
0
) )
driver open the section created by application:
InitializeObjectAttributes (&objectAttributes,
sectionNameUnicodeString,
OBJ_CASE_INSENSITIVE,
(HANDLE) NULL,
(PSECURITY_DESCRIPTOR) NULL);
status = ZwOpenSection (§ionHandle,
SECTION_ALL_ACCESS,
&objectAttributes);
viewsize = 0;
//
// Map the section
//
status = ZwMapViewOfSection (sectionHandle,
(HANDLE) -1,
&virtualAddress,
0L, // zero bits
PAGE_SIZE, // commit size
NULL, // section offset
&viewsize,
ViewShare,
0,
PAGE_READWRITE);
deviceExtension->ProcessId = PsGetCurrentProcessId();
deviceExtension->VirtualAddress = virtualAddress;
DbgPrint(“App says: %s\n”, virtualAddress);
Above way can not share memory,Why?