EldoS | Feel safer!

Software components for data protection, secure storage and transfer

How to submit WM_DEVICECHANGE from a .Net application?

Posted: 05/01/2008 10:48:29
by Vladimir Lichman (Priority Standard support level)
Joined: 05/01/2008
Posts: 19

How do I submit WM_DEVICECHANGE message to refresh Windows Exporer from a .NET application?
I am trying to solve this problem: http://www.eldos.com/cbfs/articles/5037.php

Tying to use this code:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

private void button1_Click(object sender, EventArgs e)
const int WM_DEVICECHANGE = 0x0219;
SendMessage(HWND_BROADCAST, WM_DEVICECHANGE, IntPtr.Zero, IntPtr.Zero);

but with no success.
Posted: 05/01/2008 10:53:01
by Eugene Mayevski (Team)

Are you calling this code from the UI application?

CBFS sends this broadcast itself so you need to send it if Explorer and the code which adds a mounting point, are run in different sessions.

Sincerely yours
Eugene Mayevski
Posted: 05/01/2008 12:23:01
by Vladimir Lichman (Priority Standard support level)
Joined: 05/01/2008
Posts: 19

yes I am trying to send WM_DEVICECHANGE from UI application.

The code that adds mounting point is located in Windows Service, so it cannot send WM_DEVICECHANGE as described here: http://www.eldos.com/cbfs­/articles/5037.php

I have found that I need to call BroadcastSystemMessage:

but I do not know how to do this from C# code.
Posted: 05/01/2008 12:49:15
by Eugene Mayevski (Team)

You can wait for upcoming CBFS 2.0 which will include a shell extension DLL. This DLL will send broadcasts for you.

Sincerely yours
Eugene Mayevski
Posted: 05/01/2008 14:11:58
by Vladimir Lichman (Priority Standard support level)
Joined: 05/01/2008
Posts: 19

Not sure if it absolutely correct but it works:

[DllImport("USER32.DLL", EntryPoint="BroadcastSystemMessageA",
SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true,

public static extern int BroadcastSystemMessage(Int32 dwFlags, ref Int32
pdwRecipients, int uiMessage, int wParam, IntPtr lParam);

internal enum DeviceType : uint
DBT_DEVTYP_OEM = 0x00000000,
DBT_DEVTYP_DEVNODE = 0x00000001,
DBT_DEVTYP_VOLUME = 0x00000002,
DBT_DEVTYP_PORT = 0x00000003,
DBT_DEVTYP_NET = 0x00000004

internal enum VolumeChangeFlags : ushort
DBTF_MEDIA = 0x0001,
DBTF_NET = 0x0002

internal struct DEV_BROADCAST_VOLUME
public uint dbcv_size;
public DeviceType dbcv_devicetype;
public uint dbcv_reserved;
public uint dbcv_unitmask;
public VolumeChangeFlags dbcv_flags;

private void button1_Click(object sender, EventArgs e)
const int WM_DEVICECHANGE = 0x0219;
const int DBT_DEVICEARRIVAL = 0x8000;
const int BSF_NOHANG = 0x00000008;
const int BSF_POSTMESSAGE = 0x00000010;

char volumeToRefresh = 'H';

broadcast.dbcv_devicetype = DeviceType.DBT_DEVTYP_VOLUME;
broadcast.dbcv_reserved = 0;
broadcast.dbcv_unitmask = (uint)1 << (volumeToRefresh - 'A');
broadcast.dbcv_flags = VolumeChangeFlags.DBTF_NET;

int size = Marshal.SizeOf(broadcast);
broadcast.dbcv_size = (uint)size;

IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(broadcast, buffer, true);

int recipients = 0;
BroadcastSystemMessage(BSF_NOHANG | BSF_POSTMESSAGE,
ref recipients, WM_DEVICECHANGE, DBT_DEVICEARRIVAL, buffer);



Topic viewed 8530 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS business operates as a division of /n software, inc. For more information, please read the announcement.

Got it!