VirtualBox

Ignore:
Timestamp:
Jun 17, 2013 2:32:51 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
86471
Message:

updates

Location:
trunk/src/VBox/Additions/WINNT/VBoxTray
Files:
5 edited

Legend:

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

    r43584 r46593  
    2727#include <iprt/alloc.h>
    2828#include <iprt/list.h>
     29#include <iprt/ldr.h>
    2930
    3031#define LALOG(a) do { if (gCtx.fLogEnabled) LogRel(a); } while(0)
     
    8788        char *pszPropWaitPattern; /* Which properties are monitored. */
    8889    } activeClient;
    89 
    90     HMODULE hModuleKernel32;
    9190
    9291    BOOL (WINAPI * pfnProcessIdToSessionId)(DWORD dwProcessId, DWORD *pSessionId);
     
    12501249    RT_ZERO(gCtx.activeClient);
    12511250
    1252     gCtx.hModuleKernel32 = LoadLibrary("KERNEL32");
    1253 
    1254     if (gCtx.hModuleKernel32)
    1255     {
    1256         *(uintptr_t *)&gCtx.pfnProcessIdToSessionId = (uintptr_t)GetProcAddress(gCtx.hModuleKernel32, "ProcessIdToSessionId");
    1257     }
    1258     else
    1259     {
    1260         gCtx.pfnProcessIdToSessionId = NULL;
    1261     }
     1251    *(void **)&gCtx.pfnProcessIdToSessionId = RTLdrGetSystemSymbol("KERNEL32", "ProcessIdToSessionId");
    12621252    *pfStartThread = true;
    12631253    *ppInstance = &gCtx;
     
    12801270    ActionExecutorDeleteActions(&pCtx->listDetachActions);
    12811271
    1282     if (pCtx->hModuleKernel32)
    1283     {
    1284         FreeLibrary(pCtx->hModuleKernel32);
    1285         pCtx->pfnProcessIdToSessionId = NULL;
    1286     }
    1287     pCtx->hModuleKernel32 = NULL;
     1272    pCtx->pfnProcessIdToSessionId = NULL;
    12881273}
    12891274
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxMMR.cpp

    r42295 r46593  
    1818#include "VBoxTray.h"
    1919#include "VBoxMMR.h"
     20#include <iprt/ldr.h>
    2021
    2122struct VBOXMMRCONTEXT
    2223{
    23     HINSTANCE hMod;
     24    RTLDRMOD  hModHook;
    2425    HHOOK     hHook;
    2526};
     
    3233void VBoxMMRCleanup(VBOXMMRCONTEXT *pCtx)
    3334{
    34     if (NULL != pCtx->hHook)
     35    if (pCtx->hHook)
    3536    {
    3637        UnhookWindowsHookEx(pCtx->hHook);
     38        pCtx->hHook = NULL;
    3739    }
    3840
    39     if (pCtx->hMod)
     41    if (pCtx->hModHook != NIL_RTLDRMOD)
    4042    {
    41         FreeLibrary(pCtx->hMod);
     43        RTLdrClose(pCtx->hModHook);
     44        pCtx->hModHook = NIL_RTLDRMOD;
    4245    }
    43 
    44     return;
    4546}
    4647
     
    5051    bool *pfStartThread)
    5152{
    52     HOOKPROC pHook = NULL;
    53 
    5453    LogRel2(("VBoxMMR: Initializing\n"));
    5554
    56     gCtx.hMod = LoadLibraryA(g_pszMMRDLL);
    57     if (NULL == gCtx.hMod)
     55    int rc = RTLdrLoadAppPriv(g_pszMMRDLL, &gCtx.hModHook);
     56    if (RT_SUCCESS(rc))
    5857    {
    59         LogRel2(("VBoxMMR: Hooking library not found\n"));
    60         VBoxMMRCleanup(&gCtx);
    61         return VERR_NOT_FOUND;
     58        HOOKPROC pHook = (HOOKPROC)RTLdrGetFunction(gCtx.hModHook, g_pszMMRPROC);
     59        if (pHook)
     60        {
     61            HMODULE hMod = (HMODULE)RTLdrGetNativeHandle(gCtx.hModHook);
     62            Assert(hMod != (HMODULE)~(uintptr_t)0);
     63            gCtx.hHook = SetWindowsHookEx(WH_CBT, pHook, hMod, 0);
     64            if (gCtx.hHook)
     65            {
     66                *ppInstance = &gCtx;
     67                return VINF_SUCCESS;
     68            }
     69
     70            rc = RTErrConvertFromWin32(GetLastError());
     71            LogRel2(("VBoxMMR: Error installing hooking proc: %Rrc\n", rc));
     72        }
     73        else
     74        {
     75            LogRel2(("VBoxMMR: Hooking proc not found\n"));
     76            rc = VERR_NOT_FOUND;
     77        }
    6278    }
     79    else
     80        LogRel2(("VBoxMMR: Hooking library not found (%Rrc)\n", rc));
    6381
    64     pHook = (HOOKPROC) GetProcAddress(gCtx.hMod, g_pszMMRPROC);
    65     if (NULL == pHook)
    66     {
    67         LogRel2(("VBoxMMR: Hooking proc not found\n"));
    68         VBoxMMRCleanup(&gCtx);
    69         return VERR_NOT_FOUND;
    70     }
    71 
    72     gCtx.hHook = SetWindowsHookEx(WH_CBT, pHook, gCtx.hMod, 0);
    73     if (NULL == gCtx.hHook)
    74     {
    75         int rc = RTErrConvertFromWin32(GetLastError());
    76         LogRel2(("VBoxMMR: Error installing hooking proc: %d\n", rc));
    77         VBoxMMRCleanup(&gCtx);
    78         return rc;
    79     }
    80 
    81     *ppInstance = &gCtx;
    82 
    83     return VINF_SUCCESS;
     82    RTLdrClose(gCtx.hModHook);
     83    gCtx.hModHook = NIL_RTLDRMOD;
     84    return rc;
    8485}
    8586
     
    9596    return 0;
    9697}
     98
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSeamless.cpp

    r45760 r46593  
    2525#include <VBox/VMMDev.h>
    2626#include <iprt/assert.h>
     27#include <iprt/ldr.h>
    2728#include <VBoxGuestInternal.h>
    2829
     
    3132    const VBOXSERVICEENV *pEnv;
    3233
    33     HMODULE    hModule;
     34    RTLDRMOD hModHook;
    3435
    3536    BOOL    (* pfnVBoxHookInstallWindowTracker)(HMODULE hDll);
     
    5556    *pfStartThread = false;
    5657    gCtx.pEnv = pEnv;
     58    gCtx.hModHook = NIL_RTLDRMOD;
    5759
    5860    OSVERSIONINFO OSinfo;
     
    7274    {
    7375        /* Will fail if SetWinEventHook is not present (version < NT4 SP6 apparently) */
    74         gCtx.hModule = LoadLibrary(VBOXHOOK_DLL_NAME);
    75         if (gCtx.hModule)
    76         {
    77             *(uintptr_t *)&gCtx.pfnVBoxHookInstallWindowTracker = (uintptr_t)GetProcAddress(gCtx.hModule, "VBoxHookInstallWindowTracker");
    78             *(uintptr_t *)&gCtx.pfnVBoxHookRemoveWindowTracker  = (uintptr_t)GetProcAddress(gCtx.hModule, "VBoxHookRemoveWindowTracker");
     76        rc = RTLdrLoadAppPriv(VBOXHOOK_DLL_NAME, &gCtx.hModHook);
     77        if (RT_SUCCESS(rc))
     78        {
     79            *(PFNRT *)&gCtx.pfnVBoxHookInstallWindowTracker = RTLdrGetFunction(gCtx.hModHook, "VBoxHookInstallWindowTracker");
     80            *(PFNRT *)&gCtx.pfnVBoxHookRemoveWindowTracker  = RTLdrGetFunction(gCtx.hModHook, "VBoxHookRemoveWindowTracker");
    7981
    8082            /* rc should contain success status */
     
    9092        }
    9193        else
    92         {
    93             rc = RTErrConvertFromWin32(GetLastError());
    9494            Log(("VBoxTray: VBoxSeamlessInit: LoadLibrary of \"%s\" failed with rc=%Rrc\n", VBOXHOOK_DLL_NAME, rc));
    95         }
    9695    }
    9796
     
    109108    if (gCtx.pfnVBoxHookRemoveWindowTracker)
    110109        gCtx.pfnVBoxHookRemoveWindowTracker();
    111     if (gCtx.hModule)
    112         FreeLibrary(gCtx.hModule);
    113     gCtx.hModule = 0;
     110    if (gCtx.hModHook != NIL_RTLDRMOD)
     111    {
     112        RTLdrClose(gCtx.hModHook);
     113        gCtx.hModHook = NIL_RTLDRMOD;
     114    }
    114115    return;
    115116}
     
    122123        VBoxSeamlessCheckWindows();
    123124
    124         gCtx.pfnVBoxHookInstallWindowTracker(gCtx.hModule);
     125        HMODULE hMod = (HMODULE)RTLdrGetNativeHandle(gCtx.hModHook);
     126        Assert(hMod != (HMODULE)~(uintptr_t)0);
     127        gCtx.pfnVBoxHookInstallWindowTracker(hMod);
    125128    }
    126129}
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp

    r46428 r46593  
    4141
    4242#include <iprt/buildconfig.h>
     43#include <iprt/ldr.h>
    4344
    4445/* Default desktop state tracking */
     
    6364 * of the window passed to vboxStInit */
    6465static int vboxStInit(HWND hWnd);
    65 static void vboxStTerm();
     66static void vboxStTerm(void);
    6667/* @returns true on "IsActiveConsole" state change */
    6768static BOOL vboxStHandleEvent(WPARAM EventID, LPARAM SessionID);
     
    549550        {
    550551            BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR  *SecurityDescriptor, PULONG  SecurityDescriptorSize);
    551 
    552             HMODULE hModule = LoadLibrary("ADVAPI32.DLL");
    553             if (!hModule)
    554             {
    555                 dwErr = GetLastError();
    556                 Log(("VBoxTray: Loading module ADVAPI32.DLL failed with last error = %08X\n", dwErr));
    557             }
    558             else
     552            *(void **)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA =
     553                RTLdrGetSystemSymbol("ADVAPI32.DLL", "ConvertStringSecurityDescriptorToSecurityDescriptorA");
     554            Log(("VBoxTray: pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
     555            if (pfnConvertStringSecurityDescriptorToSecurityDescriptorA)
    559556            {
    560557                PSECURITY_DESCRIPTOR    pSD;
     
    563560                BOOL                    fSaclDefaulted = FALSE;
    564561
    565                 *(uintptr_t *)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA = (uintptr_t)GetProcAddress(hModule, "ConvertStringSecurityDescriptorToSecurityDescriptorA");
    566 
    567                 Log(("VBoxTray: pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
    568                 if (pfnConvertStringSecurityDescriptorToSecurityDescriptorA)
     562                fRC = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
     563                                                                              SDDL_REVISION_1, &pSD, NULL);
     564                if (!fRC)
    569565                {
    570                     fRC = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
    571                                                                                   SDDL_REVISION_1, &pSD, NULL);
     566                    dwErr = GetLastError();
     567                    Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with last error = %08X\n", dwErr));
     568                }
     569                else
     570                {
     571                    fRC = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
    572572                    if (!fRC)
    573573                    {
    574574                        dwErr = GetLastError();
    575                         Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with last error = %08X\n", dwErr));
     575                        Log(("VBoxTray: GetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
    576576                    }
    577577                    else
    578578                    {
    579                         fRC = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
     579                        fRC = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
    580580                        if (!fRC)
    581581                        {
    582582                            dwErr = GetLastError();
    583                             Log(("VBoxTray: GetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
    584                         }
    585                         else
    586                         {
    587                             fRC = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
    588                             if (!fRC)
    589                             {
    590                                 dwErr = GetLastError();
    591                                 Log(("VBoxTray: SetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
    592                             }
     583                            Log(("VBoxTray: SetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
    593584                        }
    594585                    }
     
    10151006{
    10161007    HWND hWTSAPIWnd;
    1017     HMODULE hWTSAPI32;
     1008    RTLDRMOD hLdrModWTSAPI32;
    10181009    BOOL fIsConsole;
    10191010    WTS_CONNECTSTATE_CLASS enmConnectState;
     
    10321023    USHORT *pProtocolType = NULL;
    10331024    DWORD cbBuf = 0;
    1034     if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSConnectState, (LPTSTR *)&penmConnectState, &cbBuf))
    1035     {
    1036         if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientProtocolType, (LPTSTR *)&pProtocolType, &cbBuf))
     1025    if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSConnectState,
     1026                                               (LPTSTR *)&penmConnectState, &cbBuf))
     1027    {
     1028        if (gVBoxSt.pfnWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientProtocolType,
     1029                                                   (LPTSTR *)&pProtocolType, &cbBuf))
    10371030        {
    10381031            gVBoxSt.fIsConsole = (*pProtocolType == 0);
     
    10401033            return VINF_SUCCESS;
    10411034        }
    1042         else
    1043         {
    1044             DWORD dwErr = GetLastError();
    1045             WARN(("VBoxTray: WTSQuerySessionInformationA WTSClientProtocolType failed, error = %08X\n", dwErr));
    1046             rc = RTErrConvertFromWin32(dwErr);
    1047         }
     1035
     1036        DWORD dwErr = GetLastError();
     1037        WARN(("VBoxTray: WTSQuerySessionInformationA WTSClientProtocolType failed, error = %08X\n", dwErr));
     1038        rc = RTErrConvertFromWin32(dwErr);
    10481039    }
    10491040    else
     
    10631054static int vboxStInit(HWND hWnd)
    10641055{
    1065     int rc = VINF_SUCCESS;
    1066     memset(&gVBoxSt, 0, sizeof (gVBoxSt));
    1067     gVBoxSt.hWTSAPI32 = LoadLibrary("WTSAPI32.DLL");
    1068     if (gVBoxSt.hWTSAPI32)
    1069     {
    1070         *(uintptr_t *)&gVBoxSt.pfnWTSRegisterSessionNotification = (uintptr_t)GetProcAddress(gVBoxSt.hWTSAPI32, "WTSRegisterSessionNotification");
    1071         if (!gVBoxSt.pfnWTSRegisterSessionNotification)
    1072         {
     1056    RT_ZERO(gVBoxSt);
     1057    int rc = RTLdrLoadSystem("WTSAPI32.DLL", false /*fNoUnload*/, &gVBoxSt.hLdrModWTSAPI32);
     1058    if (RT_SUCCESS(rc))
     1059    {
     1060        rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSRegisterSessionNotification",
     1061                            (void **)&gVBoxSt.pfnWTSRegisterSessionNotification);
     1062        if (RT_SUCCESS(rc))
     1063        {
     1064            rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSUnRegisterSessionNotification",
     1065                                (void **)&gVBoxSt.pfnWTSUnRegisterSessionNotification);
     1066            if (RT_SUCCESS(rc))
     1067            {
     1068                rc = RTLdrGetSymbol(gVBoxSt.hLdrModWTSAPI32, "WTSQuerySessionInformationA",
     1069                                    (void **)&gVBoxSt.pfnWTSQuerySessionInformationA);
     1070                if (RT_FAILURE(rc))
     1071                    WARN(("VBoxTray: WTSQuerySessionInformationA not found\n"));
     1072            }
     1073            else
     1074                WARN(("VBoxTray: WTSUnRegisterSessionNotification not found\n"));
     1075        }
     1076        else
    10731077            WARN(("VBoxTray: WTSRegisterSessionNotification not found\n"));
    1074             rc = VERR_NOT_SUPPORTED;
    1075         }
    1076 
    1077         *(uintptr_t *)&gVBoxSt.pfnWTSUnRegisterSessionNotification = (uintptr_t)GetProcAddress(gVBoxSt.hWTSAPI32, "WTSUnRegisterSessionNotification");
    1078         if (!gVBoxSt.pfnWTSUnRegisterSessionNotification)
    1079         {
    1080             WARN(("VBoxTray: WTSUnRegisterSessionNotification not found\n"));
    1081             rc = VERR_NOT_SUPPORTED;
    1082         }
    1083 
    1084         *(uintptr_t *)&gVBoxSt.pfnWTSQuerySessionInformationA = (uintptr_t)GetProcAddress(gVBoxSt.hWTSAPI32, "WTSQuerySessionInformationA");
    1085         if (!gVBoxSt.pfnWTSQuerySessionInformationA)
    1086         {
    1087             WARN(("VBoxTray: WTSQuerySessionInformationA not found\n"));
    1088             rc = VERR_NOT_SUPPORTED;
    1089         }
    1090 
    1091         if (rc == VINF_SUCCESS)
     1078        if (RT_SUCCESS(rc))
    10921079        {
    10931080            gVBoxSt.hWTSAPIWnd = hWnd;
    10941081            if (gVBoxSt.pfnWTSRegisterSessionNotification(gVBoxSt.hWTSAPIWnd, NOTIFY_FOR_THIS_SESSION))
    1095             {
    10961082                vboxStCheckState();
    1097             }
    10981083            else
    10991084            {
     
    11021087                if (dwErr == RPC_S_INVALID_BINDING)
    11031088                {
    1104                     gVBoxSt.idDelayedInitTimer = SetTimer(gVBoxSt.hWTSAPIWnd, TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER, 2000, (TIMERPROC)NULL);
    1105                     rc = VINF_SUCCESS;
    1106 
     1089                    gVBoxSt.idDelayedInitTimer = SetTimer(gVBoxSt.hWTSAPIWnd, TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER,
     1090                                                          2000, (TIMERPROC)NULL);
    11071091                    gVBoxSt.fIsConsole = TRUE;
    11081092                    gVBoxSt.enmConnectState = WTSActive;
     1093                    rc = VINF_SUCCESS;
    11091094                }
    11101095                else
     
    11161101        }
    11171102
    1118         FreeLibrary(gVBoxSt.hWTSAPI32);
     1103        RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
    11191104    }
    11201105    else
    1121     {
    1122         DWORD dwErr = GetLastError();
    1123         WARN(("VBoxTray: WTSAPI32 load failed, error = %08X\n", dwErr));
    1124         rc = RTErrConvertFromWin32(dwErr);
    1125     }
    1126 
    1127     memset(&gVBoxSt, 0, sizeof (gVBoxSt));
     1106        WARN(("VBoxTray: WTSAPI32 load failed, rc = %Rrc\n", rc));
     1107
     1108    RT_ZERO(gVBoxSt);
    11281109    gVBoxSt.fIsConsole = TRUE;
    11291110    gVBoxSt.enmConnectState = WTSActive;
     
    11311112}
    11321113
    1133 static void vboxStTerm()
    1134 {
    1135     if (gVBoxSt.hWTSAPIWnd)
     1114static void vboxStTerm(void)
     1115{
     1116    if (!gVBoxSt.hWTSAPIWnd)
    11361117    {
    11371118        WARN(("VBoxTray: vboxStTerm called for non-initialized St\n"));
     
    11541135    }
    11551136
    1156     FreeLibrary(gVBoxSt.hWTSAPI32);
    1157     memset(&gVBoxSt, 0, sizeof (gVBoxSt));
     1137    RTLdrClose(gVBoxSt.hLdrModWTSAPI32);
     1138    RT_ZERO(gVBoxSt);
    11581139}
    11591140
     
    12281209    BOOL fIsInputDesktop;
    12291210    UINT_PTR idTimer;
    1230     HMODULE hHookModule;
     1211    RTLDRMOD hLdrModHook;
    12311212    BOOL (* pfnVBoxHookInstallActiveDesktopTracker)(HMODULE hDll);
    12321213    BOOL (* pfnVBoxHookRemoveActiveDesktopTracker)();
    1233     HMODULE hUSER32;
    12341214    HDESK (WINAPI * pfnGetThreadDesktop)(DWORD dwThreadId);
    12351215    HDESK (WINAPI * pfnOpenInputDesktop)(DWORD dwFlags, BOOL fInherit, ACCESS_MASK dwDesiredAccess);
     
    12891269    }
    12901270
    1291     memset(&gVBoxDt, 0, sizeof (gVBoxDt));
     1271    RT_ZERO(gVBoxDt);
    12921272
    12931273    gVBoxDt.hNotifyEvent = CreateEvent(NULL, FALSE, FALSE, VBOXHOOK_GLOBAL_DT_EVENT_NAME);
    12941274    if (gVBoxDt.hNotifyEvent != NULL)
    12951275    {
    1296         gVBoxDt.hHookModule = LoadLibrary(VBOXHOOK_DLL_NAME);
    1297         if (gVBoxDt.hHookModule)
    1298         {
    1299             *(uintptr_t *)&gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker = (uintptr_t)GetProcAddress(gVBoxDt.hHookModule, "VBoxHookInstallActiveDesktopTracker");
    1300             if (!gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker)
    1301             {
     1276        /* Load the hook dll and resolve the necessary entry points. */
     1277        rc = RTLdrLoadAppPriv(VBOXHOOK_DLL_NAME, &gVBoxDt.hLdrModHook);
     1278        if (RT_SUCCESS(rc))
     1279        {
     1280            rc = RTLdrGetSymbol(gVBoxDt.hLdrModHook, "VBoxHookInstallActiveDesktopTracker",
     1281                                (void **)&gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker);
     1282            if (RT_SUCCESS(rc))
     1283            {
     1284                rc = RTLdrGetSymbol(gVBoxDt.hLdrModHook, "VBoxHookRemoveActiveDesktopTracker",
     1285                                    (void **)&gVBoxDt.pfnVBoxHookRemoveActiveDesktopTracker);
     1286                if (RT_FAILURE(rc))
     1287                    WARN(("VBoxTray: VBoxHookRemoveActiveDesktopTracker not found\n"));
     1288            }
     1289            else
    13021290                WARN(("VBoxTray: VBoxHookInstallActiveDesktopTracker not found\n"));
    1303                 rc = VERR_NOT_SUPPORTED;
    1304             }
    1305 
    1306             *(uintptr_t *)&gVBoxDt.pfnVBoxHookRemoveActiveDesktopTracker  = (uintptr_t)GetProcAddress(gVBoxDt.hHookModule, "VBoxHookInstallActiveDesktopTracker");
    1307             if (!gVBoxDt.pfnVBoxHookRemoveActiveDesktopTracker)
    1308             {
    1309                 WARN(("VBoxTray: VBoxHookRemoveActiveDesktopTracker not found\n"));
    1310                 rc = VERR_NOT_SUPPORTED;
    1311             }
    1312 
    1313             if (VINF_SUCCESS == rc)
    1314             {
    1315                 gVBoxDt.hUSER32 = LoadLibrary("User32.dll");
    1316                 if (gVBoxDt.hUSER32)
     1291            if (RT_SUCCESS(rc))
     1292            {
     1293                /* Try get the system APIs we need. */
     1294                *(void **)&gVBoxDt.pfnGetThreadDesktop = RTLdrGetSystemSymbol("User32.dll", "GetThreadDesktop");
     1295                if (!gVBoxDt.pfnGetThreadDesktop)
    13171296                {
    1318                     *(uintptr_t *)&gVBoxDt.pfnGetThreadDesktop = (uintptr_t)GetProcAddress(gVBoxDt.hUSER32, "GetThreadDesktop");
    1319                     if (!gVBoxDt.pfnGetThreadDesktop)
     1297                    WARN(("VBoxTray: GetThreadDesktop not found\n"));
     1298                    rc = VERR_NOT_SUPPORTED;
     1299                }
     1300
     1301                *(void **)&gVBoxDt.pfnOpenInputDesktop = RTLdrGetSystemSymbol("User32.dll", "OpenInputDesktop");
     1302                if (!gVBoxDt.pfnOpenInputDesktop)
     1303                {
     1304                    WARN(("VBoxTray: OpenInputDesktop not found\n"));
     1305                    rc = VERR_NOT_SUPPORTED;
     1306                }
     1307
     1308                *(void **)&gVBoxDt.pfnCloseDesktop = RTLdrGetSystemSymbol("User32.dll", "CloseDesktop");
     1309                if (!gVBoxDt.pfnCloseDesktop)
     1310                {
     1311                    WARN(("VBoxTray: CloseDesktop not found\n"));
     1312                    rc = VERR_NOT_SUPPORTED;
     1313                }
     1314
     1315                if (RT_SUCCESS(rc))
     1316                {
     1317                    BOOL fRc = FALSE;
     1318                    /* For Vista and up we need to change the integrity of the security descriptor, too. */
     1319                    if (gMajorVersion >= 6)
    13201320                    {
    1321                         WARN(("VBoxTray: GetThreadDesktop not found\n"));
    1322                         rc = VERR_NOT_SUPPORTED;
    1323                     }
    1324 
    1325                     *(uintptr_t *)&gVBoxDt.pfnOpenInputDesktop = (uintptr_t)GetProcAddress(gVBoxDt.hUSER32, "OpenInputDesktop");
    1326                     if (!gVBoxDt.pfnOpenInputDesktop)
    1327                     {
    1328                         WARN(("VBoxTray: OpenInputDesktop not found\n"));
    1329                         rc = VERR_NOT_SUPPORTED;
    1330                     }
    1331 
    1332                     *(uintptr_t *)&gVBoxDt.pfnCloseDesktop = (uintptr_t)GetProcAddress(gVBoxDt.hUSER32, "CloseDesktop");
    1333                     if (!gVBoxDt.pfnCloseDesktop)
    1334                     {
    1335                         WARN(("VBoxTray: CloseDesktop not found\n"));
    1336                         rc = VERR_NOT_SUPPORTED;
    1337                     }
    1338 
    1339                     if (VINF_SUCCESS == rc)
    1340                     {
    1341                         BOOL bRc = FALSE;
    1342                         /* For Vista and up we need to change the integrity of the security descriptor, too. */
    1343                         if (gMajorVersion >= 6)
     1321                        HMODULE hModHook = (HMODULE)RTLdrGetNativeHandle(gVBoxDt.hLdrModHook);
     1322                        Assert((uintptr_t)hModHook != ~(uintptr_t)0);
     1323                        fRc = gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker(hModHook);
     1324                        if (!fRc)
    13441325                        {
    1345                             bRc = gVBoxDt.pfnVBoxHookInstallActiveDesktopTracker(gVBoxDt.hHookModule);
    1346                             if (!bRc)
    1347                             {
    1348                                 DWORD dwErr = GetLastError();
    1349                                 WARN(("VBoxTray: pfnVBoxHookInstallActiveDesktopTracker failed, last error = %08X\n", dwErr));
    1350                             }
    1351                         }
    1352 
    1353                         if (!bRc)
    1354                         {
    1355                             gVBoxDt.idTimer = SetTimer(ghwndToolWindow, TIMERID_VBOXTRAY_DT_TIMER, 500, (TIMERPROC)NULL);
    1356                             if (!gVBoxDt.idTimer)
    1357                             {
    1358                                 DWORD dwErr = GetLastError();
    1359                                 WARN(("VBoxTray: SetTimer error %08X\n", dwErr));
    1360                                 rc = RTErrConvertFromWin32(dwErr);
    1361                             }
    1362                         }
    1363 
    1364                         if (RT_SUCCESS(rc))
    1365                         {
    1366                             gVBoxDt.fIsInputDesktop = vboxDtCalculateIsInputDesktop();
    1367                             return VINF_SUCCESS;
     1326                            DWORD dwErr = GetLastError();
     1327                            WARN(("VBoxTray: pfnVBoxHookInstallActiveDesktopTracker failed, last error = %08X\n", dwErr));
    13681328                        }
    13691329                    }
    1370                     FreeLibrary(gVBoxDt.hUSER32);
     1330
     1331                    if (!fRc)
     1332                    {
     1333                        gVBoxDt.idTimer = SetTimer(ghwndToolWindow, TIMERID_VBOXTRAY_DT_TIMER, 500, (TIMERPROC)NULL);
     1334                        if (!gVBoxDt.idTimer)
     1335                        {
     1336                            DWORD dwErr = GetLastError();
     1337                            WARN(("VBoxTray: SetTimer error %08X\n", dwErr));
     1338                            rc = RTErrConvertFromWin32(dwErr);
     1339                        }
     1340                    }
     1341
     1342                    if (RT_SUCCESS(rc))
     1343                    {
     1344                        gVBoxDt.fIsInputDesktop = vboxDtCalculateIsInputDesktop();
     1345                        return VINF_SUCCESS;
     1346                    }
    13711347                }
    13721348            }
    13731349
    1374             FreeLibrary(gVBoxDt.hHookModule);
     1350            RTLdrClose(gVBoxDt.hLdrModHook);
    13751351        }
    13761352        else
     
    13911367
    13921368
    1393     memset(&gVBoxDt, 0, sizeof (gVBoxDt));
     1369    RT_ZERO(gVBoxDt);
    13941370    gVBoxDt.fIsInputDesktop = TRUE;
    13951371
     
    13991375static void vboxDtTerm()
    14001376{
    1401     if (!gVBoxDt.hHookModule)
     1377    if (!gVBoxDt.hLdrModHook)
    14021378        return;
    14031379
    14041380    gVBoxDt.pfnVBoxHookRemoveActiveDesktopTracker();
    14051381
    1406     FreeLibrary(gVBoxDt.hUSER32);
    1407     FreeLibrary(gVBoxDt.hHookModule);
     1382    RTLdrClose(gVBoxDt.hLdrModHook);
    14081383    CloseHandle(gVBoxDt.hNotifyEvent);
    14091384
    1410     memset(&gVBoxDt, 0, sizeof (gVBoxDt));
     1385    RT_ZERO(gVBoxDt);
    14111386}
    14121387/* @returns true on "IsInputDesktop" state change */
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxVRDP.cpp

    r33966 r46593  
    2525#include <VBoxGuestInternal.h>
    2626#include <iprt/assert.h>
     27#include <iprt/ldr.h>
    2728
    2829
     
    256257    BOOL fSavedThemeEnabled;
    257258
    258     HMODULE hModule;
     259    RTLDRMOD hModUxTheme;
    259260
    260261    HRESULT (* pfnEnableTheming)(BOOL fEnable);
     
    274275    gCtx.fSavedThemeEnabled = FALSE;
    275276
    276     gCtx.hModule = LoadLibrary("UxTheme");
    277 
    278     if (gCtx.hModule)
    279     {
    280         *(uintptr_t *)&gCtx.pfnEnableTheming = (uintptr_t)GetProcAddress(gCtx.hModule, "EnableTheming");
    281         *(uintptr_t *)&gCtx.pfnIsThemeActive = (uintptr_t)GetProcAddress(gCtx.hModule, "IsThemeActive");
     277    int rc = RTLdrLoadSystem("UxTheme.dll", false /*fNoUnload*/, &gCtx.hModUxTheme);
     278    if (RT_SUCCESS(rc))
     279    {
     280        *(PFNRT *)&gCtx.pfnEnableTheming = RTLdrGetFunction(gCtx.hModUxTheme, "EnableTheming");
     281        *(PFNRT *)&gCtx.pfnIsThemeActive = RTLdrGetFunction(gCtx.hModUxTheme, "IsThemeActive");
    282282    }
    283283    else
    284284    {
    285         gCtx.pfnEnableTheming = 0;
     285        gCtx.hModUxTheme = NIL_RTLDRMOD;
     286        gCtx.pfnEnableTheming = NULL;
     287        gCtx.pfnIsThemeActive = NULL;
    286288    }
    287289
     
    297299    VBOXVRDPCONTEXT *pCtx = (VBOXVRDPCONTEXT *)pInstance;
    298300    vboxExperienceRestore (pCtx->level);
    299     if (gCtx.hModule)
    300         FreeLibrary(gCtx.hModule);
    301     gCtx.hModule = 0;
     301    if (gCtx.hModUxTheme != NIL_RTLDRMOD)
     302    {
     303        RTLdrClose(gCtx.hModUxTheme);
     304        gCtx.hModUxTheme = NIL_RTLDRMOD;
     305    }
    302306    return;
    303307}
Note: See TracChangeset for help on using the changeset viewer.

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