AllowSetForegroundWindow & SetForegroundWindow: NPAPI plug-in wants to allow a desktop application with no success

佐手、 提交于 2019-12-08 07:36:45

问题


Here it's what I have: a web browser plug-in written in C++ and a Windows application written in C#. They communicate through a named pipe. The plug-in instructs the C# application to open a file (suppose that the file is a .txt and it opens in Notepad).

Once the C# application is given the command, it opens the file but Notepad doesn't show in the foreground, which isn't acceptable, I must open Notepad in the foreground.

I modified the C# application so that it calls the SetForegroundWindow function. This time Notepad didn't open in the foreground, but the taskbar flashes.

After reading the documentation for SetForegroundWindow and many articles I think that now I understand what the problem is: the C# application can't bring Notepad to the foreground because it wasn't the the foreground process, the browser was (?).

After reading this:

"A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindow function."

I decided to modify the plug-in.

This time the plug-in calls the AllowSetForegroundWindow function passing ASFW_ANY as a parameter (I know, ASFW_ANY could be risky, but I wanted to make sure that AllowSetForegroundWindow would do it).

After I did the modification to the plug-in I tested it and it worked! (Opera 12.02). Then I tested it on Internet Explorer and it worked too. But the problem came when I tested it in Firefox and Chrome. The C# application didn't have the ability to bring Notepad to the foreground. I noticed that for those browsers the AllowSetForegroundWindow function was returning false.

So I started investigating and I come to the conclusion that maybe it's because the plugin container that Firefox uses. An idea came to my mind: it worked in Opera 12.02, but they don't have a plugin container, although they did in Opera 12.00. So I downloaded Opera 12.00, I did the test and it failed, which makes me conclude that the plugin container is the culprit.

The question is: how can I give to the C# application the ability to set foreground?

I don't know how to continue, and I think that I tried all the legitimate ways. The AllowSetForegroundWindow & SetForegroundWindow seems to not apply here.


回答1:


You most likely can't give the C# application that ability from the plugin because the plugin process isn't the foreground process either -- it's actually running in a different process, at least on several of the browsers available to you. On IE you aren't, but you are in a low integrity process so launching the process yourself would require a registry entry specifying that it's okay to launch that application.

I have to agree with you -- I think you have tried all the legitimate ways and it just isn't possible. Heck, even installers often end up opening important windows in the background in recent versions of windows.



来源:https://stackoverflow.com/questions/12323882/allowsetforegroundwindow-setforegroundwindow-npapi-plug-in-wants-to-allow-a-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!