Recently I was building a small tool to help me do some processing on image files. Naturally this meant it needed to be able to open files, so I added a button that would launch the usual Windows built-in “Open a file” dialog.
That works, but it’s more clicks. Wouldn’t it be great if I could open files via drag-and-drop? Well it turns out this is pretty easy to do: You tell Windows you’d like to receive dragged files, you listen for the relevant notification message and then you query Windows again for info about the dragged files. Pretty simple.
And it works!
Except that some of the time it doesn’t:
Notice how, when I drag the file over my window, it still shows the “You’ve got a file and it can be dropped onto this Window” cursor. It just doesn’t “drop” the file.
Now I don’t have evidence of this but you’ll just have to trust me: My window isn’t receiving any dropped-files notification messages from the OS. This is not a case of my application incorrectly handling a dropped file, as far as my code could tell there just aren’t any dropped files.
This only happens in most cases though. Sometimes I build my program, run it, drag files, and they drop just fine. Then I fiddle with some unrelated code, build, run, drag, no dropping. Very strange.
So I try a sample application for Windows drag-and-drop support. It works just fine. I even try to make the sample application look progressively more similar to my actual application, but the sample application always works while my own program only sometimes works.
I spend 2 whole days trying to figure this out.
Until finally, during one of many “hey internet, please tell me why the hell the OS isn’t doing what it’s meant to” searches, I stumble upon this SuperUser answer. Heavy facepalming follows.
It turns out that if you run something “as admin” in Windows, you won’t be able to drag-and-drop files into that program from somewhere else that wasn’t run as admin. This is to prevent so-called “Shatter attacks” which could result in privilege escalation and you can easily try it at home: Launch Notepad, drag a file into it from Explorer and see that it works. Now launch Notepad as admin (find it in the start menu, right-click and select “Run as administrator”) and notice that dragging files from Explorer no longer works (assuming your instance of Explorer was not itself launched as admin, of course).
I was running into this because at some point in the past I configured my console to run as administrator by default. Most of the time I would launch my program (or the debugger which would in turn launch my program) from the console. So most of the time my program was running as admin (and therefore would not accept drops from Explorer). However occasionally I would test it by clicking on the executable in Explorer itself, meaning no admin privileges and working drag-and-drop functionality.
Well I guess at least it all makes sense in hindsight.
Regardless the point is that now you know that this happens and hopefully if you encounter this issue you won’t spend 2 entire days banging your head against a seemingly-impervious wall of undocumented behaviour.