How to Enable Drag and Drop for an Elevated MFC Application on Vista/Windows 7

Finding good information on how to enable drag and drop for MFC applications is hard enough (why?). But just when you think you should have it working, you hit a very solid wall: UIPI (User Interface Privilege Isolation, a variant of UAC, User Account Control). Because of “security” reasons, UIPI disables drag and drop from medium integrity processes (Explorer) to high integrity (aka elevated) processes by filtering out most window messages, including those required for drag and drop. In essence drag and drop from Explorer to your elevated application does not work.

To waste your time, Microsoft added the API function ChangeWindowMessageFilter that lets specific window messages through (punching a hole in the UIPI “firewall”), but just adding WM_DROPFILES to the list of allowed messages does not help. Drag and drop uses more than just WM_DROPFILES. But where is a list of the window messages required for drag and drop? Even the venerable Raymond Chen refused to explain how to make it work. Well, here is how.

Basic Drag and Drop

This assumes you have a dialog-based MFC application. I have implemented this with Visual Studio 2008 SP1.

1) In your dialog’s OnInitDialog method, enable drag and drop. You can either do this for the entire dialog or a specific control:

DragAcceptFiles (); // entire dialog
m_oPath.DragAcceptFiles (); // control represented by variable m_oPath

2) Add ON_WM_DROPFILES() to your message map, e.g.:

BEGIN_MESSAGE_MAP(<dialog class name>, CDialog)
   ON_WM_DROPFILES()
END_MESSAGE_MAP()

3) Add an event handler for drop events. In your dialog’s header file:

afx_msg void OnDropFiles(HDROP hDropInfo);

And in the CPP file:

void <dialog class name>::OnDropFiles (HDROP dropInfo)
{
    CString    sFile;
    DWORD      nBuffer    = 0;
    // Get the number of files dropped
    UINT nFilesDropped    = DragQueryFile (dropInfo, 0xFFFFFFFF, NULL, 0);
    // If more than one, only use the first
    if (nFilesDropped > 0)
    {
        // Get the buffer size for the first filename
        nBuffer = DragQueryFile (dropInfo, 0, NULL, 0);
        // Get path and name of the first file
        DragQueryFile (dropInfo, 0, sFile.GetBuffer (nBuffer + 1), nBuffer + 1);
        sFile.ReleaseBuffer ();
        // Do something with the path
    }
    // Free the memory block containing the dropped-file information
    DragFinish(dropInfo);
}

UAC and Elevation

Now comes the interesting part. All of the above will work – unless your application runs elevated. As mentioned earlier, WM_DROPFILES messages are simply filtered out by UIPI. You can re-enable individual messages by calling ChangeWindowMessageFilter (, MSGFLT_ADD), but enabling WM_DROPFILES alone is not sufficient. Here is my first attempt to work around that:

for (int i = 0; i <= WM_DROPFILES; i++)
{
    ChangeWindowMessageFilter (i, MSGFLT_ADD);
}

Hooray! Drag and drop works! But letting all messages through is probably a little bit too much. So I tried to narrow things down.

After some trial and error I came up with the following list of messages you need to allow through the UIPI filter in order to enable drag and drop:

ChangeWindowMessageFilter (WM_DROPFILES, MSGFLT_ADD);
ChangeWindowMessageFilter (WM_COPYDATA, MSGFLT_ADD);
ChangeWindowMessageFilter (0x0049, MSGFLT_ADD);

, , , ,

