DLL的线程中创建WINDOWS窗口并接收U盘移动磁盘插入消息
这个问题注意以下几个关键点
1.DLL的句柄的传入
2.线程中的消息循环
HINSTANCE g_hInstance;
TCHAR U[2]; //保存U盘的盘符
TCHAR FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)//看该驱动器的状态是否发生了变化
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
LRESULT OnDeviceChange(HWND hwnd,WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
if (wParam==DBT_DEVICEARRIVAL)
{
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
U[0]=FirstDriveFromMask(lpdbv ->dbcv_unitmask);//得到u盘盘符
U[1]=':';
U[2]='\0';
MessageBox(0,U,_T("Notice!"),MB_OK);
}
}
if (wParam==DBT_DEVICEREMOVECOMPLETE)
{
//设备删除
}
return LRESULT();
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DEVICECHANGE:
OnDeviceChange(hwnd,wParam,lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
DWORD WINAPI mythread_create_windows(LPVOID lparam)
{
static TCHAR szAppName[]=TEXT ("UUUUUU");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =0;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =g_hInstance;
wndclass.hIcon =0;
wndclass.hCursor =0;
wndclass.hbrBackground =0;
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL,TEXT("Program requires Windows NT!"),szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName,NULL,WS_DISABLED,0,0,0,0,NULL,NULL,g_hInstance,NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}
BOOL APIENTRY DllMain( HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
g_hInstance=(HINSTANCE)hModule;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
OutputDebugStringA("DLL_PROCESS_ATTACH ENTER");
//CreateThread(NULL,NULL,mythread,NULL,NULL,NULL);
CreateThread(NULL,NULL,mythread_create_windows,NULL,NULL,NULL);
OutputDebugStringA("DLL_PROCESS_ATTACH END");
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
这个问题注意以下几个关键点
1.DLL的句柄的传入
2.线程中的消息循环
HINSTANCE g_hInstance;
TCHAR U[2]; //保存U盘的盘符
TCHAR FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)//看该驱动器的状态是否发生了变化
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
LRESULT OnDeviceChange(HWND hwnd,WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
if (wParam==DBT_DEVICEARRIVAL)
{
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
U[0]=FirstDriveFromMask(lpdbv ->dbcv_unitmask);//得到u盘盘符
U[1]=':';
U[2]='\0';
MessageBox(0,U,_T("Notice!"),MB_OK);
}
}
if (wParam==DBT_DEVICEREMOVECOMPLETE)
{
//设备删除
}
return LRESULT();
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DEVICECHANGE:
OnDeviceChange(hwnd,wParam,lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
DWORD WINAPI mythread_create_windows(LPVOID lparam)
{
static TCHAR szAppName[]=TEXT ("UUUUUU");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =0;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =g_hInstance;
wndclass.hIcon =0;
wndclass.hCursor =0;
wndclass.hbrBackground =0;
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL,TEXT("Program requires Windows NT!"),szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName,NULL,WS_DISABLED,0,0,0,0,NULL,NULL,g_hInstance,NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}
BOOL APIENTRY DllMain( HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
g_hInstance=(HINSTANCE)hModule;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
OutputDebugStringA("DLL_PROCESS_ATTACH ENTER");
//CreateThread(NULL,NULL,mythread,NULL,NULL,NULL);
CreateThread(NULL,NULL,mythread_create_windows,NULL,NULL,NULL);
OutputDebugStringA("DLL_PROCESS_ATTACH END");
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}