访问被拒绝 - 尝试从地址栏句柄获取 url(文本)时

发布于 2024-09-11 05:49:08 字数 1464 浏览 2 评论 0原文

我正在尝试从 IE 地址栏中提取 URL。 (Windows 7 上的 IE 8)使用以下 C# 代码。

        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 调用返回“访问被拒绝”异常。使用管理员权限运行应用程序时,它会抛出“系统找不到指定的文件”。

有什么想法吗?

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

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".

Any ideas?

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

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

发布评论

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

评论(1

一场信仰旅途 2024-09-18 05:49:08

GetWindowText() 无法检索另一个进程中控件的文本,您应该将 SendMessage()WM_GETTEXTLENGTH / 结合使用WM_GETTEXT

编辑;与版本无关的方式:(

添加对 c:\WINDOWS\system32\shdocvw.dll 的引用)

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

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

Edit; Version agnostic way:

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

using SHDocVw;
.
.
foreach (InternetExplorer ieInst in new ShellWindowsClass())
   Console.WriteLine(ieInst.LocationURL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文