【问题标题】:Get text from javascript alert box in vb.net with user32.dll使用 user32.dll 从 vb.net 中的 javascript 警报框中获取文本
【发布时间】:2018-05-12 06:11:50
【问题描述】:

大家好!

请帮帮我;

我正在尝试使用 user32.dll API 从 javascript 警报框中获取文本,但我无法确定 lpszClass 名称。

如果这是一个愚蠢的问题,请有人帮助我并抱歉。

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInt32, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function

Private Function Form1_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate As String
    Dim hwnd As IntPtr = FindWindow("#32770", "Mensagem da página da web")

    'hwnd = FindWindowEx(hwnd, IntPtr.Zero, "<NEED TO KNOW WHAT TO PUT HERE", Nothing)


    Dim length As Integer = GetWindowTextLength(hwnd)
    Dim sb As New System.Text.StringBuilder("", length + 1)
    GetWindowText(hwnd, sb, sb.Capacity)
   return sb.ToString()

End Function

【问题讨论】:

  • 所以你正在尝试编写一个程序来获取另一个程序(本例中的浏览器)创建的窗口的文本?
  • 附注您的代码格式有点偏离
  • 是的。 Exactly.My 应用程序使用 WebBrowser 控件在网页中执行一些操作。但是会弹出一些警报,我需要获取短信来处理它们。所以我不知道该怎么做。

标签: javascript .net api interop user32


【解决方案1】:

这里是 Windows API 新手,但这是我做类似事情的方式:

VisualStudio 附带一个名为 Spy++ 的工具(位于“工具”菜单下),它列出了系统上的每个“窗口”以及相关信息。您要查找的信息显示在窗口标题之后:

在我的例子中,我需要找到多个具有相同类的孩子,所以我最终使用了 EnumChildWindows 函数而不是 FindWindowEx。这是我的 Java 代码(使用 JNA):

User32 u32 = User32.INSTANCE;
WinDef.HWND window = u32.FindWindow("#32770", "Control Center Login");
List<WinDef.HWND> windows = new ArrayList<>();
u32.EnumChildWindows(window, (child, data) -> {
    char[] name = new char[256];
    u32.GetClassName(child, name, 256);
    String nameStr = new String(name).trim();
     if(nameStr.equals("Edit")) {
        windows.add(child);
    }
    return true;
}, null);

我希望你在过去的 2 年里没有等待这个答案,但是如果其他人发现了这个问题,这里有一个方法:)

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2019-07-23
    • 1970-01-01
    相关资源
    最近更新 更多