views:

565

answers:

4

I have had a project for quite a while using C# winforms. I implemented a drag-drop function before windows 7 was release. 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

+5  A: 

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.

chibacity
That seems correct. I tried running IE with administrative priviliges and dragdropping from IE and it worked. Makes sense too, however annoying
Oskar Kjellin
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
@Kate The reason for running with elevated rights is that I need to start and stop services :)
Oskar Kjellin
@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
@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
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
Qwertie
+1  A: 

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
+1 Nice answer :) Thanks for clearing out how to solve it. Not worth it for me however :)
Oskar Kjellin
+1  A: 

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
A: 

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.

BlndLeadingDef