关于CreateFile不解的问题

见习学术士 2011-03-15 02:26:35
NT驱动,使用服务安装动态安装后,问题:
1,不知为何在 CreateService 后,驱动那边收到了 IRP_MJ_CLOSE 消息。。
2,启动成功后,使用CreateFile 打开错误,返回错误代码2,文件不存在。驱动文件是和测试程序在同一个目录的。而且用WINOBJ工具都看到了我的驱动,使用CreateFile 打开居然失败,不解呀。

调用:

CDriver Driver;
HANDLE hHandle;
if( !Driver.OpenDevice("QQKeyBoard", &hHandle) )
{
CString strError;
strError.Format("open dirver failed!, error code:%u", GetLastError());
MessageBox(strError);
return;
}


BOOL CDriver::OpenDevice( IN LPCTSTR DriverName, HANDLE * lphDevice )
{
TCHAR completeDeviceName[64];
HANDLE hDevice;

//
// Create a \\.\XXX device name that CreateFile can use
//
//NOTE: We're making an assumption here that the driver
// has created a symbolic link using it's own name
// (i.e. if the driver has the name "XXX" we assume
// that it used IoCreateSymbolicLink to create a
// symbolic link "\DosDevices\XXX". Usually, there
// is this understanding between related apps/drivers.

// An application might also peruse the DEVICEMAP
// section of the registry, or use the QueryDosDevice
// API to enumerate the existing symbolic links in the
// system.
//

if( (GetVersion() & 0xFF) >= 5 ) {

//
// We reference the global name so that the application can
// be executed in Terminal Services sessions on Win2K
//
wsprintf( completeDeviceName, TEXT("\\\\.\\Global\\%s"), DriverName );

} else {

wsprintf( completeDeviceName, TEXT("\\\\.\\%s"), DriverName );
}

hDevice = CreateFile( completeDeviceName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if ( hDevice == ((HANDLE)-1) )
return FALSE;

// If user wants handle, give it to them. Otherwise, just close it.
if ( lphDevice )
*lphDevice = hDevice;
else
CloseHandle( hDevice );

return TRUE;
}
...全文
129 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
见习学术士 2011-03-15
  • 打赏
  • 举报
回复
靠,原来是IoCreateDevice 的第三个参数,设备名为NULL 了。
改成我的设备么,现在又出现错误代码5:拒绝访问。。汗死
见习学术士 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 joyzml 的回复:]
如果驱动已加载,CreateFile应该会成功的呢。
completeDeviceName这个参数值正确不
[/Quote]
正确的,我的是键盘过滤驱动,刚看到网上说键盘驱动用 CreateFile 不能打开??
不会是真的吧
GARY 2011-03-15
  • 打赏
  • 举报
回复
如果驱动已加载,CreateFile应该会成功的呢。
completeDeviceName这个参数值正确不

21,597

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