访问被拒绝 - 尝试从地址栏句柄获取 url(文本)时
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GetWindowText()
无法检索另一个进程中控件的文本,您应该将SendMessage()
与WM_GETTEXTLENGTH
/结合使用WM_GETTEXT
。编辑;与版本无关的方式:(
添加对 c:\WINDOWS\system32\shdocvw.dll 的引用)
GetWindowText()
can't retrieve the text of a control in another process, instead you should useSendMessage()
withWM_GETTEXTLENGTH
/WM_GETTEXT
.Edit; Version agnostic way:
(Add a ref to c:\WINDOWS\system32\shdocvw.dll)