Hi all,
First, I know share event between user and kernel is not safe.
But under some reason, I use this mechanism.
Following is my code:
In kernel mode is a scsi driver
main thread:
case myioctl :
{
VDShrEventHandle_test = *(HANDLE *)OsrSpGetSrbDataAddress(pIInfo->SPLocalHandle,PSrb);
__try {
status = ObReferenceObjectByHandle(VDShrEventHandle_test , GENERIC_ALL, *ExEventObjectType, KernelMode, (PVOID *)(&VDShrEvent), NULL);
if(status == STATUS_SUCCESS)
{
KeSetEvent(&pIInfo->testEvent,KPRIORITY(0),FALSE);
}
} __except(EXCEPTION_EXECUTE_HANDLER) {
status = GetExceptionCode();
}
status = STATUS_SUCCESS;
PSrb->SrbStatus = STATUS_SUCCESS;
}
another thread:
KeWaitForSingleObject( &pLocalInfo->testEvent, Executive, KernelMode, FALSE, NULL );
while(TRUE)
{
if (pLocalInfo->device_close==TRUE)
{
PsTerminateSystemThread(STATUS_SUCCESS);
}
KeWaitForSingleObject(VDShrEvent, Executive, KernelMode, FALSE, NULL );
KeClearEvent(VDShrEvent);
break;
}
KeWaitForSingleObject( &pLocalInfo->testEvent, Executive, KernelMode, FALSE, NULL );
while(TRUE)
{
if (pLocalInfo->device_close==TRUE)
{
PsTerminateSystemThread(STATUS_SUCCESS);
}
KeWaitForSingleObject(VDShrEvent, Executive, KernelMode, FALSE, NULL );
KeClearEvent(VDShrEvent);
KdPrint((“Get share evnt”));
break;
}
In user mode is an user application:
share_event1 = CreateEvent(NULL,FALSE,FALSE,L"Global\NTProcDrvProcessEvent");
ZeroMemory(buffer_handle,4);
CopyMemory(buffer_handle, &share_event1, sizeof(HANDLE));
ZeroMemory(&sptdwb, sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER));
sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
sptdwb.sptd.PathId = 0;
sptdwb.sptd.TargetId = 0;
sptdwb.sptd.Lun = 0;
sptdwb.sptd.CdbLength = CDB10GENERIC_LENGTH;
sptdwb.sptd.SenseInfoLength = SPT_SENSE_LENGTH;
sptdwb.sptd.DataIn = SCSI_IOCTL_DATA_OUT;
sptdwb.sptd.DataTransferLength = 4;
sptdwb.sptd.TimeOutValue = 2;
sptdwb.sptd.DataBuffer = buffer_handle;
sptdwb.sptd.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER,ucSenseBuf);
sptdwb.sptd.Cdb[0] = myioctl;
length = sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER);
status = DeviceIoControl(hUDisk,
IOCTL_SCSI_PASS_THROUGH_DIRECT,
&sptdwb,
length,
&sptdwb,
length,
&bytesReturn,
NULL);
if (0 == iRet)
{
//getlasterror() will return ERROR_IO_DEVICE,but the buffer_handle has been send down to driver successfully.
}
getc();
SetEvent(share_event1);
then the driver receive share event successfully.
Here are two problem:
When I call DeviceIoControl, it would return ERROR_IO_DEVICE,and cause an exception
Break instruction exception - code 80000003 (first chance)
001b:00304036 cc int 3
But after I ignored this exception,BSOD didn’t happen,and windows still works.
Why?And how to solve this problem?
Thanks for any reply.