VirtualBox

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


Ignore:
Timestamp:
Jul 20, 2022 2:27:44 AM (3 years ago)
Author:
vboxsync
Message:

IPRT/process-win.cpp: Use the wide-char version of Process32First and Process32Next, that'll avoid the need for _stricmp. (Geoff claims the non-wide APIs were available in NT4, but I cannot find them there.) bugref:10261

File:
1 edited

Legend:

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

    r95736 r95738  
    7373//typedef DWORD   (WINAPI *PFNWTSGETACTIVECONSOLESESSIONID)(VOID);
    7474typedef HANDLE  (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
    75 typedef BOOL    (WINAPI *PFNPROCESS32FIRST)(HANDLE, LPPROCESSENTRY32);
    7675typedef BOOL    (WINAPI *PFNPROCESS32FIRSTW)(HANDLE, LPPROCESSENTRY32W);
    77 typedef BOOL    (WINAPI *PFNPROCESS32NEXT)(HANDLE, LPPROCESSENTRY32);
    7876typedef BOOL    (WINAPI *PFNPROCESS32NEXTW)(HANDLE, LPPROCESSENTRY32W);
    7977
    8078/* psapi.dll: */
    8179typedef BOOL    (WINAPI *PFNENUMPROCESSES)(LPDWORD, DWORD, LPDWORD);
    82 typedef DWORD   (WINAPI *PFNGETMODULEBASENAME)(HANDLE, HMODULE, LPTSTR, DWORD);
     80typedef DWORD   (WINAPI *PFNGETMODULEBASENAMEW)(HANDLE, HMODULE, LPWSTR, DWORD);
    8381
    8482/* advapi32.dll: */
     
    133131/* kernel32.dll: */
    134132static PFNCREATETOOLHELP32SNAPSHOT      g_pfnCreateToolhelp32Snapshot   = NULL;
    135 static PFNPROCESS32FIRST                g_pfnProcess32First             = NULL;
    136 static PFNPROCESS32NEXT                 g_pfnProcess32Next              = NULL;
    137133static PFNPROCESS32FIRSTW               g_pfnProcess32FirstW            = NULL;
    138134static PFNPROCESS32NEXTW                g_pfnProcess32NextW             = NULL;
    139135/* psapi.dll: */
    140 static PFNGETMODULEBASENAME             g_pfnGetModuleBaseName          = NULL;
     136static PFNGETMODULEBASENAMEW            g_pfnGetModuleBaseNameW         = NULL;
    141137static PFNENUMPROCESSES                 g_pfnEnumProcesses              = NULL;
    142138/* advapi32.dll: */
     
    317313     */
    318314    g_pfnCreateToolhelp32Snapshot   = (PFNCREATETOOLHELP32SNAPSHOT)GetProcAddress(g_hModKernel32, "CreateToolhelp32Snapshot");
    319     g_pfnProcess32First             = (PFNPROCESS32FIRST          )GetProcAddress(g_hModKernel32, "Process32First");
    320315    g_pfnProcess32FirstW            = (PFNPROCESS32FIRSTW         )GetProcAddress(g_hModKernel32, "Process32FirstW");
    321     g_pfnProcess32Next              = (PFNPROCESS32NEXT           )GetProcAddress(g_hModKernel32, "Process32Next");
    322316    g_pfnProcess32NextW             = (PFNPROCESS32NEXTW          )GetProcAddress(g_hModKernel32, "Process32NextW");
    323317
     
    326320     */
    327321    if (   !g_pfnCreateToolhelp32Snapshot
    328         || !g_pfnProcess32First
    329         || !g_pfnProcess32Next)
    330     {
    331         Assert(!g_pfnCreateToolhelp32Snapshot && !g_pfnProcess32First && !g_pfnProcess32Next);
     322        || !g_pfnProcess32FirstW
     323        || !g_pfnProcess32NextW)
     324    {
     325        Assert(!g_pfnCreateToolhelp32Snapshot && !g_pfnProcess32FirstW && !g_pfnProcess32NextW);
    332326
    333327        rc = RTLdrLoadSystem("psapi.dll", true /*fNoUnload*/, &hMod);
    334328        if (RT_SUCCESS(rc))
    335329        {
    336             rc = RTLdrGetSymbol(hMod, "GetModuleBaseNameA", (void **)&g_pfnGetModuleBaseName);
    337             AssertStmt(RT_SUCCESS(rc), g_pfnGetModuleBaseName = NULL);
     330            rc = RTLdrGetSymbol(hMod, "GetModuleBaseNameW", (void **)&g_pfnGetModuleBaseNameW);
     331            AssertStmt(RT_SUCCESS(rc), g_pfnGetModuleBaseNameW = NULL);
    338332
    339333            rc = RTLdrGetSymbol(hMod, "EnumProcesses", (void **)&g_pfnEnumProcesses);
     
    622616     * Load PSAPI.DLL and resolve the two symbols we need.
    623617     */
    624     if (   !g_pfnGetModuleBaseName
     618    if (   !g_pfnGetModuleBaseNameW
    625619        || !g_pfnEnumProcesses)
    626620        return false;
     
    659653         * than RTPATH_MAX.
    660654         */
    661         DWORD cbProcName  = RTPATH_MAX;
    662         char *pszProcName = (char *)RTMemTmpAlloc(RTPATH_MAX);
    663         if (pszProcName)
     655        PRTUTF16 pwszProcName = (PRTUTF16)RTMemTmpAllocZ(RTPATH_MAX * sizeof(pwszProcName[0]));
     656        if (pwszProcName)
    664657        {
    665658            for (size_t i = 0; papszNames[i] && !fFound; i++)
     
    671664                    if (hProc)
    672665                    {
    673                         *pszProcName = '\0';
    674                         DWORD cbRet = g_pfnGetModuleBaseName(hProc, 0 /*hModule = exe */, pszProcName, cbProcName);
     666                        *pwszProcName = '\0';
     667                        DWORD cbRet = g_pfnGetModuleBaseNameW(hProc, 0 /*hModule = exe */, pwszProcName, RTPATH_MAX);
    675668                        if (   cbRet > 0
    676                             && _stricmp(pszProcName, papszNames[i]) == 0
     669                            && RTUtf16ICmpAscii(pwszProcName, papszNames[i]) == 0
    677670                            && RT_SUCCESS(rtProcWinGetProcessTokenHandle(paPids[iPid], pSid, UINT32_MAX, phToken)))
    678671                            fFound = true;
     
    681674                }
    682675            }
    683             RTMemTmpFree(pszProcName);
     676            RTMemTmpFree(pwszProcName);
    684677        }
    685678        else
     
    718711     */
    719712    bool fFallback = true;
    720     if (g_pfnProcess32Next && g_pfnProcess32First && g_pfnCreateToolhelp32Snapshot)
     713    if (g_pfnProcess32NextW && g_pfnProcess32FirstW && g_pfnCreateToolhelp32Snapshot)
    721714    {
    722715        HANDLE hSnap = g_pfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
     
    727720            for (size_t i = 0; papszNames[i] && !fFound; i++)
    728721            {
    729                 PROCESSENTRY32 ProcEntry;
     722                PROCESSENTRY32W ProcEntry;
    730723                ProcEntry.dwSize = sizeof(PROCESSENTRY32);
    731 /** @todo use W APIs here.   */
    732                 if (g_pfnProcess32First(hSnap, &ProcEntry))
     724                ProcEntry.szExeFile[0] = '\0';
     725                if (g_pfnProcess32FirstW(hSnap, &ProcEntry))
    733726                {
    734727                    do
    735728                    {
    736                         if (_stricmp(ProcEntry.szExeFile, papszNames[i]) == 0)
     729                        if (RTUtf16ICmpAscii(ProcEntry.szExeFile, papszNames[i]) == 0)
    737730                        {
    738731                            int rc = rtProcWinGetProcessTokenHandle(ProcEntry.th32ProcessID, pSid, idDesiredSession, phToken);
     
    743736                            }
    744737                        }
    745                     } while (g_pfnProcess32Next(hSnap, &ProcEntry));
     738                    } while (g_pfnProcess32NextW(hSnap, &ProcEntry));
    746739                }
    747 #ifdef RT_STRICT
    748740                else
    749                 {
    750                     DWORD dwErr = GetLastError();
    751                     AssertMsgFailed(("dwErr=%u (%x)\n", dwErr, dwErr));
    752                 }
    753 #endif
     741                    AssertMsgFailed(("dwErr=%u (%x)\n", GetLastError(), GetLastError()));
    754742            }
    755743            CloseHandle(hSnap);
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