#include <iostream>
#include <Windows.h>
#include <Dbt.h>
using namespace std;
#define THRD_MESSAGE_EXIT WM_USER + 1
static HWND hwnd = nullptr;
static const CHAR CLASS_NAME[] = "ActivityProbe";//不可为空
static const GUID GUID_DEVINTERFACE_LIST[] = {
{ 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },
{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
{ 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }
};
//线程应该做的事
DWORD WINAPI threadCallBack(LPVOID lpParameter);
ATOM registerClass();
bool createMessageOnlyWindow();
LRESULT deviceChange(UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
DWORD WINAPI threadCallBack(LPVOID lpParameter);
char firstDriveFromMask(ULONG unitmask);
#if 1
ATOM registerClass() {
//创建窗口
WNDCLASS wc{};
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(nullptr); //窗口进程的实例句柄
wc.lpszClassName = CLASS_NAME;
return RegisterClass(&wc);
}
#endif
#if 1
bool createMessageOnlyWindow() {
hwnd = CreateWindowEx(0,
CLASS_NAME,
(LPCSTR)"",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
return hwnd != nullptr;
}
#endif
//刷新状态
void updateDevice(PDEV_BROADCAST_VOLUME pDevInf, WPARAM wParam) {
if (DBT_DEVICEARRIVAL == wParam) {
std::cout << "插入" << std::endl;
std::cout << firstDriveFromMask(pDevInf->dbcv_unitmask) << std::endl;
} else {
std::cout << "移出" << std::endl;
std::cout << firstDriveFromMask(pDevInf->dbcv_unitmask) << std::endl;
}
}
char firstDriveFromMask(ULONG unitmask) {
char i;
for (i = 0; i < 26; ++i) {
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}
return(i + 'A');
}
//检验
LRESULT deviceChange(UINT message, WPARAM wParam, LPARAM lParam) {
//设备移入与移出任意一种状态均符合状态
PDEV_BROADCAST_HDR pdr = (PDEV_BROADCAST_HDR)lParam;
if ((DBT_DEVICEARRIVAL == wParam
|| DBT_DEVICEREMOVECOMPLETE == wParam)
&& DBT_DEVTYP_VOLUME == pdr->dbch_devicetype) {
updateDevice((PDEV_BROADCAST_VOLUME)pdr, wParam);
}
return 0;
}
//系统自有全局函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT result{};
if (WM_DEVICECHANGE == message) {
result = deviceChange(message, wParam, lParam);
} else {
result = DefWindowProc(hWnd, message, wParam, lParam);
}
return result;
}
#include <thread>
#include <Poco/Thread.h>
//线程回调
DWORD WINAPI threadCallBack(LPVOID lpParameter) {
//注册类别
if (0 == registerClass()) return -1;
//创建窗口 填充HWND
if (0 == createMessageOnlyWindow()) return -1;
//注册通知
//registerDeviceNotify();
#if 1
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
if (msg.message == THRD_MESSAGE_EXIT) {
std::cout << "worker receive the exiting message..." << std::endl;
return 0;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#endif
return 0;
}
//#include <Poco/Thread.h>
//#include <Poco/ThreadTarget.h>
#include <thread>
int main() {
#if 0
DWORD threadId;
HANDLE tHandle = CreateThread(nullptr, 0, threadCallBack, nullptr, 0, &threadId);
if (nullptr == tHandle) {
std::cout << "Thread Create Error!!!" << std::endl;
return -1;
}
PostThreadMessage(threadId, THRD_MESSAGE_EXIT, 0, 0);
WaitForSingleObject(tHandle, INFINITE);
CloseHandle(tHandle);
#endif
//创建新线程用来执行检测USB动态任务
std::thread create(threadCallBack, nullptr);
std::cout << "当前子线程ID: " << create.get_id() << std::endl;
std::cout << "当前子线程句柄: " << create.native_handle() << std::endl;
//create.detach();
create.join();
return 0;
}
思考
- 奇怪了,不断精简下发现没有注册通知也能达到目的