I would like to make a program for windows 7 that checking all the time if there is a USB insertion or USB removal in C/C++, How Could I do this?, Could you give an example code?, thank you
Asked
Active
Viewed 3,323 times
0
-
1There is no "C/C++". You do it either in C or in C++. And you'll likely need quite a few Windows API functions for that. What research on the topic have you made so far? – Christian Hackl Mar 16 '14 at 17:39
-
Possible duplicate of http://stackoverflow.com/questions/16214768/detecting-usb-insertion-removal-in-c-non-gui-application – cup Mar 16 '14 at 17:58
-
When I put C/C++ is because both works for me. I was reading something about RegisterDeviceNotification but normally, in the Windows examples appears an example with detect insert and removal from CD. I was testing the code the examples of the link above but in mi case I don't need to create a window to run the program with running the program by console is enough for me – user1511378 Mar 17 '14 at 07:06
-
http://stackoverflow.com/a/6994591/2903452 – Balu Mar 17 '14 at 07:37
2 Answers
2
It works thank you all, this is the code:
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <dbt.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define USE_CDROM_GUID_ONLY
//-----------------------------------------------------------------------------
MSG msg;
void pupu(HWND hwnd);
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg,WPARAM wParam, LPARAM lParam)
{
if (msg == WM_DEVICECHANGE)
{
switch (wParam){
case DBT_DEVICEARRIVAL:
printf("new device connected \n");
break;
case DBT_DEVICEREMOVECOMPLETE:
printf("a device has been removed \n");
break;
}
}//if
else
cout << "Got msg " << msg << ", " << int(wParam)
<< ", " << int(lParam) << endl;
return 1;
}//WinProc
//-----------------------------------------------------------------------------
HWND pipi(){
const char *className = "DevNotifyTest";
WNDCLASSA wincl = { 0 };
wincl.hInstance = GetModuleHandle(0);
wincl.lpszClassName = className;
wincl.lpfnWndProc = WinProc;
HWND parent = 0;
#ifdef USE_MESSAGE_ONLY_WINDOW
parent = HWND_MESSAGE;
#endif
HWND hwnd = CreateWindowExA(WS_EX_TOPMOST, className, className,0, 0, 0, 0, 0, parent, 0, 0, 0);
GUID cdromDevIntGuid =
{ 0x53F56308, 0xB6BF, 0x11D0,
{ 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B } };
DEV_BROADCAST_DEVICEINTERFACE_A notifyFilter = { 0 };
notifyFilter.dbcc_size = sizeof(notifyFilter);
notifyFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notifyFilter.dbcc_classguid = cdromDevIntGuid;
HDEVNOTIFY hDevNotify =
RegisterDeviceNotificationA(hwnd, ¬ifyFilter,
#ifndef USE_CDROM_GUID_ONLY
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES |
#endif
DEVICE_NOTIFY_WINDOW_HANDLE);
return hwnd;
}
void pupu(HWND hwnd){
BOOL bRet = PeekMessage(&msg, hwnd, 0, 0, PM_NOREMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
Sleep(1000);
}
int main()
{
HWND hwnd = pipi();
for (;;){
pupu(hwnd);
Sleep(1000);
}
return 0;
}//main

user1511378
- 31
- 1
- 3
1
Registering for Device Notification is a sample device notification example.
RegisterDeviceNotification function to register to receive notification messages from the system.
Detecting Media Insertion or Removal Windows send WM_DEVICECHANGE messages to top-level windows when new devices or media are added and become available, and when existing devices or media are removed.

Balu
- 2,247
- 1
- 18
- 23