Fetstil Fetstil Kursiv Understrykning linje färgläggning tabellverk Punktlista Nummerlista Vänster Centrerat högerställt Utfyllt Länk Bild htmlmode
  • Forum & Blog
    • Forum - översikt
      • .Net
        • asp.net generellt
        • c#
        • vb.net
        • f#
        • silverlight
        • microsoft surface
        • visual studio .net
      • databaser
        • sql-server
        • databaser
        • access
        • mysql
      • mjukvara klient
        • datorer och komponenter
        • nätverk, lan/wan
        • operativsystem
        • programvaror
        • säkerhet, inställningar
        • windows server
        • allmänt
        • crystal reports
        • exchange/outlook
        • microsoft office
      • mjukvara server
        • active directory
        • biztalk
        • exchange
        • linux
        • sharepoint
        • webbservers
        • sql server
      • appar (win/mobil)
      • programspråk
        • c++
        • delphi
        • java
        • quick basic
        • visual basic
      • scripting
        • asp 3.0
        • flash actionscript
        • html css
        • javascript
        • php
        • regular expresssion
        • xml
      • spel och grafik
        • DirectX
        • Spel och grafik
      • ledning
        • Arkitektur
        • Systemutveckling
        • krav och test
        • projektledning
        • ledningsfrågor
      • vb-sektioner
        • activeX
        • windows api
        • elektronik
        • internet
        • komponenter
        • nätverk
        • operativsystem
      • övriga forum
        • arbete karriär
        • erbjuda uppdrag och tjänster
        • juridiska frågor
        • köp och sälj
        • matematik och fysik
        • intern information
        • skrivklåda
        • webb-operatörer
    • Posta inlägg i forumet
    • Chatta med andra
  • Konto
    • Medlemssida
    • Byta lösenord
    • Bli bonsumedlem
    • iMail
  • Material
    • Tips & tricks
    • Artiklar
    • Programarkiv
  • JOBB
  • Student
    • Studentlicenser
  • KONTAKT
    • Om pellesoft
    • Grundare
    • Kontakta oss
    • Annonsering
    • Partners
    • Felanmälan
  • Logga in

Hem / Forum översikt / inlägg

Posta nytt inlägg


Får inte min hook att funka.

Postades av 2004-04-26 23:15:11 - Henke Larsson, i forum api-windows, Tråden har 4 Kommentarer och lästs av 1435 personer

<code>
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 Henrik


Svara

Sv: Får inte min hook att funka.

Postades av 2004-04-27 01:35:50 - Andreas Hillqvist

Vad är ditt syfte med din hook? KAn du inte subclassa fönstret istället?


Svara

Sv: Får inte min hook att funka.

Postades av 2004-04-27 12:44:34 - Henke Larsson

Nja en hook passar bättre. Någon som ser vad felet kan vara? Kan posta hela koden om det skulle hjälpa.


Svara

Sv: Får inte min hook att funka.

Postades av 2004-04-27 12:52:01 - Pelle Johansson

Vet inte om detta kan vara något att arbeta vidare med.

<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>


Svara

Sv: Får inte min hook att funka.

Postades av 2004-04-27 18:43:06 - Henke Larsson

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.
Vet inte om jag skickar med thredid i temp2 på fel sätt eller vad det kan vara.


Svara

Nyligen

  • 18:42 Hvor finder man håndlavede lamper
  • 18:41 Hvor finder man håndlavede lamper
  • 16:36 Allt du behöver veta om keramiskt
  • 16:14 Vem anlitar man egentligen när tak
  • 16:14 Vem anlitar man egentligen när tak
  • 16:13 Vem anlitar man egentligen när tak
  • 11:52 Noen erfaring med uttak hos Mostbe
  • 11:51 Noen erfaring med uttak hos Mostbe

Sidor

  • Hem
  • Bli bonusmedlem
  • Läs artiklar
  • Chatta med andra
  • Sök och erbjud jobb
  • Kontakta oss
  • Studentlicenser
  • Skriv en artikel

Statistik

Antal besökare:
Antal medlemmar:
Antal inlägg:
Online:
På chatten:
4 570 576
27 958
271 741
5 947
0

Kontakta oss

Frågor runt konsultation, rådgivning, uppdrag, rekrytering, annonsering och övriga ärenden. Ring: 0730-88 22 24 | pelle@pellesoft.se

© 1986-2013 PelleSoft AB. Last Build 4.1.7169.18070 (2019-08-18 10:02:21) 4.0.30319.42000
  • Om
  • Kontakta
  • Regler
  • Cookies