47

I have had a project for quite a while using C# winforms. I implemented a drag-drop function before windows 7 was released. Worked like a charm. However, when using windows 7 it does not work. The event doesn't even get triggered.

AllowDrop is set to true. When subscribing to DragEnter it does not get called in windows 7 (not sure about vista). But on XP it works all the way. The program is run with administritave priviliges.

Is there any difference in the drag drop in windows 7 vs xp? Don't know if it's relevant, but I'm using x64

Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • I had a somewhat similar problem with a new Silverlight project. The drag and drop didn't work, because of the security features of my browser. Are you using a browser control in your winforms? – Tim May 14 '10 at 11:25
  • 2
    I've noticed that when running applications with Elevated Permissions in Windows 7, drag & drop gets disabled. – sshow May 14 '10 at 11:27

6 Answers6

79

The source and target processes need to have compatible security levels/privileges. For example, if your source is Explorer and it is running with user level privileges, but your target application is running with administrator (elevated) level permission, you will not be able to drag&drop as this is seen as a security issue as the target is running with a higher level of privileges.

Owen
  • 7,494
  • 10
  • 42
  • 52
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • That seems correct. I tried running IE with administrative priviliges and dragdropping from IE and it worked. Makes sense too, however annoying – Oskar Kjellin May 14 '10 at 11:39
  • 4
    Should provide motivation to change what you need to change so your app can run as standard user. Eg don't write to program files, to HKLM etc. – Kate Gregory May 14 '10 at 14:21
  • @Kate The reason for running with elevated rights is that I need to start and stop services :) – Oskar Kjellin May 14 '10 at 15:03
  • 2
    @Oskar Kjellin well, if you need drag and drop still, partition off the needs-elevation parts and have them launch as a separate elevated process so that the main part can be standard user. – Kate Gregory May 14 '10 at 15:56
  • @Kate Thanks. I'm going to look into if I perhaps can see from the service (it's my service) that the program has started and then pause the service. In that case I do not have to start and stop the service from the program – Oskar Kjellin May 14 '10 at 16:15
  • Strange, but this does not work for me even without requesting admin rights. I downloaded http://www.codeproject.com/KB/shell/Explorer_Drag_Drop/ExplorerDragAndDrop.zip and run under regular rights. I am trying to drag files over from Windows Explorer run under user rights but the application does not allows for drop. – Alex Jul 30 '10 at 17:14
  • 10
    Too bad Windows doesn't tell the user it is doing this. As far as the user (and developer!) can tell, the app just doesn't support Drag & Drop like it used to. My app can run as a normal user but I run VS as admin so that it can register COM DLLs. – Qwertie Oct 15 '10 at 23:45
  • How do you set up your app so that it only has user level privileges? How do you do that for an app ran in debug mode from Visual Studio? – wip Nov 13 '13 at 09:17
  • Tip of the day. If you can't run Visual Studio without administrative priviliges, you can use the openfiledialog of Notepad instance, running with administrative right, as an "elevated" file explorer. – Marco Guignard May 29 '19 at 13:52
18

It is called UIPI, User Interface Privilege Isolation. Designed to prevent input injection exploits from programs that run with restricted privileges. It can be disabled, you'll need to do this:

  • Modify the manifest, set the uiAccess attribute for the <requestedExecutionLevel> element to true.
  • Store your program's EXE in a subdirectory of c:\windows or c:\program files
  • Sign your EXE with a certificate from an valid code signing authority

Never actually tried this, ymmv.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

It may be unrelated, but I was having whacky Drag-n-Drop issues with Windows 8. It would work for a while and then stop working. I couldn't drag-n-drop between Explorer, in an editor, etc. It turns out that when I interacted with a Hyper-V VM running windows 7, my drag-n-drop ability was altered (or perhaps it was the ctrl-alt-end keystroke to simulate crtl-alt-delete to Hyper-V). In any event, one the issue occurred, the following resolved it:

I found the resolution to my problem here: Fix Drag Drop Functionality Not Working In Windows 7 Vista XP

Basically the solution was:

Left-Click on a file in explorer, and while holding down the mouse button, press [Esc] then [Ctrl], then release the mouse button. I have to assume that this is resetting some accessibility setting or something.

Metro
  • 1,464
  • 14
  • 24
1

From your application, call ChangeWindowMessageFilter with the following values to allow dragging and dropping to/from your elevated application and non-elevated applications like Explorer:

ChangeWindowMessageFilter (WM_DROPFILES, MSGFLT_ADD);
ChangeWindowMessageFilter (WM_COPYDATA, MSGFLT_ADD);
ChangeWindowMessageFilter (0x0049, MSGFLT_ADD);
dmex
  • 464
  • 4
  • 4
1

A Minor addition to dmex's post. The following defines the variables and the constant.

private const uint WM_DROPFILES = 0x233;
private const uint WM_COPYDATA = 0x004A;
private const uint WM_COPYGLOBALDATA = 0x0049;
private const uint MSGFLT_ADD = 1;

Also, you may want to consider using ChangeWindowMessageFilterEx if you're application is on Windows 7. I also believe that OLE drag and drop may not use Windows messaging. So it wouldn't effect that at all.

1

I had same problem when debugging a desktop C# application from VS 2015 and Windows 7 x64 as S.O. It was due to the administrator permission applied over the shortcut of my VS IDE in the desktop (see screenshot). Unchecking this option Drag & Drop events raise properly.

Thanks to TimLloyd for his help.

enter image description here

mggSoft
  • 992
  • 2
  • 20
  • 35