There are actually two interfaces besides the Callback interface which Geoff Chappell doesn't mention on his site.
However it crashes for me and I have tried everything I can think of
I have tried implementation via Class - it fails.
I have tried to swap the VTable - failes
I have tried to change the INotificationCB to LONG so I can do AddressOf - fails.
It'd crashes on:
Call pITN.RegisterCallback(pINCB)
Here is the VB6 code:
First creation of the instances.
Code:
Public Function ShCreateTrayNotifyWin8(ByRef pITN As ITrayNotifyWin8) As Long
Const sCLSID_TrayNotify As String = "{25DEAD04-1EAC-4911-9E3A-AD0A4AB560FD}"
Const sIID_ITrayNotifyWin8 As String = "{FB852B2C-6BAD-4605-9551-F15F87830935}"
Static CLSID_TrayNotify As GUID
Static IID_ITrayNotify As GUID
CLSIDFromString StrPtr(sCLSID_TrayNotify), CLSID_TrayNotify
CLSIDFromString StrPtr(sIID_ITrayNotifyWin8), IID_ITrayNotify 'IID_ITrayNotify
ShCreateTrayNotifyWin8 = CoCreateInstance(CLSID_TrayNotify, 0, CLSCTX_LOCAL_SERVER, IID_ITrayNotify, pITN)
End Function
Public Function ShCreateTrayNotify(ByRef pITN As ITrayNotify) As Long
Const sCLSID_TrayNotify As String = "{25DEAD04-1EAC-4911-9E3A-AD0A4AB560FD}"
Const sIID_ITrayNotify As String = "{D133CE13-3537-48BA-93A7-AFCD5D2053B4}"
Static CLSID_TrayNotify As GUID
Static IID_ITrayNotify As GUID
CLSIDFromString StrPtr(sCLSID_TrayNotify), CLSID_TrayNotify
CLSIDFromString StrPtr(sIID_ITrayNotify), IID_ITrayNotify 'IID_ITrayNotify
ShCreateTrayNotify = CoCreateInstance(CLSID_TrayNotify, 0, CLSCTX_LOCAL_SERVER, IID_ITrayNotify, pITN)
End Function
Code:
Private Sub Command4_Click()
Dim pITN As ITrayNotifyWin8
Dim pINCB As INotificationCB
Dim lpItem As NOTIFYITEM
'Dim Handle As Long
On Error Resume Next
hr = ShCreateTrayNotifyWin8(pITN)
If hr = S_OK Then
lpItem.dwPreference = PREFERENCE_SHOW_ALWAYS
lpItem.hIcon = 0
lpItem.hWnd = Me.hWnd
lpItem.pszExeName = 0
lpItem.pszTip = 0
lpItem.uID = 1
pITN.SetPreference lpItem
g_hWnd = Me.hWnd
Set pINCB = New NotificationCB
Call pITN.RegisterCallback(pINCB) <---- THIS IS CRASHING EVERY TIME
End If
End Sub
Last edited by nebeln; Jan 18th, 2025 at 08:02 PM.
So it doesn't crash for me. I ran it in tB with WinDevLib;
Code:
Private cTB As cTrayNotifyCB
Private pCookie As Long
Private Sub Command1_Click() Handles Command1.Click
Debug.Print "click"
Set cTB = New cTrayNotifyCB
Dim pUnk As IUnknownUnrestricted
' Set pUnk = New ITrayNotify
CoCreateInstance(CLSID_TrayNotify, Nothing, CLSCTX_LOCAL_SERVER, IID_IUnknown, pUnk)
On Error Resume Next
Dim pT8 As ITrayNotifyWin8
Dim pT As ITrayNotify
Set pT8 = pUnk
If pT8 Is Nothing Then
Debug.Print "No win8"
Set pT = pUnk
pT.RegisterCallback(cTB)
pT.RegisterCallback(Nothing)
Else
Debug.Print "using win8"
pT8.RegisterCallback(cTB, pCookie)
pT8.UnregisterCallback(pCookie)
End If
End Sub
End Class
Class cTrayNotifyCB
Implements INotificationCB
Private Sub INotificationCB_Notify(ByVal uCode As Long, pItem As NOTIFYITEM) Implements INotificationCB.Notify
Dim pszItem As String
pszItem = LPWSTRtoStr(pItem.pszExeName, False)
Dim pszText As String = LPWSTRtoStr(pItem.pszIconText, False)
Debug.Print "Action " & uCode & ", Pref " & pItem.dwUserPref & " on " & pszText & " (" & pszItem & ")"
If (uCode < 0) Or (uCode > 3) Then
Err.ReturnHResult = E_FAIL
End If
End Sub
End Class
Code:
using win8
Action 0, Pref 2 on MyAltice 51e251
Internet access ({F38BF404-1D43-42F2-9305-67DE0B28FC23}\explorer.exe)
Action 0, Pref 2 on Speakers: 73% ({F38BF404-1D43-42F2-9305-67DE0B28FC23}\explorer.exe)
Action 0, Pref 0 on Safely Remove Hardware and Eject Media ({F38BF404-1D43-42F2-9305-67DE0B28FC23}\explorer.exe)
Action 0, Pref 0 on ScreenToGif (C:\util\ScreenToGif.exe)
Action 0, Pref 0 on CPU history: 19.15%
msedgewebview2.exe: 8.68% ({6D809377-6AF0-444B-8957-A3773F02200E}\SystemInformer\SystemInformer.exe)
and so on. I'm using Win10 1809; didn't try modifying the preference.
Ok I see. I tried your solution to use IUnknown in CoCreateInstance and it returns S_OK.
But when I tries to set ITrayNotifyWin8 with IUnknown then VB rasing error 13 Type Mismatch.
And when I tries instead to set ITrayNotify first and it sets but I recive error 98 for the class implementation (Set pINCB = New NotificationCB). Never ever before recieved that type of error.
I compiled it this time and the error 13 was not longer present but it still chrashes. No preferenses set.
I also tried it in tB and it runned without crashing - reported not Win8.
Maybe its not "vb6" friendly?
But initial preferences it crash in tB.