vb.net下可以用API函数的吧,我想问一个findwindow的问题
以下是代码,很久以前在VB6.0下用过,现在用vb2005。希望能找到Maxthon浏览器的句柄,然后控制窗口的最小化、最大化(这部分代码还没加)。
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Const GW_HWNDNEXT = &H1
Private Sub btnWatermark_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatermark.Click
Dim handle As Long
Dim strTitle As String
Dim intLen As Long
handle = GetWindow(Me.Handle, GW_HWNDNEXT) '出错
Do While handle
handle = GetWindow(handle, GW_HWNDNEXT)
intLen = GetWindowTextLength(handle)
GetWindowText(handle, strTitle, intLen + 1)
If (Microsoft.VisualBasic.Left(strTitle, 9) = "[Maxthon]") Then
Exit Do
End If
Loop
RichTextBox1.Text &= strTitle
End Sub
End Class
当运行到注释“出错”处时出现以下问题:
PInvokeStackImbalance was detected
A call to PInvoke function 'Tester!Tester.Form1::GetWindow' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
侦测到调用堆栈失衡
对函数GetWindow的调用使堆栈失衡。原因可能是"managed PInvoke signature"与"unmanaged target signature"不匹配。确认"PInvoke signature"的调用规则和参数与目标"unmanaged signature"匹配。
不知具体是什么意思?高手能不能从中找到解决办法?我取消了对这个错误的侦测后,GetWindow也无法得到正确的句柄。怎么回事呢?