1

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.

Stefan P.
  • 9,489
  • 6
  • 29
  • 43
  • Since the browser is in focus, how about having the plugin open Notepad instead of the C# app doing it? You have a communication channel open, so the app could tell the plugin which filename to open. If the C# app needs any information about the spawned Notepad process, the plugin could send it back to the app. – Remy Lebeau Sep 08 '12 at 00:02
  • @Remy: Other issues arise (see taxilian answer). – David Robert Jones Sep 11 '12 at 15:56

1 Answers1

0

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.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Let's focus on Opera 12.00. We have opera.exe and opera_plugin_wrapper.exe. The plug-in is running inside opera_plugin_wrapper.exe. AllowSetForegroundWindow is returning false (SetForegroundWindow too). opera_plugin_wrapper.exe was started by opera.exe, which is the foreground process. According to the SetForegroundWindow documentation, "A process can set the foreground window only if one of the following conditions is true: The process was started by the foreground process." Therefore SetForegroundWindow should succeed, although it isn't succeeding. Do you know why? – David Robert Jones Sep 10 '12 at 14:48
  • I don't know why; I suspect that something in the way opera starts the process prevents this permission from staying. Browser manufacturers generally don't want you doing what you're trying to do, so... :-/ sorry – taxilian Sep 10 '12 at 22:13
  • Neither do I. Unfortunately it happens in Chrome, Firefox and Opera. GetLastError returns ERROR_ACCESS_DENIED after calling AllowSetForegroundWindow. – David Robert Jones Sep 11 '12 at 15:43