简体   繁体   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

the code to invoke Internet explorer is as follows 调用Internet Explorer的代码如下

 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? 启动后是否可以为该进程分配URL?

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

Yes, you can do that. 是的,你可以这么做。 You have to provide URL as parameter. 您必须提供URL作为参数。 Have a look at this . 看看这个

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

This looks possible by using Interop. 通过使用Interop,这看起来可能实现。 Take a look at this codeproject project it does what you want to do. 看一下这个代码项目项目,它可以完成您想做的事情。

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. 但是,使用IE(如您的示例),您很幸运,因为可以使用COM对其进行控制。 See the answer from adriaanp for a clean solution. 请参阅adriaanp的答案以获取干净的解决方案。

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?! 想法是使用Win API查找浏览器窗口,找到地址栏并输入您的URL-便宜,不是吗? This way you eliminate all COM dependencies and you are rolling in no time. 这样,您就消除了所有COM依赖关系,并且可以立即滚动。

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)): 这是一些代码(只有一个公共方法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.

相关问题 C#-使用iexplore.exe -embeddding导航URL? - c# - navigate url with using iexplore.exe -embedding? 的Process.Start(“IEXPLORE.EXE”); &lt; - 这可靠吗? - Process.Start(“IExplore.exe”); <— Is this reliable? Process.Start(“ IExplore.exe”,“ http://google.com”)未在VM上启动。 在服务器和本地上工作 - Process.Start(“IExplore.exe”, “http://google.com”) Not Launching On VM. Works on Server and Local Process.Kill似乎不适用于iexplore.exe - Process.Kill does not seem to work with iexplore.exe Process.Start("IExplore.exe"); --&gt; System.IO.FileNotFoundException - Process.Start("IExplore.exe"); --> System.IO.FileNotFoundException IEXPLORE.exe的奇怪的ProcessorAffinity问题 - Strange ProcessorAffinity issue with IEXPLORE.exe Process.Start(“IEXPLORE.EXE”)在启动后立即触发退出事件..为什么? - Process.Start(“IEXPLORE.EXE”) immediately fires the Exited event after launch.. why? 在外部过程中获取对象。 可能吗? (C#) - Get object in external process. Is it possible? (c#) C#在子文件夹中使用Process.Start for exe在与启动exe相同的文件夹中启动 - C# using Process.Start for exe in a sub folder launches in same folder as launching exe 通过C#进程使用cmd.exe的多个参数 - Multiple arguments with cmd.exe via a C# Process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM