In general, it is not possible to "to hook arguments to an existing process". You'll need some kind of knowledge of the application in question and implement case-by-case solutions.
However, with IE (as in your example) you are in luck, as it can be controlled using COM. See the answer from adriaanp for a clean solution.
Then again, if you happen to be in a hackish mood today, there might be a simpler to implement solution. This is by no means the right way to do this, nor is it guaranteed to work under all circumstances, but I've seen something like this used in production code.
The idea is to use Win API to find the browser window, locate the adress bar and enter your URL - cheap, isn't it?! This way you eliminate all COM dependencies and you are rolling in no time.
Here is some code (there is only one public method DoPopup
, which you call with the URL and some part of the caption of the browser window (you can use other means to get the handle of the browser window if you like)):
static class PopupHelper
{
class SearchData
{
public readonly String Criterion;
public IntPtr ResultHandle;
public SearchData(String criterion)
{
Criterion = criterion;
ResultHandle = IntPtr.Zero;
}
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc callback, ref SearchData data);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr window, EnumWindowsProc callback, ref SearchData data);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr handle, StringBuilder result, int length);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr handle, StringBuilder result, int length);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr windowHandle);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr windowHandle, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
delegate bool EnumWindowsProc(IntPtr hWnd, ref SearchData data);
static IntPtr SearchForWindowByTitle(string title)
{
SearchData searchData = new SearchData(title);
EnumWindows(new EnumWindowsProc(delegate(IntPtr handle, ref SearchData searchDataRef)
{
StringBuilder candidate = new StringBuilder(1024);
GetWindowText(handle, candidate, candidate.Capacity);
if (!candidate.ToString().Contains(searchDataRef.Criterion)) { return true; }
searchDataRef.ResultHandle = handle;
return false;
}), ref searchData);
return searchData.ResultHandle;
}
static IntPtr SearchForChildWindowByClassName(IntPtr parent, string className)
{
SearchData searchData = new SearchData(className);
EnumChildWindows(parent, new EnumWindowsProc(delegate(IntPtr handle, ref SearchData searchDataRef)
{
StringBuilder candidate = new StringBuilder(64);
GetClassName(handle, candidate, candidate.Capacity);
if (candidate.ToString() != searchDataRef.Criterion) { return true; }
searchDataRef.ResultHandle = handle;
return false;
}), ref searchData);
return searchData.ResultHandle;
}
static void NavigateToUrlInExistingBrowserWindow(IntPtr browserWindowHandle, IntPtr addressEditBoxHandle, string url)
{
StringBuilder addressBuilder = new StringBuilder(url);
const uint WM_SETTEXT = 0x000C;
const uint WM_KEYDOWN = 0x0100;
const uint RETURN_KEY = 13;
SendMessage(addressEditBoxHandle, WM_SETTEXT, IntPtr.Zero, addressBuilder);
SendMessage(addressEditBoxHandle, WM_KEYDOWN, new IntPtr(RETURN_KEY), null);
SetForegroundWindow(browserWindowHandle);
}
public static void DoPopup(string url, string captionPart)
{
IntPtr windowHandle = SearchForWindowByTitle(captionPart);
if (windowHandle != IntPtr.Zero)
{
IntPtr addressEditBoxHandle = SearchForChildWindowByClassName(windowHandle, "Edit");
if (addressEditBoxHandle != IntPtr.Zero)
{
NavigateToUrlInExistingBrowserWindow(windowHandle, addressEditBoxHandle, url);
return;
}
}
Process.Start("iexplore", url);
}
}