tags:

views:

62

answers:

4

the code to invoke Internet explorer is as follows

 System.Diagnostics.Process oProcess = new System.Diagnostics.Process();
                oProcess.StartInfo.FileName = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
                oProcess.Start();

is it possible to assign a URL to this process once its started?

+1  A: 
Process.Start(
    "C:\\Program Files\\Internet Explorer\\iexplore.exe", 
    "http://www.google.com"
);

or to open with the default browser:

Process.Start("http://www.google.com");
Darin Dimitrov
I think he means after it has already been started.
adriaanp
Once it is started you will need COM interop. For this case I would recommend using the `System.Windows.Forms.WebBrowser` control instead of spawning a new process manually.
Darin Dimitrov
@adriaanp yes I meant how to navigate to a URL after the IExplore is laucnhed using Process
balalakshmi
A: 

Yes, you can do that. You have to provide URL as parameter. Have a look at this.

Process.Start("IExplore.exe", "www.northwindtraders.com");
Sharique
A: 

This looks possible by using Interop. Take a look at this codeproject project it does what you want to do.

adriaanp
A: 

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);
    }
}
kicsit