Click here to Skip to main content
15,887,683 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: WSAGetLastError() Pin
dellthinker11-Oct-07 5:22
dellthinker11-Oct-07 5:22 
GeneralRe: WSAGetLastError() Pin
David Crow11-Oct-07 5:33
David Crow11-Oct-07 5:33 
GeneralRe: WSAGetLastError() Pin
dellthinker11-Oct-07 7:23
dellthinker11-Oct-07 7:23 
QuestionRe: WSAGetLastError() Pin
David Crow11-Oct-07 7:34
David Crow11-Oct-07 7:34 
AnswerRe: WSAGetLastError() Pin
dellthinker11-Oct-07 15:37
dellthinker11-Oct-07 15:37 
GeneralRe: WSAGetLastError() Pin
JudyL_MD12-Oct-07 3:27
JudyL_MD12-Oct-07 3:27 
GeneralRe: WSAGetLastError() Pin
David Crow12-Oct-07 3:30
David Crow12-Oct-07 3:30 
QuestionProblems handling DBT_DEVICEQUERYREMOVE [modified] Pin
64BitWho10-Oct-07 12:23
64BitWho10-Oct-07 12:23 
In an MFC app on VS C++ 2005, I need to process and close files on a USB drive before it is pulled out. My plan is to do this processing when DBT_DEVICEQUERYREMOVE message is broadcast, and to return a TRUE after I have finished processing files on the USB drive.

I have handled WM_DEVICECHANGE and am getting DBT_DEVICEREMOVECOMPLETE messages. But I am not getting DBT_DEVICEQUERYREMOVE messages despite registering. What do I need to do to get those messages? Am I passing the wrong handle or GUID? I'm at my wit's end here, I've been stuck for a day now!

I handled WM_DEVICECHANGE in my message map in MainFrm.cpp like this:
ON_MESSAGE(WM_DEVICECHANGE, &CMainFrame::OnMyDeviceChange)


This function, stripped down, is:
LRESULT CMainFrame::OnMyDeviceChange(WPARAM wParam, LPARAM lParam)
{
	LRESULT lpReturn = TRUE;
	if (wParam == DBT_DEVICEQUERYREMOVE)
	{
		PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;      
		switch( pHdr->dbch_devicetype ) 
		{
			case DBT_DEVTYP_DEVICEINTERFACE:
				{
				}

			case DBT_DEVTYP_HANDLE:
				{
				}

			case DBT_DEVTYP_OEM:
				{
				}

			case DBT_DEVTYP_PORT:
				{
				}

			case DBT_DEVTYP_VOLUME:
				{
					PDEV_BROADCAST_VOLUME pDevVolume = (PDEV_BROADCAST_VOLUME)pHdr;
					MessageBox("The user wants to remove this USB drive.");
					//Do your processing and close all handles
					//lpReturn = BROADCAST_QUERY_DENY; if not ready to unload USB
					lpReturn = TRUE;					
					break;
				}
		}
	}
	return lpReturn;
}


Here is how I am calling my registration function in OnCreate() in my MainFrm.cpp:
HDEVNOTIFY hDevNotify = NULL;
GUID InterfaceClassGuid={0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}; //For USB Drives
cHF.DoRegisterDeviceInterface(this->GetSafeHwnd(), InterfaceClassGuid, hDevNotify);


Here is where I am trying to register to get the DBT_DEVICEQUERYREMOVE messages for USB Drives, in my HelperFunctions.cpp:
BOOL CHelperFucntions::DoRegisterDeviceInterface(HWND hWnd, GUID InterfaceClassGuid, HDEVNOTIFY hDevNotify)
{
    char szMsg[80];
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = InterfaceClassGuid;

    hDevNotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); //or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES

    if(!hDevNotify) 
    {
        wsprintf(szMsg, "Could not register to receive notifications about drive events: %d\n", 
                GetLastError());
        MessageBox(hWnd, szMsg, "RegisterDeviceNotification failed", MB_OK);        
        return FALSE;
    }
    return TRUE;
}


So what am I missing here? Any ideas as to why am I not getting the DBT_DEVICEQUERYREMOVE notification Confused | :confused: I am getting the DBT_DEVICEARRIVAL (0x8000) and DBT_DEVICEREMOVECOMPLETE (0x8004), so it must be something wrong with my registering for the DBT_DEVICEQUERYREMOVE notification?

Any help would be much appreciated. Thanks!

--
Omne Ignotum Pro Magnifico


-- modified at 18:32 Wednesday 10th October, 2007
QuestionVRP using genetic algorithms coded in c++ Pin
mihira18410-Oct-07 11:35
mihira18410-Oct-07 11:35 
AnswerRe: VRP using genetic algorithms coded in c++ Pin
Mark Salsbery10-Oct-07 12:24
Mark Salsbery10-Oct-07 12:24 
Questionfilling memory with a pattern Pin
DQNOK10-Oct-07 11:00
professionalDQNOK10-Oct-07 11:00 
QuestionRe: filling memory with a pattern Pin
David Crow11-Oct-07 3:28
David Crow11-Oct-07 3:28 
AnswerRe: filling memory with a pattern Pin
DQNOK11-Oct-07 3:44
professionalDQNOK11-Oct-07 3:44 
GeneralRe: filling memory with a pattern Pin
David Crow11-Oct-07 3:52
David Crow11-Oct-07 3:52 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 7:06
professionalDQNOK11-Oct-07 7:06 
GeneralRe: filling memory with a pattern Pin
David Crow11-Oct-07 7:21
David Crow11-Oct-07 7:21 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 7:25
professionalDQNOK11-Oct-07 7:25 
AnswerRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 8:10
professionalScott Dorman11-Oct-07 8:10 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 8:30
professionalDQNOK11-Oct-07 8:30 
GeneralRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 8:41
professionalScott Dorman11-Oct-07 8:41 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 8:51
professionalDQNOK11-Oct-07 8:51 
GeneralRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 9:15
professionalScott Dorman11-Oct-07 9:15 
GeneralRe: filling memory with a pattern Pin
DQNOK11-Oct-07 9:43
professionalDQNOK11-Oct-07 9:43 
GeneralRe: filling memory with a pattern Pin
Scott Dorman11-Oct-07 9:45
professionalScott Dorman11-Oct-07 9:45 
QuestionChange the size of the scrollbar, getsystemmetrics [modified] Pin
tortexy10-Oct-07 7:36
tortexy10-Oct-07 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.