VirtualBox

Changeset 95965 in vbox


Ignore:
Timestamp:
Aug 1, 2022 2:34:03 PM (2 years ago)
Author:
vboxsync
Message:

Additions/VBoxTray: Moved the session tracking code into an own module. VBoxTray.cpp only should contain the main / sub service handling (if possible).

Location:
trunk/src/VBox/Additions/WINNT/VBoxTray
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/VBoxTray/Makefile.kmk

    r95960 r95965  
    4141        VBoxDispIf.cpp \
    4242        VBoxSeamless.cpp \
     43        VBoxSessionTracking.cpp \
    4344        VBoxDisplay.cpp \
    4445        VBoxVRDP.cpp \
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp

    r95959 r95965  
    2424
    2525#include "VBoxTray.h"
     26#include "VBoxTrayInternal.h"
    2627#include "VBoxTrayMsg.h"
    2728#include "VBoxHelpers.h"
     
    5455#include <VBox/err.h>
    5556
    56 /* Default desktop state tracking */
    57 #include <Wtsapi32.h>
    58 
    59 /*
    60  * St (session [state] tracking) functionality API
    61  *
    62  * !!!NOTE: this API is NOT thread-safe!!!
    63  * it is supposed to be called & used from within the window message handler thread
    64  * of the window passed to vboxStInit */
    65 static int vboxStInit(HWND hWnd);
    66 static void vboxStTerm(void);
    67 /* @returns true on "IsActiveConsole" state change */
    68 static BOOL vboxStHandleEvent(WPARAM EventID);
    69 static BOOL vboxStIsActiveConsole();
    70 static BOOL vboxStCheckTimer(WPARAM wEvent);
     57
     58
    7159
    7260/*
     
    13901378}
    13911379
    1392 /* St (session [state] tracking) functionality API impl */
    1393 
    1394 typedef struct VBOXST
    1395 {
    1396     HWND hWTSAPIWnd;
    1397     RTLDRMOD hLdrModWTSAPI32;
    1398     BOOL fIsConsole;
    1399     WTS_CONNECTSTATE_CLASS enmConnectState;
    1400     UINT_PTR idDelayedInitTimer;
    1401     BOOL (WINAPI * pfnWTSRegisterSessionNotification)(HWND hWnd, DWORD dwFlags);
    1402     BOOL (WINAPI * pfnWTSUnRegisterSessionNotification)(HWND hWnd);
    1403     BOOL (WINAPI * pfnWTSQuerySessionInformationA)(HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPTSTR *ppBuffer, DWORD *pBytesReturned);
    1404 } VBOXST;
    1405 
    1406 static VBOXST gVBoxSt;
    1407 
    1408 static int vboxStCheckState()
    1409 {
    1410     int rc = VINF_SUCCESS;
    1411     WTS_CONNECTSTATE_CLASS *penmConnectState = NULL;
    1412     USHORT *pProtocolType = NULL;
    1413     DWORD cbBuf = 0;
    1414     if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSConnectState,
    1415                                                (LPTSTR *)&penmConnectState, &cbBuf))
    1416     {
    1417         if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientProtocolType,
    1418                                                    (LPTSTR *)&pProtocolType, &cbBuf))
    1419         {
    1420             gVBoxSt.fIsConsole = (*pProtocolType == 0);
    1421             gVBoxSt.enmConnectState = *penmConnectState;
    1422             return VINF_SUCCESS;
    1423         }
    1424 
    1425         DWORD dwErr = GetLastError();
    1426         LogFlowFunc(("WTSQuerySessionInformationA WTSClientProtocolType failed, error = %08X\n", dwErr));
    1427         rc = RTErrConvertFromWin32(dwErr);
    1428     }
    1429     else
    1430     {
    1431         DWORD dwErr = GetLastError();
    1432         LogFlowFunc(("WTSQuerySessionInformationA WTSConnectState failed, error = %08X\n", dwErr));
    1433         rc = RTErrConvertFromWin32(dwErr);
    1434     }
    1435 
    1436     /* failure branch, set to "console-active" state */
    1437     gVBoxSt.fIsConsole = TRUE;
    1438     gVBoxSt.enmConnectState = WTSActive;
    1439 
    1440     return rc;
    1441 }
    1442 
    1443 static int vboxStInit(HWND hWnd)
    1444 {
    1445     RT_ZERO(gVBoxSt);
    1446     int rc = RTLdrLoadSystem("WTSAPI32.DLL", false /*fNoUnload*/, &gVBoxSt.hLdrModWTSAPI32);
    1447     if (RT_SUCCESS(rc))
    1448     {
    1449         rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSRegisterSessionNotification",
    1450                             (void **)&gVBoxSt.pfnWTSRegisterSessionNotification);
    1451         if (RT_SUCCESS(rc))
    1452         {
    1453             rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSUnRegisterSessionNotification",
    1454                                 (void **)&gVBoxSt.pfnWTSUnRegisterSessionNotification);
    1455             if (RT_SUCCESS(rc))
    1456             {
    1457                 rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSQuerySessionInformationA",
    1458                                     (void **)&gVBoxSt.pfnWTSQuerySessionInformationA);
    1459                 if (RT_FAILURE(rc))
    1460                     LogFlowFunc(("WTSQuerySessionInformationA not found\n"));
    1461             }
    1462             else
    1463                 LogFlowFunc(("WTSUnRegisterSessionNotification not found\n"));
    1464         }
    1465         else
    1466             LogFlowFunc(("WTSRegisterSessionNotification not found\n"));
    1467         if (RT_SUCCESS(rc))
    1468         {
    1469             gVBoxSt.hWTSAPIWnd = hWnd;
    1470             if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
    1471                 vboxStCheckState();
    1472             else
    1473             {
    1474                 DWORD dwErr = GetLastError();
    1475                 LogFlowFunc(("WTSRegisterSessionNotification failed, error = %08X\n", dwErr));
    1476                 if (dwErr == RPC_S_INVALID_BINDING)
    1477                 {
    1478                     gVBoxSt.idDelayedInitTimer = SetTimer(gVBoxSt.hWTSAPIWnd, TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER,
    1479                                                           2000, (TIMERPROC)NULL);
    1480                     gVBoxSt.fIsConsole = TRUE;
    1481                     gVBoxSt.enmConnectState = WTSActive;
    1482                     rc = VINF_SUCCESS;
    1483                 }
    1484                 else
    1485                     rc = RTErrConvertFromWin32(dwErr);
    1486             }
    1487 
    1488             if (RT_SUCCESS(rc))
    1489                 return VINF_SUCCESS;
    1490         }
    1491 
    1492         RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
    1493     }
    1494     else
    1495         LogFlowFunc(("WTSAPI32 load failed, rc = %Rrc\n", rc));
    1496 
    1497     RT_ZERO(gVBoxSt);
    1498     gVBoxSt.fIsConsole = TRUE;
    1499     gVBoxSt.enmConnectState = WTSActive;
    1500     return rc;
    1501 }
    1502 
    1503 static void vboxStTerm(void)
    1504 {
    1505     if (!gVBoxSt.hWTSAPIWnd)
    1506     {
    1507         LogFlowFunc(("vboxStTerm called for non-initialized St\n"));
    1508         return;
    1509     }
    1510 
    1511     if (gVBoxSt.idDelayedInitTimer)
    1512     {
    1513         /* notification is not registered, just kill timer */
    1514         KillTimer(gVBoxSt.hWTSAPIWnd, gVBoxSt.idDelayedInitTimer);
    1515         gVBoxSt.idDelayedInitTimer = 0;
    1516     }
    1517     else
    1518     {
    1519         if (!gVBoxSt.pfnWTSUnRegisterSessionNotification(gVBoxSt.hWTSAPIWnd))
    1520         {
    1521             LogFlowFunc(("WTSAPI32 load failed, error = %08X\n", GetLastError()));
    1522         }
    1523     }
    1524 
    1525     RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
    1526     RT_ZERO(gVBoxSt);
    1527 }
    1528 
    1529 #define VBOXST_DBG_MAKECASE(_val) case _val: return #_val;
    1530 
    1531 static const char* vboxStDbgGetString(DWORD val)
    1532 {
    1533     switch (val)
    1534     {
    1535         VBOXST_DBG_MAKECASE(WTS_CONSOLE_CONNECT);
    1536         VBOXST_DBG_MAKECASE(WTS_CONSOLE_DISCONNECT);
    1537         VBOXST_DBG_MAKECASE(WTS_REMOTE_CONNECT);
    1538         VBOXST_DBG_MAKECASE(WTS_REMOTE_DISCONNECT);
    1539         VBOXST_DBG_MAKECASE(WTS_SESSION_LOGON);
    1540         VBOXST_DBG_MAKECASE(WTS_SESSION_LOGOFF);
    1541         VBOXST_DBG_MAKECASE(WTS_SESSION_LOCK);
    1542         VBOXST_DBG_MAKECASE(WTS_SESSION_UNLOCK);
    1543         VBOXST_DBG_MAKECASE(WTS_SESSION_REMOTE_CONTROL);
    1544         default:
    1545             LogFlowFunc(("invalid WTS state %d\n", val));
    1546             return "Unknown";
    1547     }
    1548 }
    1549 
    1550 static BOOL vboxStCheckTimer(WPARAM wEvent)
    1551 {
    1552     if (wEvent != gVBoxSt.idDelayedInitTimer)
    1553         return FALSE;
    1554 
    1555     if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
    1556     {
    1557         KillTimer(gVBoxSt.hWTSAPIWnd, gVBoxSt.idDelayedInitTimer);
    1558         gVBoxSt.idDelayedInitTimer = 0;
    1559         vboxStCheckState();
    1560     }
    1561     else
    1562     {
    1563         LogFlowFunc(("timer WTSRegisterSessionNotification failed, error = %08X\n", GetLastError()));
    1564         Assert(gVBoxSt.fIsConsole == TRUE);
    1565         Assert(gVBoxSt.enmConnectState == WTSActive);
    1566     }
    1567 
    1568     return TRUE;
    1569 }
    1570 
    1571 
    1572 static BOOL vboxStHandleEvent(WPARAM wEvent)
    1573 {
    1574     RT_NOREF(wEvent);
    1575     LogFlowFunc(("WTS Event: %s\n", vboxStDbgGetString(wEvent)));
    1576     BOOL fOldIsActiveConsole = vboxStIsActiveConsole();
    1577 
    1578     vboxStCheckState();
    1579 
    1580     return !vboxStIsActiveConsole() != !fOldIsActiveConsole;
    1581 }
    1582 
    1583 static BOOL vboxStIsActiveConsole(void)
    1584 {
    1585     return (gVBoxSt.enmConnectState == WTSActive && gVBoxSt.fIsConsole);
    1586 }
    1587 
    15881380/*
    15891381 * Dt (desktop [state] tracking) functionality API impl
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTrayInternal.h

    r95963 r95965  
    2121# pragma once
    2222#endif
     23/*
     24 * St (session [state] tracking) functionality API
     25 *
     26 * !!!NOTE: this API is NOT thread-safe!!!
     27 * it is supposed to be called & used from within the window message handler thread
     28 * of the window passed to vboxStInit */
     29int vboxStInit(HWND hWnd);
     30void vboxStTerm(void);
     31/* @returns true on "IsActiveConsole" state change */
     32BOOL vboxStHandleEvent(WPARAM EventID);
     33BOOL vboxStIsActiveConsole();
     34BOOL vboxStCheckTimer(WPARAM wEvent);
    2335
    2436DWORD VBoxDisplayGetCount();
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