IPB

Welcome Guest ( Log In | Register )

3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> How To Hook Msn Messenger In Visual Basic, updated tutorial
Stigmata
post Sep 5 2004, 09:45 AM
Post #1


Mess.betterthanyours
Group Icon

Group: Supervisor
Posts: 1,951
Joined: 5-September 04
From: Uk
Member No.: 79



here is a little tutorial, most of the code is based on the code wrote by daniel (http://www.msnfanatic.com)
Written by Stigmata/jackasswanabe
http://www.plasticangels.com
jackasswanabeuser posted imagegmail.com

Please, me and the world don't want a bunch of new addons coming at once. This is for information only and if you do include it in your program, i would like my name in the credits, thank you.

Firstly you need 1 form and 1 module and 2 .dll files which will be included in the zip attachment.

Add 1 label to the form and in the form you will have this code:

CODE

Private Sub Form_Load()
Call SetCBTSHLHook(-1, AddressOf hookfind, 0)
form1.visible = false
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Call SetCBTSHLHook(0, 0, 0)
  Call SetCWPMSGHook(0, 0, 0)
End Sub



In the form load, you are calling the hook to take place. By calling setcbtshlhook, its telling the .dll to find the messenger window class. Also i've added to hide the form because there is no real point of seeing it.

Form unload, its self explainatory, it unloads everything that has taken place.

Then moving on to the module code:

CODE

Option Explicit
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function AppendMenu Lib "user32.dll" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function CreatePopupMenu Lib "user32.dll" () As Long
Private Declare Function InsertMenu Lib "user32.dll" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function GetMenu Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hWnd As Long) As Long

Private Const HCBT_CREATEWND As Long = 3
Private Const HCBT_DESTROYWND As Long = 4
Private Const MF_BYCOMMAND As Long = &H0&
Private Const MF_BYPOSITION As Long = &H400&
Private Const MF_POPUP As Long = &H10&
Private Const MF_STRING As Long = &H0&
Public Const MF_SEPARATOR = &H800&
Public Const MF_DISABLED = &H2&
Public Const MF_ENABLED = &H0&
Public Const MF_HILITE = &H80&
Public Const MF_UNHILITE = &H0&
Public Const MF_GRAYED = &H1&
Public Const MF_CHECKED = &H8&
Public Const MF_UNCHECKED = &H0&
Private Const WM_COMMAND As Long = &H111


Basically thats the api calls you need to make for the menu. Normally nothing special about that. You have the mf_popup etc which are the flags for you to choose what type of menu you're using.

Also in the declerations you will need the following:


CODE
Public Declare Function SetCBTSHLHook Lib "dscbtshl" (ByVal Hook As Long, ByVal AdrCBT As Long, ByVal AdrSHL As Long) As Long
Public Declare Function SetCWPMSGHook Lib "dscwpmsg" (ByVal hWnd As Long, ByVal AdrCWP As Long, ByVal AdrMSG As Long) As Long

Public Const Offset& = 2000



The 2 declare functions are declaring the .dll that are needed for the hook
and the const is saying the number which will be needed later to make a menu click do something.

The first function is hookfind:

CODE

Public Function hookfind(ByVal hWnd As Long, ByVal nCode As Long) As Long

  Dim hHiddenWindowClass As Long
  Dim sClassName As String
  Dim lRetVal As Long
  Dim lProcessId As Long
 
  If Not nCode = HCBT_CREATEWND Then        
      If Not nCode = HCBT_DESTROYWND Then
          Exit Function
      End If
  End If

  sClassName = Space(256)
  lRetVal = GetClassName(hWnd, sClassName, 256)
  sClassName = Left$(sClassName, lRetVal)
  lProcessId = GetWindowThreadProcessId(hWnd, 0)
  If sClassName = "MSNHiddenWindowClass" Then
      If nCode = HCBT_CREATEWND Then
          Call OnMessengerStart(lProcessId, hWnd)
      ElseIf nCode = HCBT_DESTROYWND Then
          Call OnMessengerClose(lProcessId, hWnd)
      End If
      Exit Function
  End If
