Description: Creates a Win32 form program. Use the global variable hwnd to obtain the handle for creating the form. Create a form in the winmain function, and register the message with the drive letter to the form. In the form callback function, add a response to the message with the drive letter changed.
// Testwin32.cpp: defines the entry point for the application.
//
# Include "stdafx. H"
# Include "testwin32.h"
# Include <DBT. h>
# Include <strsafe. h>
# Define max_loadstring 100
// Global variables:
Hinstance hinst; // current instance
Tchar sztitle [max_loadstring]; // The title bar text
Tchar szwindowclass [max_loadstring]; // The main window class name
Hwnd; // custom global variable, form handle
// Forward declarations of functions included in this Code module:
Atom myregisterclass (hinstance );
Bool initinstance (hinstance, INT );
Lresult callback wndproc (hwnd, uint, wparam, lparam );
Int_ptr callback about (hwnd, uint, wparam, lparam );
// Obtain the new drive letter, that is, it is not enough to insert the drive letter light of the device into the USB interface. The best thing is to use the getdevicetype () function to judge the drive letter.
Char firstdrivefrommask (ulong unitmask)
{
Char I;
For (I = 0; I <26; ++ I)
{
If (unitmask & 0x1)
Break;
Unitmask = unitmask> 1;
}
Return (I + 'A ');
}
/// Register a message with the form for changing the drive letter
Bool doregisterdeviceinterface (
Guid interfaceclassguid,
Hdevnotify * hdevnotify
)
/*
Routine description:
Registers for notification of changes in the device interfaces
The specified Interface Class guid.
Parameters:
Interfaceclassguid-the interface class guid for the device
Interfaces.
Hdevpolicy-keys es the device notification handle. On failure,
This value is null.
Return Value:
If the function succeeds, the return value is true.
If the function fails, the return value is false.
*/
{
Dev_broadcast_deviceinterface icationicationfilter;
Char szmsg [80];
Zeromemory (& notificationfilter, sizeof (icationicationfilter ));
Icationicationfilter. dbcc_size =
Sizeof (dev_broadcast_deviceinterface );
Icationicationfilter. dbcc_devicetype = dbt_devtyp_deviceinterface;
Icationicationfilter. dbcc_classguid = interfaceclassguid;
* Hdevnotify = registerdevicenotification (hwnd,
& Notificationfilter,
Device_policy_window_handle
);
If (! * Hdevnotify)
{
Sprintf (szmsg, "registerdevicenotification failed: % d/N ",
Getlasterror ());
MessageBox (hwnd, szmsg, "Registration", mb_ OK );
Return false;
}
Return true;
}
Int apientry _ twinmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
Unreferenced_parameter (hprevinstance );
Unreferenced_parameter (lpcmdline );
// Todo: Place code here.
MSG;
Haccel hacceltable;
// Initialize global strings
Loadstring (hinstance, ids_app_title, sztitle, max_loadstring );
Loadstring (hinstance, idc_testwin32, szwindowclass, max_loadstring );
Myregisterclass (hinstance );
// Perform application initialization:
If (! Initinstance (hinstance, ncmdshow ))
{
Return false;
}
// Define a drive letter to change the message guid and register it with the form through the Function
_ Guid guid_devinterface_usb_device;
Guid_devinterface_usb_device.data1 = 0xa5dcbf10l;
Guid_devinterface_usb_device.data2 = 0x6530;
Guid_devinterface_usb_device.data3 = 0x11d2;
Guid_devinterface_usb_device.data4 [0] = 0x90;
Guid_devinterface_usb_device.data4 [1] = 0x1f;
Guid_devinterface_usb_device.data4 [2] = 0x00;
Guid_devinterface_usb_device.data4 [3] = 0xc0;
Guid_devinterface_usb_device.data4 [4] = 0x4f;
Guid_devinterface_usb_device.data4 [5] = 0xb9;
Guid_devinterface_usb_device.data4 [6] = 0x51;
Guid_devinterface_usb_device.data4 [7] = 0xed;
Hdevnotify;
Doregisterdeviceinterface (guid_devinterface_usb_device, & hdevnotify );
// Waitforsingleobject (hdevnotify, infinite );
Hacceltable = loadaccelerators (hinstance, makeintresource (idc_testwin32 ));
// Main message loop:
While (getmessage (& MSG, null, 0, 0 ))
{
If (! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
}
Return (INT) msg. wparam;
}
//
// Function: myregisterclass ()
//
// Purpose: registers the window class.
//
// Comments:
//
// This function and its usage are only necessary if you want this code
// To be compatible with Win32 systems prior to the 'registerclassex'
// Function that was added to Windows 95. It is important to call this function
// So that the application will get 'well formed 'small icons associated
// With it.
//
Atom myregisterclass (hinstance)
{
Wndclassex wcex;
Wcex. cbsize = sizeof (wndclassex );
Wcex. Style = cs_hredraw | cs_vredraw;
Wcex. lpfnwndproc = wndproc;
Wcex. cbclsextra = 0;
Wcex. cbwndextra = 0;
Wcex. hinstance = hinstance;
Wcex. hicon = loadicon (hinstance, makeintresource (idi_testwin32 ));
Wcex. hcursor = loadcursor (null, idc_arrow );
Wcex. hbrbackground = (hbrush) (color_window + 1 );
Wcex. lpszmenuname = makeintresource (idc_testwin32 );
Wcex. lpszclassname = szwindowclass;
Wcex. hiconsm = loadicon (wcex. hinstance, makeintresource (idi_small ));
Return registerclassex (& wcex );
}
//
// Function: initinstance (hinstance, INT)
//
// Purpose: saves instance handle and creates Main Window
//
// Comments:
//
// In this function, we save the instance handle in a global variable and
// Create and display the main program window.
//
Bool initinstance (hinstance, int ncmdshow)
{
Hinst = hinstance; // store instance handle in our global variable
Hwnd = createwindow (szwindowclass, sztitle, ws_overlappedwindow,
Cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null );
If (! Hwnd)
{
Return false;
}
/// Display the form and refresh it
Showwindow (hwnd, ncmdshow );
Updatewindow (hwnd );
Return true;
}
//
// Function: wndproc (hwnd, uint, wparam, lparam)
//
// Purpose: Processes messages for the main window.
//
// Wm_command-process the application menu
// Wm_paint-paint the Main Window
// Wm_destroy-post a quit message and return
//
//
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Int wmid, wmevent;
Paintstruct pS;
HDC;
Switch (Message)
{
Case wm_command:
Wmid = loword (wparam );
Wmevent = hiword (wparam );
// Parse the menu selections:
Switch (wmid)
{
Case idm_about:
Dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about );
Break;
Case idm_exit:
Destroywindow (hwnd );
Break;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Break;
Case wm_paint:
HDC = beginpaint (hwnd, & PS );
// Todo: add any drawing code here...
Endpaint (hwnd, & PS );
Break;
Case wm_destroy:
Postquitmessage (0 );
Break;
////// Handle the message response when the drive letter changes
Case wm_devicechange:
If (wparam = dbt_devicearrival) // device activation
{
Pdev_broadcast_hdr lpdb = (pdev_broadcast_hdr) lparam;
Pdev_broadcast_volume lpdbv = (pdev_broadcast_volume) lpdb;
Char szmsg [80];
Sprintf (szmsg, "Drive % C: loaded/N ",
Firstdrivefrommask (lpdbv-> dbcv_unitmask ));
MessageBox (hwnd, szmsg, "wm_devicechange", mb_ OK );
}
Else if (wparam = dbt_deviceremovecomplete)
{
Pdev_broadcast_hdr lpdb = (pdev_broadcast_hdr) lparam;
Pdev_broadcast_volume lpdbv = (pdev_broadcast_volume) lpdb;
Char szmsg [80];
Sprintf (szmsg, "Drive % C: uninstalled/N ",
Firstdrivefrommask (lpdbv-> dbcv_unitmask ));
MessageBox (hwnd, szmsg, "wm_devicechange", mb_ OK );
}
Break;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Return 0;
}
// Message handler for about box.
Int_ptr callback about (hwnd hdlg, uint message, wparam, lparam)
{
Unreferenced_parameter (lparam );
Switch (Message)
{
Case wm_initdialog:
Return (int_ptr) true;
Case wm_command:
If (loword (wparam) = idok | loword (wparam) = idcancel)
{
Enddialog (hdlg, loword (wparam ));
Return (int_ptr) true;
}
Break;
}
Return (int_ptr) false;
}