c#:是否可以将参数挂钩到现有进程。启动 iexplore.exe 然后使其导航到网站的示例

发布于 2024-09-08 11:37:34 字数 295 浏览 4 评论 0原文

调用 Internet Explorer 的代码如下

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

是否可以在该进程启动后为其分配 URL?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晒暮凉 2024-09-15 11:37:34
Process.Start(
    "C:\\Program Files\\Internet Explorer\\iexplore.exe", 
    "http://www.google.com"
);

或使用默认浏览器打开:

Process.Start("http://www.google.com");
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");
淡笑忘祈一世凡恋 2024-09-15 11:37:34

通过使用 Interop,这看起来是可能的。看看这个 codeproject 项目,它可以完成您想做的事情。

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

自找没趣 2024-09-15 11:37:34

是的,你可以这样做。您必须提供 URL 作为参数。看看这个

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

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

Process.Start("IExplore.exe", "www.northwindtraders.com");
铁憨憨 2024-09-15 11:37:34

一般来说,“将参数挂钩到现有进程”是不可能的。您需要对相关应用程序有一定的了解并实施具体情况的解决方案。

但是,对于 IE(如您的示例),您很幸运,因为它可以使用 COM 进行控制。请参阅 adriaanp 的答案以获得干净的解决方案。

话又说回来,如果您今天碰巧心情不好,可能有一个更容易实现的解决方案。这绝不是执行此操作的正确方法,也不能保证在所有情况下都有效,但我已经在生产代码中看到过类似的方法。

这个想法是使用 Win API 来查找浏览器窗口,找到地址栏并输入您的 URL - 便宜,不是吗?通过这种方式,您可以消除所有 COM 依赖性,并且很快就能开始工作。

这是一些代码(只有一个公共方法DoPopup,您可以使用URL和浏览器窗口标题的一部分来调用它(您可以使用其他方式来获取浏览器窗口的句柄)如果你喜欢)):

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文