繁体   English   中英

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

[英]c# : is it possible to hook arguments to an existing process. Example launching iexplore.exe and then making it to navigate to a website

调用Internet Explorer的代码如下

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

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

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

或使用默认浏览器打开:

Process.Start("http://www.google.com");

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

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

通过使用Interop,这看起来可能实现。 看一下这个代码项目项目,它可以完成您想做的事情。

通常,不可能“将参数挂接到现有进程”。 您将需要对所涉及的应用程序有某种了解,并实施逐案解决方案。

但是,使用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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM