Results 1 to 7 of 7

Thread: Trouble with getting ITrayNotify to work.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    945

    Trouble with getting ITrayNotify to work.

    I have tried all day long to get ITrayNotify to work but I fails misserable
    I run VB6 on a Win11 Home Edition machine.

    The code looks like this (first the undocumented ODL code).
    Code:
      [
        uuid(f9425e51-caac-55c0-95e8-43b7da5d6546),
        version(1.0),
    	helpstring("Undocumented Interfaces - Shell Interfaces for VB6"),
    ]
    library undoc {
    
    // OLE Automation
    importlib("stdole2.tlb");
    
    #include "typdef.odl";
    
        // The known values for NOTIFYITEM's dwPreference member.
        typedef enum NOTIFYITEM_PREFERENCE {
            // In Windows UI: "Only show notifications."
            PREFERENCE_SHOW_WHEN_ACTIVE = 0,
            // In Windows UI: "Hide icon and notifications."
            PREFERENCE_SHOW_NEVER = 1,
            // In Windows UI: "Show icon and notifications."
            PREFERENCE_SHOW_ALWAYS = 2
        }NOTIFYITEM_PREFERENCE;
    
    typedef struct tagNOTIFYITEM
    {
    	PWSTR pszExeName;
    	PWSTR pszTip;
    	HICON hIcon;
    	HWND hWnd;
    	NOTIFYITEM_PREFERENCE dwPreference;
    	UINT uID;
    	GUID guidItem;
    } NOTIFYITEM; 
    typedef NOTIFYITEM *PNOTIFYITEM;
    
    [
        odl,
        uuid(D782CCBA-AFB0-43F1-94DB-FDA3779EACCB),
        helpstring("INotificationCB Interface")
    ]
    interface INotificationCB : stdole.IUnknown {
    
        HRESULT Notify ([in]UINT Event,[in] NOTIFYITEM *Item);
    }
    
    [
        odl,
        uuid(D133CE13-3537-48BA-93A7-AFCD5D2053B4),
        helpstring("ITrayNotify Interface")
    ]
    interface ITrayNotify : stdole.IUnknown {
    
        HRESULT RegisterCallback ([in] INotificationCB *pINotificationCB,[out] ULONG* handle);    
        HRESULT UnRegisterCallback ([in] ULONG Handle); 
        HRESULT SetPreference([in] NOTIFYITEM *Item);
        HRESULT EnableAutoTray([in] BOOL fEnable);
        HRESULT DoAction([in] BOOL fEnabled); 
    }
    
    [
        odl,
        uuid(FB852B2C-6BAD-4605-9551-F15F87830935),
        helpstring("ITrayNotifyWin8 Interface")
    ]
    interface ITrayNotifyWin8 : stdole.IUnknown {
    
        HRESULT RegisterCallback ([in] INotificationCB *pINotificationCB);
        HRESULT SetPreference([in] NOTIFYITEM *Item);
        HRESULT EnableAutoTray([in] BOOL fEnable);
    
    }
    
    };
    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.

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,077

    Re: Trouble with getting ITrayNotify to work.

    Doesn't the new version from win8 have a 2nd arg in registercallback?

    https://thread0.wordpress.com/tag/itraynotify/

    Also I saw your GitHub post... Did you see the comment before yours saying it doesn't work after win10 1703?
    Last edited by fafalone; Jan 18th, 2025 at 07:09 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    945

    Re: Trouble with getting ITrayNotify to work.

    It's the other interface who got the 2nd arg.
    I did read it as it didn't work for win10.
    But it's probably true it's not workable after that release.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,077

    Re: Trouble with getting ITrayNotify to work.

    Well no, it's the Win8 interface.

    Code:
    class __declspec(uuid(&quot;D133CE13-3537-48BA-93A7-AFCD5D2053B4&quot;)) ITrayNotifyWindows8 : public IUnknown
    {
    public:
        virtual HRESULT __stdcall
        RegisterCallback(INotificationCB* callback, unsigned long*) = 0;
        virtual HRESULT __stdcall UnregisterCallback(unsigned long*) = 0;
        virtual HRESULT __stdcall SetPreference(NOTIFYITEM const*) = 0;
        virtual HRESULT __stdcall EnableAutoTray(BOOL) = 0;
        virtual HRESULT __stdcall DoAction(BOOL) = 0;
    };
    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    945

    Re: Trouble with getting ITrayNotify to work.

    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.
    Attached Images Attached Images  

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,906

    Re: Trouble with getting ITrayNotify to work.

    > But when I tries to set ITrayNotifyWin8 with IUnknown then VB rasing error 13 Type Mismatch.

    The complete code above is

    Code:
            On Error Resume Next
            Dim pT8 As ITrayNotifyWin8
            Set pT8 = pUnk
            If pT8 Is Nothing Then ...
    The OERN statement is important.

    cheers,
    </wqw>

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    945

    Re: Trouble with getting ITrayNotify to work.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width