'**********note rest does not work with polygamy***********
  hHiddenWindowClass = FindWindow("MSNHiddenWindowClass", vbNullString)
         
  'If an instance is open
  If Not hHiddenWindowClass = 0 Then
If lProcessId = GetWindowThreadProcessId(hHiddenWindowClass, 0) Then
          If nCode = HCBT_CREATEWND Then
              Call OnWindowOpen(lProcessId, hWnd, sClassName)
          ElseIf nCode = HCBT_DESTROYWND Then
              Call OnWindowClose(lProcessId, hWnd, sClassName)
          End If
      End If
  End If
End Function


Thats basicly readable if you want to know whats happening.
Its looking for msnhiddenwindowclass and when it finds it it places the menu hook on it


CODE
Public Sub OnMessengerStart(ByRef r_lProcessId As Long, ByRef r_hHiddenWnd As Long)
 
Call SetCWPMSGHook(r_hHiddenWnd, 0, AddressOf hookmenu)
Msgbox "messenger opened"  
End Sub

Public Sub OnMessengerClose(ByRef r_lProcessId As Long, ByRef r_hHiddenWnd As Long)
Msgbox "messenger closed"
End Sub



After finding the main window, the hookfind function calls 'onmessengerstart' which calls the menu hook.

Now here is the fun part, you decide what goes where

CODE

Public Sub OnWindowOpen(ByRef r_lProcessId As Long, ByRef r_hWnd As Long, ByRef r_sClassName As String)
 
  Dim hMenu As Long
  Dim hSubMenu As Long
 
  Select Case r_sClassName
      Case "MSNMSBLClass"
          msgbox "messenger main window opened"
          Do
              hMenu = GetMenu(r_hWnd)
          Loop While hMenu = 0
         
          hSubMenu = CreatePopupMenu
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 1, "&Open Log Files")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 2, "&Event Log Window")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 3, "&Lock Messenger")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 4, "S&cheduler...")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 5, "Contact &Manager...")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 6, "&Sounds Settings...")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 7, "&Preferences...")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 8, "&Help")
          Call AppendMenu(hSubMenu, MF_STRING, Offset + 9, "&About")
          Call AppendMenu(hMenu, MF_BYPOSITION Or MF_POPUP, hSubMenu, "&Plus")
          Call DrawMenuBar(r_hWnd)
         
  End Select

End Sub



**sorry patchou, couldn't think of menu names ***

Ok you've have got this far, now its searching for a class name (msnmsblclass, messenger main window classname)
Now you are adding (call appendmenu) to the messenger main window in the form of a menu.

Lets have a look at the appendmenu


CODE
Call AppendMenu(hSubMenu, MF_STRING, Offset + 9, "&Plus")


hsubmenu = where to put it, you define hsubmenu earlier in the sub as long
mf_string = you say what flag it is, eg string so it is used as a string
offset+# = the number of the menu, you see we are using the offset with we declared in the declerations
"&Plus" = the name of the Menu and what text will appear. The "&" is used for the alt + that letter combination like most applications.

Also if you want to add a menu on the side, eg little arrow into another menu then you could do the following, after dim hsubmenu, add dim hsubsubmenu, or something along those lines.
Then repeat at hsubmenu = createpopupmenu by putting after hsubsubmenu = createpopupmenu.
Then in the call append menu put

CODE

call appendmenu(hsubmenu, MF_BYPOSITION Or MF_POPUP, hsubsubMenu, "SUBSUB menu name")

Then to insert into that menu put

CODE
call appendmenu(hsubsubmenu, mf_string, offset + 10, "inside subsubmenu name")


then like repeating code onmessengerclose

CODE

Public Sub OnWindowClose(ByRef r_lProcessId As Long, ByRef r_hWnd As Long, ByRef r_sClassName As String)
 
Select Case r_sClassName
Case "MSNMSBLClass"
Msgbox "main messenger window closed"
End Select
End Sub


then to make the actual manu do something u need the return code


