从用户进程拖放到提升的进程
我有一个 C# 应用程序,需要使用 runas 进行提升才能运行某些功能。我现在遇到的问题是,我附加了拖放功能的图片框在运行后无法正常工作。我整晚都在看帖子,所以我很熟悉原因。我还找到了很多有关通过过滤器接受这些消息的信息。 http://msdn.microsoft.com/en- us/library/ms632675(v=VS.85).aspx
我还没有找到,我希望有人可以帮助我,是在代码中如何以及在何处实现这一点。我一直在尝试各种方法,但无法使其发挥作用。这是我尝试让它工作的代码片段。顺便说一句,我也尝试使用清单中的过滤器进行更改,但也没有去那里。预先感谢任何人可以提供的帮助。
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr ChangeWindowMessageFilter(uint message, uint dwFlag);
public Form1()
{
InitializeComponent();
ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
ChangeWindowMessageFilter(0x0049, MSGFLT_ADD);
GetAndDisplayRights();
}
private const uint WM_DROPFILES = 0x233;
private const uint WM_COPYDATA = 0x004A;
private const uint WM_COPYGLOBALDATA = 0x0049;
private const uint MSGFLT_ADD = 1;
I have a C# app that requires elevating with runas for certain functions to run. The problem I now have, is a picturebox that I have a drag/drop attached to is not working after runas. I've been reading posts all night so I'm familiar with the reason. I have also found a lot of info about accepting those messages through the filter. http://msdn.microsoft.com/en-us/library/ms632675(v=VS.85).aspx
What I haven't found, and I hope someone can help me with this, is how and where in the code to implement this. I've been trying various approaches and I can't get it to work. Here is a snippet of code that has my attempt at getting this to work. By the way, I also tried making changes with the filter in the manifest, but no go there as well. Thanks in advance for assistance anyone can offer.
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr ChangeWindowMessageFilter(uint message, uint dwFlag);
public Form1()
{
InitializeComponent();
ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
ChangeWindowMessageFilter(0x0049, MSGFLT_ADD);
GetAndDisplayRights();
}
private const uint WM_DROPFILES = 0x233;
private const uint WM_COPYDATA = 0x004A;
private const uint WM_COPYGLOBALDATA = 0x0049;
private const uint MSGFLT_ADD = 1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,也没有解决方法。 D+D 无法将对象从未提升的进程放入提升的进程中。 UIPI(UAC 的 UI 组件)可以防止这种情况发生。 ChangeWindowMessageFilter() 解决方法不起作用,D+D 不是基于消息的,它使用 COM。 WM_DROPFILES 可以追溯到 Windows 3 并且不再使用。
我怀疑 Windows 的某些未来版本会提供解决方法,但从 Windows 7 开始还不可能。
This is not possible, no workaround for it either. D+D cannot drop an object into an elevated process from an unelevated one. UIPI (the UI component of UAC) prevents this. The ChangeWindowMessageFilter() workaround doesn't work, D+D isn't message based, it uses COM. WM_DROPFILES dates back to Windows 3 and is no longer used.
I suspect some future version of Windows to provide a workaround, it isn't possible yet as of Windows 7.
这是我解决这个问题的方法。
我在表单中嵌入了目录列表和文件列表,并为文件列表设置了鼠标单击侦听器。我将其配置为仅显示图像文件,一旦您单击图像,它就会被设置为图片框图像。这不是拖放,但非常接近。最重要的是,无论应用程序的权限级别如何,它都可以正常工作。
Here's how I worked around this problem.
I embedded a directory list and file list in my form and setup a mouse click listener for the file list. I have it configured to only show image files and as soon as you click an image, it gets set as the picturebox image. It's not drag and drop, but pretty darn close. The most important thing is it works regardless of privilege level of the app.