vb.net下可以用API函数的吧,我想问一个findwindow的问题

magicbacon 2007-01-22 08:41:24
以下是代码,很久以前在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也无法得到正确的句柄。怎么回事呢?
...全文
535 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
magicbacon 2007-01-23
  • 打赏
  • 举报
回复
解决,50分够各位瓜分么?
snowfox007 2007-01-23
  • 打赏
  • 举报
回复
注意获得Text的函数中的接收String,在使用 GetWindowText之前先使用Space函数填充.

下面的程序已经通过验证.

Private Sub btnWatermark_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatermark.Click
Dim handle As Integer
Dim strTitle As String
Dim intLen As Integer
handle = GetWindow(Me.Handle, GW_HWNDNEXT)
Do While handle
handle = GetWindow(handle, GW_HWNDNEXT)
intLen = GetWindowTextLength(handle)
strTitle = Space(intLen) '用空格填充
GetWindowText(handle, strTitle, intLen + 1)

Debug.Print(strTitle)
Loop

End Sub
snowfox007 2007-01-23
  • 打赏
  • 举报
回复
这是部分相关的常数定义:

Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3

VB中的声明中的所有的Long应该改为Integer或者Int32,因为VB中的Long的大小为4字节,VB.net中的Integer才为4字节.
snowfox007 2007-01-23
  • 打赏
  • 举报
回复
老兄,你定义的常数错了!!!
Private Const GW_HWNDNEXT = &H1

应该为
Private Const GW_HWNDNEXT = &H2

兔子-顾问 2007-01-23
  • 打赏
  • 举报
回复
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer
Public Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
magicbacon 2007-01-23
  • 打赏
  • 举报
回复
改为Int32到出错处时会溢出,所以说GetWindow无法取到正确的句柄,改成Int64可以运行,但在循环过程中handle的值不发生变化,与Long一样.
Anyway,谢谢楼上大哥!
sx_lxh 2007-01-22
  • 打赏
  • 举报
回复
将long全改为int32 试试。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