17 Responses to How to Enable Drag and Drop for an Elevated MFC Application on Vista/Windows 7

  1. Mark March 28, 2010 at 21:32 #

    Thank you. This solution worked for my app.

  2. Tom Deseyn March 31, 2010 at 21:37 #

    Have you tested this on a windows 7 machine? It seems to fail, even with the for-loop approach.

  3. Helge April 1, 2010 at 09:08 #

    Tom,yes, I developed and tested my drag and drop solution on Windows 7 x64.

  4. Tom Deseyn April 2, 2010 at 22:00 #

    I have created a .NET 3.5 application.In the Load event of my form, I put:ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);ChangeWindowMessageFilter(0×0049, MSGFLT_ADD);with:private const uint WM_DROPFILES = 0×233;private const uint WM_COPYDATA = 0x004A;private const uint MSGFLT_ADD = 1;The application has a manifest containing:The application is signed with a 'Trusted Root Certification Authority' certificate and installed in the Program Files folder.Unfortunately the application still does not allow drag and drop from the windows explorer. It does allow drag and drop from other applications that run as administrator.Do you know what is missing, or how I can debug the problem?Thanks in advance,Tom

  5. buzz5 June 15, 2010 at 10:38 #

    To do this without installing the Vista SDK in C++ use the following code:#define MSGFLT_ADD 1extern "C" BOOL ( STDAPICALLTYPE *pChangeWindowMessageFilter )( UINT,DWORD ) = NULL;BOOL CYourApp::InitInstance(){HMODULE hMod = 0; if ( ( hMod = ::LoadLibrary( _T( "user32.dll" ) ) ) != 0 ) { pChangeWindowMessageFilter = (BOOL (__stdcall *)( UINT,DWORD ) )::GetProcAddress( hMod, "ChangeWindowMessageFilter" ); } if ( pChangeWindowMessageFilter ) { pChangeWindowMessageFilter (WM_DROPFILES, MSGFLT_ADD); pChangeWindowMessageFilter (WM_COPYDATA, MSGFLT_ADD); pChangeWindowMessageFilter (0×0049, MSGFLT_ADD);} // rest of init instance here}

  6. Galian September 9, 2010 at 17:40 #

    Thank you for the posting,but it's not working for me on Windows 7 (x64). Don't know why…Strange thing that my view is working correctly under Windows XP, and only as a Drag-n-Drop source under Windows 7, but not as a destination… And ChangeWindowMessageFilter return TRUE

  7. Thomas January 3, 2011 at 12:28 #

    Hi Helge,

    thanks for the post, but it doesn’t seem to work for (Windows 7 64). ChangeWindowMessageFilter returns true, but Drag and drop still isn’t allowed. I even tried allowing all messages from 0 to 0×4000. No change.

    Any Ideas?

    • Helge January 3, 2011 at 13:30 #

      All I can say is it works for me on Windows 7 x64. Sorry I cannot be more helpful.

      • stentor January 24, 2012 at 00:11 #

        Maybe you already know that ChangeWindowMessageFilterEx is the future-safe method for Win7.

  8. MJ June 23, 2011 at 20:14 #

    Thanks. It worked for the most part. However, it appears to cause the app to crash on Windows XP. I believe the problem occurs because the method is not available on XP. Attempting to bypass the lines on XP using an
    if ( osversion >= vista ) {
    ChangeWindowMessageFilter (WM_DROPFILES, MSGFLT_ADD);
    ChangeWindowMessageFilter (WM_COPYDATA, MSGFLT_ADD);
    ChangeWindowMessageFilter (0×0049, MSGFLT_ADD);
    }
    Seemed obvious but it didn’t actually help. If you know of a solution that will work for XP Vista and Windows 7 by working around this limitation please let me know. Thanks again.

    • Helge June 25, 2011 at 11:33 #

      You need to do this:

      // ChangeWindowMessageFilter
      typedef BOOL (WINAPI *PFN_CHANGEWINDOWMESSAGEFILTER) (UINT, DWORD);

      HMODULE hModule = GetModuleHandle (TEXT(“user32.dll”));
      PFN_CHANGEWINDOWMESSAGEFILTER pfnChangeWindowMessageFilter = (PFN_CHANGEWINDOWMESSAGEFILTER) GetProcAddress (hModule, “ChangeWindowMessageFilter”);

      Then you can use it like this:

      (*pfnChangeWindowMessageFilter) (WM_DROPFILES, MSGFLT_ADD);

  9. Stefano Bordoni August 14, 2011 at 11:15 #

    On my Win7_x86 + UAC, this fix works on my RAD Studio XE application.

    (i had linked this API dynamically since it has to run on WinXP too).

    Really nice work.

    Thank you

  10. Jurysko May 21, 2012 at 00:21 #

    Aaaa great! works like charm, thanks :)

  11. Mat January 11, 2013 at 04:20 #

    This does not work for OLE drag&drop.

  12. Sebastiaan January 16, 2013 at 16:50 #

    Hi,

    This does not (yet) work for Windows 8. Does anyone have a solution for this ?

    • Peter June 3, 2014 at 20:20 #

      Sebastiaan,

      Did you ever find out wrt windows 8 ?

  13. c00000fd January 24, 2013 at 10:59 #

    There’s one more restriction applicable to IE. It’s discussed here:
    http://stackoverflow.com/questions/14470665/drag-and-drop-from-ie-9-into-my-application

Leave a Reply