CODE
Public Function hookmenu(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case wMsg
      Case WM_COMMAND
          Debug.Print wParam
If wParam = 2001 Then MsgBox "sorry cant open messenger chat logs......"
If wParam > Offset Then  
  Form1.Label1 = "SubMenu" & Trim$(wParam - Offset) & " was selected"
  Beep
  End If
  End Select
End Function



wparam contains the menu id. where it says 2001. that is the offset plus the menu number.
for each menu item you have just do
if wparam = (2000 + your menu item number) then

so if your menu item was 10, then it would be
wparam = 2010 then msgbox "you clicked item 10"

Download necessary files from the attachment


to make it work with polygamy
and easy way to make it work with polygamy would be to make it into 2 seperate programs.. have the Function hookfind, the Sub OnWindowOpen and nearly everthing else on one program, and the Function hookmenu on another:D

becuase as long as you make the same wparam call it will be picked up msn_grin.gif


Note For Msn Messenger 7 Users:
to make it compatible with msn messenger 7 replace the old main window class name with the new window class name msn_grin.gif

MSNMSBLClass -> ????? (spy++ will help you find it msn_happy.gif )

grats

Stigmata
Attached File(s)
Attached File  dll_files.zip ( 6.12K ) Number of downloads: 1989
 
Go to the top of the page
 
+Quote Post
b0t13
post Sep 28 2004, 07:24 PM
Post #2


Mess.Newscout
***

Group: News Scout
Posts: 191
Joined: 28-September 04
From: Bob's BackGarden
Member No.: 535



thanks for that guys (daniel, stigmata, and whoever else)
ill enjoy playing with that:)
Go to the top of the page
 
+Quote Post
leejeffery
post Nov 6 2004, 07:29 AM
Post #3


Mess.bean
*

Group: Members
Posts: 3
Joined: 5-September 04
Member No.: 77



nice tutorial, any chance of making one for MSN 7 msn_angel.gif
Go to the top of the page
 
+Quote Post
psyko
post Nov 6 2004, 11:08 AM
Post #4


Anti-RIAA Fanatic
******

Group: *Valued Members
Posts: 773
Joined: 27-September 04
From: /usr/bin/root
Member No.: 345



There's no need until the beta becomes a public final release. msn_happy.gif
Go to the top of the page
 
+Quote Post
Stigmata
post Nov 6 2004, 11:58 AM
Post #5


Mess.betterthanyours
Group Icon

Group: Supervisor
Posts: 1,951
Joined: 5-September 04
From: Uk
Member No.: 79



I have updated the tutorial for msn messenger 7 msn_grin.gif

tip: to make it work with both versions as well as instant messages

use multipul cases
Go to the top of the page
 
+Quote Post
SuNcO
post Dec 31 2004, 01:17 AM
Post #6


Windows Live Butterfly
***

Group: Members
Posts: 144
Joined: 31-December 04
From: Mazatlán, Sinaloa, México
Member No.: 4,134



I see that code in another webs and.. works, but i do it the same more short and with one dll, only the dscwpmsg.dll

CODE

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
On Error Resume Next
 
Dim lLength As Long
Dim strCaption As String
Dim lMenuHwnd As Long
Dim lMenuCount As Long

Dim sClassName As String
Dim lRetVal As Long

' Try to get a Menu
lLength = GetWindowTextLength(hwnd)
strCaption = String$(lLength, vbNullChar)
GetWindowText hwnd, strCaption, lLength + 1
lMenuHwnd = GetMenu(hwnd)
lMenuCount = GetMenuItemCount(lMenuHwnd)
   
' Get the class
sClassName = Space(256)
lRetVal = GetClassName(hwnd, sClassName, 256)
sClassName = Left(sClassName, lRetVal)
   
If sClassName Like "*MSNHiddenWindowClass*" Then
 ' Check if i have not the Hwnd yet
 If MSN1.HwndMSNP = "0" Then
  MSN1.HwndMSNP = hwnd
  Call SetCWPMSGHook(CLng(MSN1.HwndMSNP), 0, AddressOf MenuMsn)
 End If
End If

