先看最终代码:
Declare Function GetwindowText lib "user32.dll" Alias "GetWindowTextA" (ByVal hWin As Long, ByRef pStr As String, ByVal iMax As Long) As Long Declare Function SendMessage lib "user32.dll" Alias "SendMessageA" (ByVal hWin As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Const WM_GETTEXTLENGTH As Long = 14 Function GetText(ByVal hWnd As Long) As String Dim iSize As Long = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) If iSize > 0 Then Dim sRet As String * (iSize + 1) GetwindowText(hWnd, sRet, iSize + 1) Return sRet Else Return "" End If End Function Function Main() As Long TracePrint GetText(23791652) End Function
先通过 WM_GETTEXTLENGTH 消息确定返回字符串的大小,然后创建一个缓冲区用于接收数据,最后使用 GetwindowText 获取窗口标题。
果壳精灵在需要调用以形参(形式参数)返回字符串的API时,在确定了缓冲区大小后,创建一个对应大小的字符串,把字符串传入参数即可获得数据。
在API声明时,形式参数字符串应声明为:ByRef pStr As String