VirtualBox

Ignore:
Timestamp:
Nov 16, 2009 10:14:48 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
54868
Message:

VbglR3/VBoxService: Split up and removed redundant code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibMisc.cpp

    r24390 r24686  
    226226
    227227/**
     228 * Looks up the storage path handle (registry).
     229 *
     230 * @returns IPRT status value
     231 * @param   hKey        Receives pointer of allocated version string. NULL is
     232 *                      accepted. The returned pointer must be closed by
     233 *                      vbglR3CloseAdditionsWinStoragePath().
     234 */
     235static int vbglR3GetAdditionsWinStoragePath(PHKEY phKey)
     236{
     237    /*
     238     * Try get the *installed* version first.
     239     */
     240    LONG r;
     241
     242    /* Check the new path first. */
     243    r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
     244# ifdef RT_ARCH_AMD64
     245    if (r != ERROR_SUCCESS)
     246    {
     247        /* Check Wow6432Node (for new entries). */
     248        r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
     249    }
     250# endif
     251
     252    /* Still no luck? Then try the old xVM paths ... */
     253    if (r != ERROR_SUCCESS)
     254    {
     255        r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
     256# ifdef RT_ARCH_AMD64
     257        if (r != ERROR_SUCCESS)
     258        {
     259            /* Check Wow6432Node (for new entries). */
     260            r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
     261        }
     262# endif
     263    }
     264    return RTErrConvertFromNtStatus(r);
     265}
     266
     267
     268/**
     269 * Closes the storage path handle (registry).
     270 *
     271 * @returns IPRT status value
     272 * @param   hKey        Handle to close, retrieved by
     273 *                      vbglR3GetAdditionsWinStoragePath().
     274 */
     275static int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
     276{
     277    return RTErrConvertFromNtStatus(RegCloseKey(hKey));
     278}
     279
     280
     281/**
    228282 * Retrieves the installed Guest Additions version and/or revision.
    229283 *
     
    239293{
    240294#ifdef RT_OS_WINDOWS
    241     /*
    242      * Try get the *installed* version first.
    243      */
    244     HKEY hKey = NULL;
    245     LONG r;
    246 
    247     /* Check the new path first. */
    248     r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, &hKey);
    249 # ifdef RT_ARCH_AMD64
    250     if (r != ERROR_SUCCESS)
    251     {
    252         /* Check Wow6432Node (for new entries). */
    253         r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, &hKey);
    254     }
    255 # endif
    256 
    257     /* Still no luck? Then try the old xVM paths ... */
    258     if (r != ERROR_SUCCESS)
    259     {
    260         r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, &hKey);
    261 # ifdef RT_ARCH_AMD64
    262         if (r != ERROR_SUCCESS)
    263         {
    264             /* Check Wow6432Node (for new entries). */
    265             r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, &hKey);
    266         }
    267 # endif
    268     }
    269 
    270     /* Did we get something worth looking at? */
    271     int rc = VINF_SUCCESS;
    272     if (r == ERROR_SUCCESS)
     295    HKEY hKey;
     296    int rc = vbglR3GetAdditionsWinStoragePath(&hKey);
     297    if (RT_SUCCESS(rc))
    273298    {
    274299        /* Version. */
     300        LONG l;
    275301        DWORD dwType;
    276302        DWORD dwSize = 32;
     
    281307            if (pszTmp)
    282308            {
    283                 r = RegQueryValueEx(hKey, "Version", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
    284                 if (r == ERROR_SUCCESS)
     309                l = RegQueryValueEx(hKey, "Version", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
     310                if (l == ERROR_SUCCESS)
    285311                {
    286312                    if (dwType == REG_SZ)
     
    291317                else
    292318                {
    293                     rc = RTErrConvertFromNtStatus(r);
     319                    rc = RTErrConvertFromNtStatus(l);
    294320                }
    295321                RTMemFree(pszTmp);
     
    305331            if (pszTmp)
    306332            {
    307                 r = RegQueryValueEx(hKey, "Revision", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
    308                 if (r == ERROR_SUCCESS)
     333                l = RegQueryValueEx(hKey, "Revision", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
     334                if (l == ERROR_SUCCESS)
    309335                {
    310336                    if (dwType == REG_SZ)
     
    315341                else
    316342                {
    317                     rc = RTErrConvertFromNtStatus(r);
     343                    rc = RTErrConvertFromNtStatus(l);
    318344                }
    319345                RTMemFree(pszTmp);
     
    328354            }
    329355        }
    330         if (hKey != NULL)
    331             RegCloseKey(hKey);
     356        rc = vbglR3CloseAdditionsWinStoragePath(hKey);
    332357    }
    333358    else
     
    349374}
    350375
     376
     377/**
     378 * Retrieves the installation path of Guest Additions.
     379 *
     380 * @returns IPRT status value
     381 * @param   ppszPath    Receives pointer of allocated installation path string.
     382 *                      The returned pointer must be freed using
     383 *                      RTStrFree().
     384 */
     385VBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
     386{
     387    int rc;
     388#ifdef RT_OS_WINDOWS
     389    HKEY hKey;
     390    rc = vbglR3GetAdditionsWinStoragePath(&hKey);
     391    if (RT_SUCCESS(rc))
     392    {
     393        /* Installation directory. */
     394        DWORD dwType;
     395        DWORD dwSize = _MAX_PATH * sizeof(char);
     396        char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
     397        if (pszTmp)
     398        {
     399            LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
     400            if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
     401            {
     402                rc = RTErrConvertFromNtStatus(l);
     403            }
     404            else
     405            {
     406                if (dwType == REG_SZ)
     407                    rc = RTStrDupEx(ppszPath, pszTmp);
     408                else
     409                    rc = VERR_INVALID_PARAMETER;
     410                if (RT_SUCCESS(rc))
     411                {
     412                    /* Flip slashes. */
     413                    for (char* pszTmp2 = ppszPath[0]; ppszPath; ++ppszPath)
     414                        if (*pszTmp2 == '\\')
     415                            *pszTmp2 = '/';
     416                }
     417            }
     418            RTMemFree(pszTmp);
     419        }
     420        else
     421            rc = VERR_NO_MEMORY;
     422        rc = vbglR3CloseAdditionsWinStoragePath(hKey);
     423    }
     424#else
     425    /** @todo implement me */
     426    rc = VINF_NOT_IMPLEMENTED;
     427#endif
     428    return rc;
     429}
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