' This is for check the Window.. (check if have Caption, and Menus)
' and have MSN in the Caption, then insert the New Menu
If lLength > 0 And lMenuHwnd > 0 And lMenuCount > 0 Then
 If strCaption Like "*MSN*" Then
  MSN1.HwndMSN = hwnd
  If MSN1.HwndMSN <> MSN1.HwndAnterior Then
    MSN1.HwndAnterior = MSN1.HwndMSN
    InsertMenu
  End If
 End If
End If

EnumWindowsProc = True

End Function


As you see, i only use one function to get the Hwnd of the main MSN Window (the program) and for the MSN Window (for insert the menu)

For hook : Call SetCWPMSGHook(CLng(MSN1.HwndMSNP), 0, AddressOf MenuMsn)
For insert the menu : InsertMenu

One problem in the code of Stigmata is than the hook only works when MSN is not runned... my code works anyway

MSN1.HwndMSNP, MSN1.HwndMSN and MSN1.HwndAnterior are Labels init to "0" (of course, can be Vars)

I'm new in this forum, i hope learn from the experts =)

For Admins : If i put a [B] inside a [CODE] not work

This post has been edited by SuNcO: Dec 31 2004, 01:29 AM
Go to the top of the page
 
+Quote Post
mark87
post Jan 16 2005, 10:11 AM
Post #7


Mess.bedbug
*

Group: Members
Posts: 46
Joined: 16-January 05
Member No.: 4,564



Hi, I'm having a little problem with the code posted by Stigmata, I would like to add another menu item to the one in a convo window (next to your own display pic).

Also, couldn't get SuNcO's working - expected a sub/function with 'AddressOf MenuMsn'.

Any help appreciated! msn_tongue.gif
Go to the top of the page
 
+Quote Post
Stigmata
post Jan 16 2005, 11:06 AM
Post #8


Mess.betterthanyours
Group Icon

Group: Supervisor
Posts: 1,951
Joined: 5-September 04
From: Uk
Member No.: 79



to sunco :
i only use the other dll so that i can find out when the messenger windows have been opened.


mark87:
i think i mite write a tutorial on how to do it, like msgplus's button in a contact window.
keep your eyes on the forum. msn_happy.gif
Go to the top of the page
 
+Quote Post
mark87
post Jan 16 2005, 11:23 AM
Post #9


Mess.bedbug
*

Group: Members
Posts: 46
Joined: 16-January 05
Member No.: 4,564



Thanks, looking forward to it! msn_tongue.gif
Go to the top of the page
 
+Quote Post
hmaster
post Jan 16 2005, 11:28 AM
Post #10


Mess.beanbagfoo
Group Icon

Group: *Valued Members
Posts: 4,733
Joined: 27-September 04
From: UK
Member No.: 237



wohoo..
u gonna write 2?

one for the contactlist window and one for the convo window
Go to the top of the page
 
+Quote Post
Stigmata
post Jan 16 2005, 06:09 PM
Post #11


Mess.betterthanyours
Group Icon

Group: Supervisor
Posts: 1,951
Joined: 5-September 04
From: Uk
Member No.: 79



well here it is msn_happy.gif

http://forum.mess.be/index.php?showtopic=4318
Go to the top of the page
 
+Quote Post
ninj4
post Feb 28 2005, 12:07 AM
Post #12


Mess.bean
*

Group: Members
Posts: 7
Joined: 28-February 05
Member No.: 5,910



Erm have attempted so many times to do this with some luck, is there any chance that we can actually have the window thingie for messenger 7.0.0604 because i realy want to ad my program to messenger, cheers msn_thinking.gif
Go to the top of the page
 
+Quote Post
Volv
post Feb 28 2005, 05:54 AM
Post #13


Mess.bemused
*****

Group: Members
Posts: 506
Joined: 8-October 04
Member No.: 1,272



Main Window/Contact List
MSN6: "MSNMSBLClass"
MSN7: "MSBLWindowClass"

Conversation Window
Both: "IMWindowClass"

This post has been edited by Volv: Feb 28 2005, 05:55 AM
Go to the top of the page
 
+Quote Post
ninj4
post Feb 28 2005, 12:18 PM
Post #14


Mess.bean
*

