Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:29:45+00:00 2026-06-11T20:29:45+00:00

I am trying to detect when a USB drive is inserted. Right now, I’m

  • 0

I am trying to detect when a USB drive is inserted. Right now, I’m creating a dummy window and RegisterDeviceNotificationing it. However, I don’t think my approach is correct, as the window doesn’t seem to be receiving messages.

#include <iostream>
#define WINVER 0x501
#include <windows.h>
#include <dbt.h>
#include "devicehandler.h"
#include "remover.h"

DeviceHandler::DeviceHandler(Remover* remover)
{
    this->remover = remover;
    this->hWnd = this->createHandleWindow();
    this->registerDeviceHandler();
    this->messageLoop(this->hWnd);
}

DeviceHandler::~DeviceHandler()
{
    this->unregisterDeviceHandler();
}

void DeviceHandler::messageLoop(HWND hWnd)
{
    std::cerr << "Entering message loop." << std::endl; // Gets here!

    MSG msg;
    while (GetMessage(&msg, this->hWnd, 0, 0)) {
        std::cerr << "Loop!" << std::endl; // Does not get here!
        switch (msg.message) {
            case WM_DEVICECHANGE:
                {
                    PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR) msg.lParam;
                    switch(msg.wParam) {
                        case DBT_DEVICEARRIVAL:
                            std::cerr << "Device added!" << std::endl;
                            break;
                        default:
                            std::cerr << "Other device event." << std::endl;
                            break;
                    }
                    break;
                }
            default:
                break;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

HWND DeviceHandler::createHandleWindow()
{
    std::cerr << "Creating handle window... ";

    HWND hWnd = CreateWindow(NULL, NULL, WS_ICONIC, 0, 0, CW_USEDEFAULT, 0,
                             NULL, NULL, GetModuleHandle(NULL), NULL);
    ShowWindow(hWnd, SW_HIDE);

    std::cerr << "done!" << std::endl;
    return hWnd;
}

void DeviceHandler::registerDeviceHandler()
{
    std::cerr << "Device notification handling... ";
    // GUID guid = { 0xa5dcbf10, 0x6530, 0x11d2, { 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed } };
    GUID guid = { 0x25dbce51, 0x6c8f, 0x4a72, { 0x8a, 0x6d, 0xb5, 0x4c, 0x2b, 0x4f, 0xc8, 0x35 } };
    DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
    ZeroMemory(&notificationFilter, sizeof(notificationFilter));
    notificationFilter.dbcc_size       = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    notificationFilter.dbcc_classguid  = guid;

    this->deviceNotifier = RegisterDeviceNotification(this->hWnd,
                                                      &notificationFilter,
                                                      DEVICE_NOTIFY_WINDOW_HANDLE);
    std::cerr << "done!" << std::endl;
}

void DeviceHandler::unregisterDeviceHandler()
{
    UnregisterDeviceNotification(this->deviceNotifier);
}

I’m guessing that this is not the right place to put the message loop, but I’m not very good with WinAPI. How do I get my program to enter the message loop? (And also preferably register device notifications.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:29:46+00:00Added an answer on June 11, 2026 at 8:29 pm

    You should be creating a message only Window. It can receive messages it registers like device notification and power events, but it will never be shown.

    Message only window must be registered with RegisterClassEx.

    With you window class registered, you then pass your window class name to CreateWindowEx along with HWND_MESSAGE as a parent. Your CreateWindow should use CreateWindowEx instead to look like this :

    HWND hWnd = CreateWindowEx(0, myClassName, _T("This is not the class name"), 
                    0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, this);
    

    You will still use GetMessage along with TranslateMessage and DispathMessage which will call your WNDPROC like a callback. Nothing wrong with that, but you loose the pointer to your DeviceHandler class in your windows procedure.

    This is why you pass this as the last parameter of CreateWindowEx. You can retreive it when you receive the WM_CREATE message and set it to your windows class user data with SetWindowLongPtr and GWLP_USERDATA. Every call after that will retreive the pointer with GetWindowLongPtr and cast it to a DeviceHandler object.

    Here is a good example of the technique.

    To play by the rules, you should register your own window class to use GWLP_USERDATA, although it should still work using the STATIC window class.

    Final note : be sure to call DefWindowProc for messages you don’t handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to detect a USB disk drive being inserted within a Windows
Trying to detect left click vs right click (without using jQuery!) and I have
I am trying to detect changes in window.location (for example to be notified if
I am trying to detect the Turn on USB storage using BroadcastReceiver though i
I've been trying to watch a USB subsystem to detect when devices are added
I am trying detect when a textarea becomes full for creating pagination effect. Using
im trying to detect when a user make a left or right swing in
In trying to detect a right mouse click with jquery, I noticed that the
I'm trying to detect a swipe gesture to switch between two .xib files. Right
I am trying to detect groups which contain the difference between first age and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.