<code> Vad är ditt syfte med din hook? KAn du inte subclassa fönstret istället? Nja en hook passar bättre. Någon som ser vad felet kan vara? Kan posta hela koden om det skulle hjälpa. Vet inte om detta kan vara något att arbeta vidare med. Denna rutin hookar ju upp sig själv så att säga. Min kod funkar för att göra detta också men det jag inte får att funka är när jag försöker installera den på en tråd som tillhör en externapplikation som ex. vis notepad.Får inte min hook att funka.
Public Sub SetWndProcHook()
Dim temp2 As Long
temp2 = &H1052A //Bara för testningssyfte. Byter denna till rätt varje gång.
If IsHooked Then
MsgBox "Don''''''''t hook CALLWNDPROC twice or you will be unable to unhook it."
Else
hHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf WndProc, 0, temp2)
IsHooked = True
End If
End Sub
</code>
När jag kör följande kod så retuners 0 till hHook. Min temp 2 är en giltig thredid som jag kollat upp i Spy++. Vad har jag gjort fel?
Kan man även skicka med hwnd i temp2 eller måste det vara en thredid? Om det måste vara en threadid hur ska jag då bära mig åt om jag vill övervaka enbart ett window? Har läst att det ska gå att göra med denna hook.
MVH HenrikSv: Får inte min hook att funka.
Sv: Får inte min hook att funka.
Sv: Får inte min hook att funka.
<codevb>
''''''''put in declarations part of a form
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Sub Form_Load()
Dim x As Long, staticx As Long, MyStr As String
x = FindWindow("#32770", vbNullString)
staticx = FindWindowEx(x, 0&, "static", vbNullString)
MyStr = String(100, Chr$(0))
GetWindowText staticx, MyStr, 100
MyStr = Left$(MyStr, InStr(MyStr, Chr$(0)) - 1)
MsgBox MyStr
End Sub
</code>
Starta sen en ny instans av vb och i form_load bara skriv msgbox "Tjenare", sen kör du detta program. Då kommer en messagebox att visas med den texten du angav i det andra programmet om allt fungerar som det är tänkt.
En annan kod är att centrera en messagebox och det kanske också kan vara intressant.
<codevb>
''''misc API constants
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
''''UDT for passing data through the hook
Private Type MSGBOX_HOOK_PARAMS
hwndOwner As Long
hHook As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
''''need this declared at module level as
''''it is used in the call and the hook proc
Private mhp As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function MessageBox Lib "user32" _
Alias "MessageBoxA" _
(ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As Long) As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Function MoveWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, _
lpRect As RECT) As Long
Public Function Msgbox(sPrompt As String, _
Optional dwStyle As Long, _
Optional sTitle As String) As Long
''''replaces VB''''s built in MsgBox function in VB5/6
Dim hInstance As Long
Dim hThreadId As Long
If dwStyle = 0 Then dwStyle = vbOKOnly
If Len(sTitle) = 0 Then sTitle = "VBnet Messagebox Demo"
''''Set up the hook
hInstance = GetWindowLong(Form1.hwnd, GWL_HINSTANCE)
hThreadId = GetCurrentThreadId()
''''set up the MSGBOX_HOOK_PARAMS values
''''By specifying a Windows hook as one
''''of the params, we can intercept messages
''''sent by Windows and thereby manipulate
''''the dialog
With MHP
.hwndOwner = Form1.hwnd
.hHook = SetWindowsHookEx(WH_CBT, _
AddressOf MsgBoxHookProc, _
hInstance, hThreadId)
End With
''''call the MessageBox API and return the
''''value as the result of this function
Msgbox = MessageBox(Form1.hwnd, sPrompt, sTitle, dwStyle)
End Function
Public Function MsgBoxHookProc(ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim rc As RECT
''''temporary vars for demo
Dim newLeft As Long
Dim newTop As Long
Dim dlgWidth As Long
Dim dlgHeight As Long
Dim scrWidth As Long
Dim scrHeight As Long
Dim frmLeft As Long
Dim frmTop As Long
Dim frmWidth As Long
Dim frmHeight As Long
Dim hwndMsgBox As Long
''''When the message box is about to be shown,
''''centre the dialog
If uMsg = HCBT_ACTIVATE Then
''''in a HCBT_ACTIVATE message, wParam holds
''''the handle to the messagebox
hwndMsgBox = wParam
''''Just as was done in other API hook demos,
''''position the dialog centered in the calling
''''parent form
Call GetWindowRect(hwndMsgBox, rc)
frmLeft = Form1.Left \ Screen.TwipsPerPixelX
frmTop = Form1.Top \ Screen.TwipsPerPixelY
frmWidth = Form1.Width \ Screen.TwipsPerPixelX
frmHeight = Form1.Height \ Screen.TwipsPerPixelX
dlgWidth = rc.Right - rc.Left
dlgHeight = rc.Bottom - rc.Top
scrWidth = Screen.Width \ Screen.TwipsPerPixelX
scrHeight = Screen.Height \ Screen.TwipsPerPixelY
newLeft = frmLeft + ((frmWidth - dlgWidth) \ 2)
newTop = frmTop + ((frmHeight - dlgHeight) \ 2)
Call MoveWindow(hwndMsgBox, newLeft, newTop, dlgWidth, dlgHeight, True)
''''done with the dialog so release the hook
UnhookWindowsHookEx MHP.hHook
End If
''''return False to let normal
''''processing continue
MsgBoxHookProc = False
End Function
</code>
För att testa denna rutin använder du något i stil med:
<codevb>
Option Explicit
Private Sub Command1_Click()
''''Display the API message box
Dim sTitle As String
Dim sPrompt As String
Dim dwStyle As Long
sTitle = "VBnet MessageBox Hook Demo"
sPrompt = "This is a demo of the MessageBox API showing how to hook" & vbCrLf & _
"the dialog and centre it with respect to the parent form."
dwStyle = vbAbortRetryIgnore Or vbInformation
Select Case Msgbox(sPrompt, dwStyle, sTitle)
Case vbRetry: Text1.Text = "Retry button pressed"
Case vbAbort: Text1.Text = "Abort button pressed"
Case vbIgnore: Text1.Text = "Ignore button pressed"
End Select
End Sub
</code>Sv: Får inte min hook att funka.
Vet inte om jag skickar med thredid i temp2 på fel sätt eller vad det kan vara.