|
用CreateFile打开过滤驱动失败,这是怎么回事?
[复制链接]
做了一个U盘过滤驱动,需要用DeviceIoControl和过滤驱动通信,在MFC exe程序中可以使用CreateFile打开并使用DeviceIoControl与过滤驱动通信。
但是在一个dll中却无法使用CreateFile打开驱动,我用GetLastError和FormatMessage跟踪,没有报错,说是操作成功完成,估计是权限不够吧。
但是过滤驱动的DriverEntry函数中,已经加了降低权限的代码,不知道为什么还是打不开?向大家请教
#define USBFILTER_SYSDEVICE_NAME L"\\Device\\HTUsbFilter"
#define USBFILTER_DOSDEVICE_NAME L"\\DosDevices\\HTUsbFilter"
#pragma INITCODE
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath)
{
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
MyDriverObject = DriverObject;
// create control device object
RtlInitUnicodeString(&DeviceName, USBFILTER_SYSDEVICE_NAME);
NtStatus = IoCreateDevice(DriverObject,
0,
&DeviceName,
FILE_DEVICE_DISK,
0,
FALSE,
&MyControlDeviceObject);
if (!NT_SUCCESS(NtStatus))
{
KdPrint((DRIVERNAME " - IoCreateDevice failed - %X\n", NtStatus));
return NtStatus;
}
NtStatus = ObOpenObjectByPointer(MyControlDeviceObject,
0,
NULL,
FILE_ALL_ACCESS,
NULL,KernelMode,
&CtlDevHandle);
if(!NT_SUCCESS(NtStatus))
{
return NtStatus;
}
ObDereferenceObject(MyControlDeviceObject);
SecurityDescriptor = (PSECURITY_DESCRIPTOR)ExAllocatePool(NonPagedPool,sizeof(SECURITY_DESCRIPTOR));
NtStatus = RtlCreateSecurityDescriptor(SecurityDescriptor,SECURITY_DESCRIPTOR_REVISION);
if(!NT_SUCCESS(NtStatus))
{
KdPrint((DRIVERNAME " - Entering DriverEntry: DriverObject %8.8lX\n", DriverObject));
return NtStatus;
}
NtStatus = NtSetSecurityObject(CtlDevHandle,DACL_SECURITY_INFORMATION,SecurityDescriptor);
if(!NT_SUCCESS(NtStatus))
{
return NtStatus;
}
ZwClose(CtlDevHandle);
// create symbol Link
RtlInitUnicodeString(&SymLinkName, USBFILTER_DOSDEVICE_NAME);
NtStatus = IoCreateSymbolicLink(&SymLinkName, &DeviceName);
if (!NT_SUCCESS(NtStatus))
{
KdPrint((DRIVERNAME " - IoCreateSymbolicLink failed - %X\n", NtStatus));
IoDeleteDevice(MyControlDeviceObject);
return NtStatus;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
return STATUS_SUCCESS;
} // DriverEntry
dll中的相关代码:
BOOL SendKeyUsernameToUsbfilter(char* Username,int len)
{
BOOL bStatus;
DWORD dwRet;
int buflen = len;
char keyUsername[64] = {0};
HANDLE hHTUSBFILTERDevice = INVALID_HANDLE_VALUE;//scott
memcpy(keyUsername,Username,len);
//add notification to USBFILTER
hHTUSBFILTERDevice = CreateFile(HTUSBFILTER_NAME,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hHTIPSECDevice != INVALID_HANDLE_VALUE)
{
bStatus = DeviceIoControl(hHTUSBFILTERDevice,
IOCTL_USBFILTER_GET_USERNAME,
keyUsername,
buflen,
NULL,
0,
&dwRet,
NULL);
CloseHandle( hHTUSBFILTERDevice );
if (!bStatus)
{
MessageBox(NULL,"通知U盘过滤驱动失败-DeviceIoControl!","SendKeyUsernameToUsbfilter",0);
}
MessageBox(NULL,"通知U盘过滤驱动成功!","SendKeyUsernameToUsbfilter",0);
}
else
{
// DWORD nErrorNo = GetLastError ( ); // 得到错误代码
// LPSTR lpBuffer;
// FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
// FORMAT_MESSAGE_IGNORE_INSERTS |
// FORMAT_MESSAGE_FROM_SYSTEM,
// NULL,
// nErrorNo, // 此乃错误代码,通常在程序中可由 GetLastError()得之
// LANG_NEUTRAL,
// (LPTSTR) & lpBuffer,
// 0 ,
// NULL );
// MessageBox(NULL,lpBuffer,0,0);
// LocalFree (lpBuffer);
MessageBox(NULL,"打开U盘过滤驱动失败-CreateFile!","SendKeyUsernameToUsbfilter",0);
return FALSE;
}
return TRUE;
}
|
|