Group: Members
Posts: 7
Joined: 28-February 05
Member No.: 5,910



QUOTE(Volv @ Feb 28 2005, 05:54 AM)
Main Window/Contact List
MSN6: "MSNMSBLClass"
MSN7: "MSBLWindowClass"

Conversation Window
Both: "IMWindowClass"
[right][snapback]41927[/snapback][/right]


thankyou soo much msn_grin.gif

QUOTE(ninj4 @ Feb 28 2005, 12:18 PM)
thankyou soo much  msn_grin.gif
[right][snapback]41948[/snapback][/right]


ERm only one more problem, when i click the menus they dont do anything, this is the problem i have encounterd before with msn 6. msn_confused.gif ive copied the code right and got the wparam thingie correct but when i click the button nuthin happens msn_sad.gif
Go to the top of the page
 
+Quote Post
Volv
post Mar 1 2005, 02:27 AM
Post #15


Mess.bemused
*****

Group: Members
Posts: 506
Joined: 8-October 04
Member No.: 1,272



QUOTE(ninj4 @ Feb 28 2005, 11:32 PM)
ERm only one more problem, when i click the menus they dont do anything, this is the problem i have encounterd before with msn 6. msn_confused.gif ive copied the code right and got the wparam thingie correct but when i click the button nuthin happens msn_sad.gif
[right][snapback]41951[/snapback][/right]

Have you set a certain action to happen when the user clicks on one of the menu items?

CODE
Public Function hookmenu(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 Select Case wMsg
     Case WM_COMMAND
         Debug.Print wParam
If wParam = 2001 Then MsgBox "sorry cant open messenger chat logs......"
If wParam > Offset Then  
 Form1.Label1 = "SubMenu" & Trim$(wParam - Offset) & " was selected"
 Beep
 End If
 End Select
End Function
Go to the top of the page
 
+Quote Post
ninj4
post Mar 1 2005, 12:23 PM
Post #16


Mess.bean
*

Group: Members
Posts: 7
Joined: 28-February 05
Member No.: 5,910



cheers people code is working and my prog will be finished soon msn_happy.gif thankyou soo much, keep uo the good work.. also is there any chance you can change my personal post count?? instead of mess bean id like Dead Msn Devloper if its atall possible???
Go to the top of the page
 
+Quote Post
john
post Mar 20 2005, 02:49 PM
Post #17


Mess.bedbug
*

Group: Members
Posts: 26
Joined: 21-January 05
Member No.: 4,710



Very interesting idea. But I noticed a problem with the DLL.

If you try to hook more than twice (that includes hooking once and then unhooking), you can't seem to hook a 3rd time.

Did one of you guys make this DLL?
Go to the top of the page
 
+Quote Post
Stigmata
post Mar 20 2005, 04:39 PM
Post #18


Mess.betterthanyours
Group Icon

Group: Supervisor
Posts: 1,951
Joined: 5-September 04
From: Uk
Member No.: 79



there is the read me for dscwpmsg.dll

it was created by the same ppl i think so..
Attached File(s)
Attached File  dscwpmsg.txt ( 6.99K ) Number of downloads: 767
 
Go to the top of the page
 
+Quote Post
john
post Mar 20 2005, 06:59 PM
Post #19


Mess.bedbug
*

Group: Members
Posts: 26
Joined: 21-January 05
Member No.: 4,710



Doesn't seem to document the problem. It is rather serious, as it means that the hook won't work if the user of the program restarts their Messenger.

Give it a try.
Go to the top of the page
 
+Quote Post
Stigmata
post Mar 20 2005, 07:43 PM
Post #20


Mess.betterthanyours
Group Icon

Group: Supervisor
Posts: 1,951
Joined: 5-September 04
From: Uk
Member No.: 79



recently there have been some problems with messenger 7

read up here:

http://forums.msnfanatic.com/index.php?showtopic=10087
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic

 



RSS Lo-Fi Version Time is now: 28th May 2018 - 05:22 AM
© 2004-2008 Seb Skuse, mess.be.
MessForum v6.1.4 - created for, and for use only by mess.be.