VirtualBox

Changeset 41919 in vbox for trunk/src/VBox/Runtime/r3/win


Ignore:
Timestamp:
Jun 27, 2012 8:06:27 AM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
78783
Message:

Runtime/process: Add RTProcQueryUsername to query the username of the current process

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/win/process-win.cpp

    r41182 r41919  
    3838#include <errno.h>
    3939#include <Strsafe.h>
     40#include <Lmcons.h>
    4041
    4142#include <iprt/process.h>
     
    14241425}
    14251426
     1427
     1428RTR3DECL(int) RTProcQueryUsername(RTPROCESS hProcess, char *pszUser, size_t cbUser,
     1429                                  size_t *pcbUser)
     1430{
     1431    AssertPtrReturn(pszUser, VERR_INVALID_POINTER);
     1432    AssertReturn(cbUser > 0, VERR_INVALID_PARAMETER);
     1433    AssertPtrReturn(pcbUser, VERR_INVALID_POINTER);
     1434
     1435    if (hProcess != RTProcSelf())
     1436        return VERR_NOT_SUPPORTED;
     1437
     1438    RTUTF16 awszUserName[UNLEN + 1];
     1439    DWORD   cchUserName = UNLEN + 1;
     1440
     1441    if (!GetUserNameW(&awszUserName[0], &cchUserName))
     1442        return RTErrConvertFromWin32(GetLastError());
     1443
     1444    char *pszUserName = NULL;
     1445    int rc = RTUtf16ToUtf8(awszUserName, &pszUserName);
     1446    if (RT_SUCCESS(rc))
     1447    {
     1448        size_t cbUserName = strlen(pszUserName) + 1;
     1449
     1450        *pcbUser = cbUserName;
     1451
     1452        if (cbUserName > cbUser)
     1453            rc = VERR_BUFFER_OVERFLOW;
     1454        else
     1455        {
     1456            memcpy(pszUser, pszUserName, cbUserName);
     1457            rc = VINF_SUCCESS;
     1458        }
     1459
     1460        RTStrFree(pszUserName);
     1461    }
     1462
     1463    return rc;
     1464}
     1465
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