19,502
社区成员
发帖
与我相关
我的任务
分享
HANDLE hFile = INVALID_HANDLE_VALUE; /* 驱动程序设备句柄 */
DWORD Key1Count = 0;
// 读 KEY1 状态线程
DWORD CKey1ExampleDlg::ReadKey1Thread(LPVOID lparam)
{
BYTE status;
DWORD actlen;
CString strCount;
CKey1ExampleDlg *pDlg = (CKey1ExampleDlg*)lparam; /* 取得对话框指针 */
CStatic *pCountStatic = (CStatic*)pDlg->GetDlgItem(IDC_KEY1_COUNTER);
/* 取得显示计数值的文本框指针 */
while(TRUE)
{
if (hFile == INVALID_HANDLE_VALUE)
break; /* 驱动未打开, 退出线程 */
if (ReadFile(hFile, &status, 1, &actlen, NULL) == TRUE)
{
OutputDebugString(_T("ReadFile(hFile, &status, 1, &actlen, NULL)\r\n"));
if (status == 0x03)
{ /* KEY1 按键按下 */
Key1Count++; /* 计数器计数 */
strCount.Format(_T("%d"), Key1Count);
pCountStatic->SetWindowText(strCount); /* 显示 */
OutputDebugString(_T("STATUS ======= 3\r\n"));
}
else if (status == 0xff)
{ /* KEY1 按键按下 */
OutputDebugString(_T("STATUS ======= ff\r\n"));
}
else if (status == 1)
{ /* KEY1 按键按下 */
Key1Count++; /* 计数器计数 */
strCount.Format(_T("%d"), Key1Count);
pCountStatic->SetWindowText(strCount); /* 显示 */
OutputDebugString(_T("STATUS ======= 3\r\n"));
}
else
{
break;
}
}
else
break;
}
return 1;
}
// "打开驱动" 按键单击事件代码
void CKey1ExampleDlg::OnOpenKey1()
{
DWORD IDThread;
HANDLE hReadKey1Thread; /* 读 KEY1 线程句柄 */
// 打开 KEY1 驱动
hFile = CreateFile(TEXT("KEY1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 KEY1 驱动失败!"));
return;
}
// 创建读 KEY1 状态线程
hReadKey1Thread = CreateThread(0, 0, ReadKey1Thread, this, 0, &IDThread);
if (hReadKey1Thread == NULL)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
return;
}
CloseHandle(hReadKey1Thread);
// 使能 "关闭驱动" 按键, 禁止 "打开驱动" 按键
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_KEY1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_KEY1); /* 取得控件指针 */
pOpenButton->EnableWindow(FALSE);
pCloseButton->EnableWindow(TRUE);
MessageBox(_T("打开 KEY1 驱动成功!"));
}
// "关闭驱动" 按键单击事件代码
void CKey1ExampleDlg::OnCloseKey1()
{
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_KEY1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_KEY1); /* 取得控件指针 */
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile); /* 关闭驱动 */
hFile = INVALID_HANDLE_VALUE;
pOpenButton->EnableWindow(TRUE); /* 使能"打开驱动"按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止"关闭驱动"按键 */
}
}
HANDLE hFile = INVALID_HANDLE_VALUE; /* 驱动程序设备句柄 */
DWORD Key1Count = 0;
// 读 KEY1 状态线程
DWORD CKey1ExampleDlg::ReadKey1Thread(LPVOID lparam)
{
BYTE status;
DWORD actlen;
CString strCount;
CKey1ExampleDlg *pDlg = (CKey1ExampleDlg*)lparam; /* 取得对话框指针 */
CStatic *pCountStatic = (CStatic*)pDlg->GetDlgItem(IDC_KEY1_COUNTER);
/* 取得显示计数值的文本框指针 */
while(TRUE)
{
if (hFile == INVALID_HANDLE_VALUE)
break; /* 驱动未打开, 退出线程 */
if (ReadFile(hFile, &status, 1, &actlen, NULL) == TRUE)
{
OutputDebugString(_T("ReadFile(hFile, &status, 1, &actlen, NULL)\r\n"));
if (status == 0x03)
{ /* KEY1 按键按下 */
Key1Count++; /* 计数器计数 */
strCount.Format(_T("%d"), Key1Count);
pCountStatic->SetWindowText(strCount); /* 显示 */
OutputDebugString(_T("STATUS ======= 3\r\n"));
}
else if (status == 0xff)
{ /* KEY1 按键按下 */
OutputDebugString(_T("STATUS ======= ff\r\n"));
}
else if (status == 1)
{ /* KEY1 按键按下 */
Key1Count++; /* 计数器计数 */
strCount.Format(_T("%d"), Key1Count);
pCountStatic->SetWindowText(strCount); /* 显示 */
OutputDebugString(_T("STATUS ======= 3\r\n"));
}
else
{
break;
}
}
else
break;
}
return 1;
}
// "打开驱动" 按键单击事件代码
void CKey1ExampleDlg::OnOpenKey1()
{
DWORD IDThread;
HANDLE hReadKey1Thread; /* 读 KEY1 线程句柄 */
// 打开 KEY1 驱动
hFile = CreateFile(TEXT("KEY1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 KEY1 驱动失败!"));
return;
}
// 创建读 KEY1 状态线程
hReadKey1Thread = CreateThread(0, 0, ReadKey1Thread, this, 0, &IDThread);
if (hReadKey1Thread == NULL)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
return;
}
CloseHandle(hReadKey1Thread);
// 使能 "关闭驱动" 按键, 禁止 "打开驱动" 按键
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_KEY1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_KEY1); /* 取得控件指针 */
pOpenButton->EnableWindow(FALSE);
pCloseButton->EnableWindow(TRUE);
MessageBox(_T("打开 KEY1 驱动成功!"));
}
// "关闭驱动" 按键单击事件代码
void CKey1ExampleDlg::OnCloseKey1()
{
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_KEY1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_KEY1); /* 取得控件指针 */
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile); /* 关闭驱动 */
hFile = INVALID_HANDLE_VALUE;
pOpenButton->EnableWindow(TRUE); /* 使能"打开驱动"按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止"关闭驱动"按键 */
}
}
#include <windows.h>