VirtualBox

Changeset 105432 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Jul 22, 2024 6:04:56 PM (7 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
164089
Message:

GA/Windows: Code cleanup in Session state tracker. bugref:10714

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSessionTracking.cpp

    r98103 r105432  
    7171            gVBoxSt.fIsConsole = (*pProtocolType == 0);
    7272            gVBoxSt.enmConnectState = *penmConnectState;
     73            LogFlowFunc(("Session information: ProtocolType %d, ConnectState %d", *pProtocolType, *penmConnectState));
    7374            return VINF_SUCCESS;
    7475        }
    75 
     76        else
     77        {
     78            DWORD dwErr = GetLastError();
     79            LogWarnFunc(("WTSQuerySessionInformationA WTSClientProtocolType failed, error = %08X\n", dwErr));
     80            rc = RTErrConvertFromWin32(dwErr);
     81        }
     82    }
     83    else
     84    {
    7685        DWORD dwErr = GetLastError();
    77         LogFlowFunc(("WTSQuerySessionInformationA WTSClientProtocolType failed, error = %08X\n", dwErr));
    78         rc = RTErrConvertFromWin32(dwErr);
    79     }
    80     else
    81     {
    82         DWORD dwErr = GetLastError();
    83         LogFlowFunc(("WTSQuerySessionInformationA WTSConnectState failed, error = %08X\n", dwErr));
     86        LogWarnFunc(("WTSQuerySessionInformationA WTSConnectState failed, error = %08X\n", dwErr));
    8487        rc = RTErrConvertFromWin32(dwErr);
    8588    }
     
    9598{
    9699    RT_ZERO(gVBoxSt);
    97     int rc = RTLdrLoadSystem("WTSAPI32.DLL", false /*fNoUnload*/, &gVBoxSt.hLdrModWTSAPI32);
     100    int rc = RTLdrLoadSystem("WTSAPI32.DLL", true /*fNoUnload*/, &gVBoxSt.hLdrModWTSAPI32);
     101
    98102    if (RT_SUCCESS(rc))
    99103    {
     
    101105                            (void **)&gVBoxSt.pfnWTSRegisterSessionNotification);
    102106        if (RT_SUCCESS(rc))
    103         {
    104107            rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSUnRegisterSessionNotification",
    105                                 (void **)&gVBoxSt.pfnWTSUnRegisterSessionNotification);
    106             if (RT_SUCCESS(rc))
     108                            (void **)&gVBoxSt.pfnWTSUnRegisterSessionNotification);
     109        if (RT_SUCCESS(rc))
     110            rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSQuerySessionInformationA",
     111                             (void **)&gVBoxSt.pfnWTSQuerySessionInformationA);
     112
     113        AssertRC(rc);
     114        RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
     115    }
     116
     117    if (RT_SUCCESS(rc))
     118    {
     119        gVBoxSt.hWTSAPIWnd = hWnd;
     120        if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
     121        {
     122            LogFunc(("hWnd(%p) registered for WTS session changes\n", gVBoxSt.hWTSAPIWnd));
     123            vboxStCheckState();
     124        }
     125        else
     126        {
     127            DWORD dwErr = GetLastError();
     128            LogRel(("WTSRegisterSessionNotification failed, error = %08X\n", dwErr));
     129            if (dwErr == RPC_S_INVALID_BINDING)
    107130            {
    108                 rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSQuerySessionInformationA",
    109                                     (void **)&gVBoxSt.pfnWTSQuerySessionInformationA);
    110                 if (RT_FAILURE(rc))
    111                     LogFlowFunc(("WTSQuerySessionInformationA not found\n"));
     131                gVBoxSt.idDelayedInitTimer = SetTimer(gVBoxSt.hWTSAPIWnd, TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER,
     132                                                        2000, (TIMERPROC)NULL);
     133                gVBoxSt.fIsConsole = TRUE;
     134                gVBoxSt.enmConnectState = WTSActive;
     135                rc = VINF_SUCCESS;
    112136            }
    113137            else
    114                 LogFlowFunc(("WTSUnRegisterSessionNotification not found\n"));
    115         }
    116         else
    117             LogFlowFunc(("WTSRegisterSessionNotification not found\n"));
     138                rc = RTErrConvertFromWin32(dwErr);
     139        }
     140
    118141        if (RT_SUCCESS(rc))
    119         {
    120             gVBoxSt.hWTSAPIWnd = hWnd;
    121             if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
    122                 vboxStCheckState();
    123             else
    124             {
    125                 DWORD dwErr = GetLastError();
    126                 LogFlowFunc(("WTSRegisterSessionNotification failed, error = %08X\n", dwErr));
    127                 if (dwErr == RPC_S_INVALID_BINDING)
    128                 {
    129                     gVBoxSt.idDelayedInitTimer = SetTimer(gVBoxSt.hWTSAPIWnd, TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER,
    130                                                           2000, (TIMERPROC)NULL);
    131                     gVBoxSt.fIsConsole = TRUE;
    132                     gVBoxSt.enmConnectState = WTSActive;
    133                     rc = VINF_SUCCESS;
    134                 }
    135                 else
    136                     rc = RTErrConvertFromWin32(dwErr);
    137             }
    138 
    139             if (RT_SUCCESS(rc))
    140                 return VINF_SUCCESS;
    141         }
    142 
    143         RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
    144     }
    145     else
    146         LogFlowFunc(("WTSAPI32 load failed, rc = %Rrc\n", rc));
     142            return VINF_SUCCESS;
     143    }
     144    else
     145    {
     146        LogRel(("WtsApi32.dll APIs are not available (%Rrc)\n", rc));
     147        gVBoxSt.pfnWTSRegisterSessionNotification = NULL;
     148        gVBoxSt.pfnWTSUnRegisterSessionNotification = NULL;
     149        gVBoxSt.pfnWTSQuerySessionInformationA = NULL;
     150    }
    147151
    148152    RT_ZERO(gVBoxSt);
     
    154158void vboxStTerm(void)
    155159{
    156     if (!gVBoxSt.hWTSAPIWnd)
    157     {
    158         LogFlowFunc(("vboxStTerm called for non-initialized St\n"));
    159         return;
    160     }
     160    AssertReturnVoid(gVBoxSt.hWTSAPIWnd);
    161161
    162162    if (gVBoxSt.idDelayedInitTimer)
     
    170170        if (!gVBoxSt.pfnWTSUnRegisterSessionNotification(gVBoxSt.hWTSAPIWnd))
    171171        {
    172             LogFlowFunc(("WTSAPI32 load failed, error = %08X\n", GetLastError()));
    173         }
    174     }
    175 
    176     RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
     172            LogWarnFunc(("WTSAPI32 unload failed, error = %08X\n", GetLastError()));
     173        }
     174    }
     175
    177176    RT_ZERO(gVBoxSt);
    178177}
    179178
    180 #define VBOXST_DBG_MAKECASE(_val) case _val: return #_val;
    181 
    182179static const char* vboxStDbgGetString(DWORD val)
    183180{
    184181    switch (val)
    185182    {
    186         VBOXST_DBG_MAKECASE(WTS_CONSOLE_CONNECT);
    187         VBOXST_DBG_MAKECASE(WTS_CONSOLE_DISCONNECT);
    188         VBOXST_DBG_MAKECASE(WTS_REMOTE_CONNECT);
    189         VBOXST_DBG_MAKECASE(WTS_REMOTE_DISCONNECT);
    190         VBOXST_DBG_MAKECASE(WTS_SESSION_LOGON);
    191         VBOXST_DBG_MAKECASE(WTS_SESSION_LOGOFF);
    192         VBOXST_DBG_MAKECASE(WTS_SESSION_LOCK);
    193         VBOXST_DBG_MAKECASE(WTS_SESSION_UNLOCK);
    194         VBOXST_DBG_MAKECASE(WTS_SESSION_REMOTE_CONTROL);
     183        RT_CASE_RET_STR(WTS_CONSOLE_CONNECT);
     184        RT_CASE_RET_STR(WTS_CONSOLE_DISCONNECT);
     185        RT_CASE_RET_STR(WTS_REMOTE_CONNECT);
     186        RT_CASE_RET_STR(WTS_REMOTE_DISCONNECT);
     187        RT_CASE_RET_STR(WTS_SESSION_LOGON);
     188        RT_CASE_RET_STR(WTS_SESSION_LOGOFF);
     189        RT_CASE_RET_STR(WTS_SESSION_LOCK);
     190        RT_CASE_RET_STR(WTS_SESSION_UNLOCK);
     191        RT_CASE_RET_STR(WTS_SESSION_REMOTE_CONTROL);
    195192        default:
    196             LogFlowFunc(("invalid WTS state %d\n", val));
     193            LogWarnFunc(("invalid WTS state %d\n", val));
    197194            return "Unknown";
    198195    }
     
    212209    else
    213210    {
    214         LogFlowFunc(("timer WTSRegisterSessionNotification failed, error = %08X\n", GetLastError()));
     211        LogWarnFunc(("timer WTSRegisterSessionNotification failed, error = %08X\n", GetLastError()));
    215212        Assert(gVBoxSt.fIsConsole == TRUE);
    216213        Assert(gVBoxSt.enmConnectState == WTSActive);
     
    228225{
    229226    RT_NOREF(wEvent);
    230     LogFlowFunc(("WTS Event: %s\n", vboxStDbgGetString(wEvent)));
     227    LogFunc(("WTS Event: %s\n", vboxStDbgGetString(wEvent)));
    231228    BOOL fOldIsActiveConsole = vboxStIsActiveConsole();
    232229
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette