|
- HANDLE HandleDriver = INVALID_HANDLE_VALUE; /* 驱动程序句柄 */
- HANDLE hFile = INVALID_HANDLE_VALUE; /* 设备文件操作句柄 */
- HKEY hDrvKey = NULL; /* 注册表操作句柄 */
- // Simple Driver 注册表信息
- WCHAR *szRegKey = L"Drivers\\BuiltIn\\SPL";
- WCHAR *szPrefix = L"SPL";
- WCHAR *szDLL = L"\\SimpleDriver.dll";
- WCHAR *szFriendlyName = L"Simple Driver";
- DWORD DrvIndex = 1;
- DWORD DrvOrder = 0;
- /*
- ; Simple Driver 注册表信息
- [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SPL]
- "Prefix"="SPL"
- "Dll"="\\SimpleDriver.dll"
- "FriendlyName"="Simple Driver"
- "Index"=dword:0
- "Order"=dword:0
- */
- // 向注册表写入一个字符串
- #define WRITE_REG_SZ(Name, Value) RegSetValueEx(hDrvKey, Name, 0, REG_SZ, (LPBYTE)Value, \
- (wcslen(Value)+1)*sizeof(WCHAR))
- // 向注册表写入一个 DWORD 型的值
- #define WRITE_REG_DWORD(Name, Value) {DWORD dwValue = Value; RegSetValueEx( hDrvKey, Name, \
- 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)); }
- // "加载驱动"按键单击事件代码
- void CSPLLoaderDlg::OnLoadDriver()
- {
- LONG nErr;
- DWORD dwDisp;
- // 创建注册表键HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SPL
- nErr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0, L"", 0, 0, NULL, &hDrvKey, &dwDisp);
- if (nErr != ERROR_SUCCESS)
- {
- MessageBox(_T("创建驱动注册表子键失败!"));
- return;
- }
- else
- {
- // 往注册表添加内容
- WRITE_REG_SZ( L"Prefix", szPrefix); /* 写入 "Prefix"="SPL" */
- WRITE_REG_SZ( L"Dll", szDLL); /* 写入 "Dll"="\\SimpleDriver.dll" */
- WRITE_REG_SZ( L"FriendlyName", szFriendlyName); /* 写入 "FriendlyName"="Simple Driver" */
- WRITE_REG_DWORD( L"Order", DrvOrder); /* 写入 "Index"=dword:0 */
- WRITE_REG_DWORD( L"Index", DrvIndex); /* 写入 "Order"=dword:0 */
- }
- HandleDriver = ActivateDeviceEx(szRegKey, NULL, 0, NULL); /* 加载 SimpleDriver 驱动 */
- if (HandleDriver == INVALID_HANDLE_VALUE)
- MessageBox(_T("加载SimpleDriver失败."));
- else
- MessageBox(_T("加载SimpleDriver成功."));
- }
- // "卸载驱动"按键单击事件代码
- void CSPLLoaderDlg::OnUnloadDriver()
- {
- if (HandleDriver != INVALID_HANDLE_VALUE)
- {
- OnCloseDriver(); /* 关闭驱动 */
- BOOL ret = DeactivateDevice(HandleDriver); /* 卸载 SimpleDriver 驱动 */
- if (ret == TRUE)
- {
- RegCloseKey(hDrvKey); /* 关闭注册表键 */
- RegDeleteKey(HKEY_LOCAL_MACHINE, szRegKey); /* 删除注册表键的内容 */
- HandleDriver = INVALID_HANDLE_VALUE;
- MessageBox(_T("卸载SimpleDriver成功."));
- }
- else
- {
- MessageBox(_T("卸载SimpleDriver失败."));
- }
- }
- }
- // "打开驱动" 按键单击事件代码
- void CSPLLoaderDlg::OnOpenDriver()
- {
- // 打开 SimpleDriver 驱动
- hFile = CreateFile(TEXT("SPL1:"), GENERIC_READ | GENERIC_WRITE, 0,
- NULL, OPEN_EXISTING, 0, 0);
-
- if (hFile == INVALID_HANDLE_VALUE)
- MessageBox(_T("不能打开 SPL1!"));
- else
- MessageBox(_T("打开 SPL1 成功!"));
- }
- // "关闭驱动" 按键单击事件代码
- void CSPLLoaderDlg::OnCloseDriver()
- {
- if (hFile != INVALID_HANDLE_VALUE)
- {
- CloseHandle(hFile);
- hFile = INVALID_HANDLE_VALUE;
- }
- }
- // "写入" 按键单击事件代码
- void CSPLLoaderDlg::OnWrite()
- {
- DWORD tmp, actlen;
- if (hFile == INVALID_HANDLE_VALUE) /* 检查驱动是否已打开 */
- {
- MessageBox(_T("SimpleDriver 驱动未打开!"));
- return;
- }
- UpdateData(TRUE);
- if ((m_WriteByte.GetLength() != 2) ||
- (_stscanf(m_WriteByte, _T("%x"), &tmp) != 1)) /* 检查输入参数是否正确 */
- {
- MessageBox(_T("写入字节必须是1个字节的16进制数!"));
- return;
- }
- BYTE writebyte = (BYTE)tmp;
- BOOL ret = WriteFile(hFile, &writebyte, 1, &actlen, NULL); /* 将字节写入驱动 */
- if ((ret == FALSE) || (actlen != 1))
- MessageBox(_T("写入数据失败!"));
- else
- MessageBox(_T("写入数据成功!"));
- }
- // "读出" 按键单击事件代码
- void CSPLLoaderDlg::OnRead()
- {
- BYTE recbuff;
- DWORD actlen;
- if (hFile == INVALID_HANDLE_VALUE) /* 检查驱动是否已打开 */
- {
- MessageBox(_T("SimpleDriver 驱动未打开!"));
- return;
- }
- BOOL ret = ReadFile(hFile, &recbuff, 1, &actlen, NULL); /* 从驱动中读取一个字节 */
- if ((ret == FALSE) || (actlen != 1))
- {
- MessageBox(_T("读出数据失败!"));
- return;
- }
- else
- MessageBox(_T("读出数据成功!"));
- CString strTmp;
- strTmp.Format(_T("%x"), recbuff);
- CEdit *pDispEdit = (CEdit*)GetDlgItem(IDC_READ_DISP); /* 取得控件指针 */
- pDispEdit->SetWindowText(strTmp); /* 读取内容在窗口显示出来 */
- }
复制代码 |
|