访问被拒绝 - 试图从地址栏的句柄的URL(文字)时, [英] Access is denied - when trying to get the url (text) from address bar's handle

查看:164
本文介绍了访问被拒绝 - 试图从地址栏的句柄的URL(文字)时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从IE浏览器的地址栏中的URL。使用下面的C#代码(在Windows 7中的IE 8)。

I'm trying to extract the URL from the address bar of IE. (IE 8 on Windows 7) using the following C# code.

        static string GetUrlFromIE()
        {
            IntPtr windowHandle = APIFuncs.getForegroundWindow();
            IntPtr childHandle;
            String strUrlToReturn = "";

            //try to get a handle to IE's toolbar container
            childHandle = APIFuncs.FindWindowEx(windowHandle, IntPtr.Zero, "WorkerW", IntPtr.Zero);
            if (childHandle != IntPtr.Zero)
            {
                //get a handle to address bar
                childHandle = APIFuncs.FindWindowEx(childHandle, IntPtr.Zero, "ReBarWindow32", IntPtr.Zero);
                if (childHandle != IntPtr.Zero)
                {
                    childHandle = APIFuncs.FindWindowEx(childHandle, IntPtr.Zero, "Address Band Root", IntPtr.Zero);
                    if (childHandle != IntPtr.Zero)
                    {
                        childHandle = APIFuncs.FindWindowEx(childHandle, IntPtr.Zero, "Edit", IntPtr.Zero);
                        if (childHandle != IntPtr.Zero)
                        {
                            strUrlToReturn = new string((char)0, 256);
                            GetWindowText(hwnd, strUrlToReturn , strUrlToReturn.Length);
                        }
                    }
                 }
            }
            return strUrlToReturn;
        } 



GetWindowText函数调用返回访问被拒绝异常。在运行具有管理员权限的应用程序,它抛出一个系统找不到指定的文件。

The GetWindowText call returns an "Access is denied" exception. On running the app with admin privileges, it throws a "System cannot find the file specified".

任何想法?

推荐答案

GetWindowText函数()无法检索控制在另一个进程中的文字,而应该使用 SendMessage函数() WM_GETTEXTLENGTH / WM_GETTEXT

GetWindowText() can't retrieve the text of a control in another process, instead you should use SendMessage() with WM_GETTEXTLENGTH / WM_GETTEXT.

Edit(编辑)版本无关方式:

Edit; Version agnostic way:

(添加裁判C:\WINDOWS\system32\shdocvw.dll)

(Add a ref to c:\WINDOWS\system32\shdocvw.dll)

using SHDocVw;
.
.
foreach (InternetExplorer ieInst in new ShellWindowsClass())
   Console.WriteLine(ieInst.LocationURL);

这篇关于访问被拒绝 - 试图从地址栏的句柄的URL(文字)时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