0

我发现此代码用于检测闪存,但有时在打开 .exe 文件时出现错误,我无法找出为什么有人可以帮助我了解它是如何工作的?(对不起,我的英语知识非常糟糕)

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <dbt.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
     hInstance = reinterpret_cast<HINSTANCE>(GetModuleHandle(NULL));

    WNDCLASS wndClass = {0};
    wndClass.lpfnWndProc = WndProc;
    wndClass.lpszClassName = TEXT("lua");
    wndClass.hInstance = hInstance;

    if (RegisterClass(&wndClass))
    {
        HWND lua = CreateWindow(wndClass.lpszClassName, NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
        if (lua != NULL)
        {
            DEV_BROADCAST_VOLUME NotificationFilter = {0};
            NotificationFilter.dbcv_size = sizeof(NotificationFilter);
            NotificationFilter.dbcv_devicetype = DBT_DEVTYP_VOLUME;

            HDEVNOTIFY hVolNotify = RegisterDeviceNotification(lua, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

            if (hVolNotify != NULL)
            {
                MSG msg;
                while( GetMessage(&msg, NULL, 0, 0) > 0 )
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }

                UnregisterDeviceNotification(hVolNotify);
            }

            DestroyWindow(lua);
        }

        UnregisterClass(wndClass.lpszClassName, hInstance);
    }

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
    if (uiMsg == WM_DEVICECHANGE)
    {
        MessageBox(NULL, TEXT("WM_DEVICECHANGE"), TEXT("WndProc"), MB_OK);
        return 0;
    }

    return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
4

0 回答 0