Click here to Skip to main content
Click here to Skip to main content

Programmatic Refresh of a File System

, 17 Apr 2005
Rate this:
Please Sign up or sign in to vote.
How to force the file system to refresh.

Introduction

This is a short article, written after many hours spent on the problem of refreshing a CD file system after burning. At the end, the solution I've found is very very simple, so I've decided to explain it here.

The function

I've used a call to the BroadcastSystemMessage function to send a WM_DEVICECHANGE message to Windows (XP in my case). The function is defined as follows (found on MSDN):

long BroadcastSystemMessage(      
    DWORD dwFlags,
    LPDWORD lpdwRecipients,
    UINT uiMessage,
    WPARAM wParam,
    LPARAM lParam
);

To set the correct parameters for the call, I've initialized a DEV_BROADCAST_VOLUME struct, which is defined as:

typedef struct _DEV_BROADCAST_VOLUME {
  DWORD dbcv_size;
  DWORD dbcv_devicetype;
  DWORD dbcv_reserved;
  DWORD dbcv_unitmask;
  WORD dbcv_flags;
} DEV_BROADCAST_VOLUME, *PDEV_BROADCAST_VOLUME;

The code of my function is here:

bool RefreshFileSystem(char cdrive)
{
  DEV_BROADCAST_VOLUME hdr;
  long result;

  hdr.dbcv_size       =  sizeof(DEV_BROADCAST_VOLUME);
  hdr.dbcv_devicetype =  DBT_DEVTYP_VOLUME;
  hdr.dbcv_reserved = 0;
  cdrive = (char)_totupper(cdrive));  // thanks to Blake Miller
  hdr.dbcv_unitmask = 1 << (cdrive-'A');
  hdr.dbcv_flags  = DBTF_MEDIA;

  result = BroadcastSystemMessage(BSF_NOHANG|BSF_POSTMESSAGE,
                         NULL, WM_DEVICECHANGE, DBT_DEVICEARRIVAL,
                         (LPARAM)&hdr);
  return (result>=0);
}

I've heard of someone who used this kind of a solution but unfortunately the system crashed. I'm trying to find out what has caused the crash.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

shaman74
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
GeneralC# code - Works for me on network drives Pinmembertheit851412-Jun-11 18:00 
GeneralPossible crash causess PinmemberSimon Owen25-Jun-06 22:58 
Could the crash on some systems be because you're passing a pointer in LPARAM, and it'll only be valid in your own address space? It'll only crash if something is registered for those notifications and tries to use the pointer. There's some extra magic involved when the system sends them, to ensure it's valid for whatever is receiving it.

Another possibility is that you're POSTING the messages, but the pointer you're passing is to a stack variable that will be out of scope before some applications have processed the message. Making the structure variable static would help, but only if the pointer is actually valid to other processes anyway.
Questionquery Pinmemberachalshah717-Mar-06 2:52 
GeneralSHChangeNotify PinmemberCabbi9-Jun-05 3:53 
Generalcan't refresh CDRW content PinsussSergey Apollonov23-May-05 20:34 
GeneralCode Correction : Drive Letter PinmemberBlake Miller12-Apr-05 5:30 
GeneralRe: Code Correction : Drive Letter Pinmembershaman7412-Apr-05 6:17 
GeneralHhhhhhhhhhhhhhh ! PinmemberKochise8-Apr-05 0:24 
GeneralRe: Hhhhhhhhhhhhhhh ! Pinmembershaman748-Apr-05 0:32 
GeneralRe: Hhhhhhhhhhhhhhh ! PinmemberOutlook14-Apr-05 11:41 
GeneralRe: Hhhhhhhhhhhhhhh ! Pinmembershaman7414-Apr-05 21:52 
GeneralRe: Hhhhhhhhhhhhhhh ! PinmemberMX-Mauro18-Apr-05 2:20 
GeneralRe: Hhhhhhhhhhhhhhh ! PinmemberAndy N18-Apr-05 14:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 18 Apr 2005
Article Copyright 2005 by shaman74
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid