怎么不对啊
Public Class Form1
Public Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal hwnd As String, ByVal lpText As String) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim jb As Long
jb = FindWindow( "SciCalc ", vbNullString)
TextBox1.Text = jb.ToString
End Sub
End Class
不管我开没开计算器,总是返回串数字
[解决办法]
我认为这段代码在VB.NET下不能正常运行,因为VB中的Long在.NET中相当于Int32,所以您的数据类型可能写错了.我试了试,好像只有这样写才得到了计算器的句柄:
Public Class Form1
Public Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal hwnd As String, ByVal lpText As String) As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim jb As Int32
jb = FindWindow( "SciCalc ", Nothing)
TextBox1.Text = jb.ToString
End Sub
End Class
VB中的vbNullString在.NET中是Nothing.
[解决办法]
Alias,FindWindow 是 FindWindowA 的别名,FindWindowA 是 Unicode API 中的实名
以下是MS官方文档的说明:
Win32 API中,为了在支持Unicode的同时保持对ANSI的支持,许多API都定义了两个原型,分别以W结尾和A结尾。
如LoadLibraryW和LoadLibraryA。
事实上,在定义LoadLibrary的头文件Winbase.h中,有如下定义
#ifdef UNICODE
#define LoadLibrary LoadLibraryW
#else
#define LoadLibrary LoadLibraryA
#endif // !UNICODE
因此,当您的程序中定义了UNICODE宏时,您的LoadLibrary就相当于LoadLibraryW;相反,如果没有定义UNICODE宏,则程序中的LoadLibrary就相当于LoadLibraryA。