[vlc-commits] [Git][videolan/vlc][master] package/win32: only use Windows XP+ API's to look for a process in NSIS

Steve Lhomme (@robUx4) gitlab at videolan.org
Fri Nov 22 08:23:10 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
5c724871 by Steve Lhomme at 2024-11-22T07:44:13+00:00
package/win32: only use Windows XP+ API's to look for a process in NSIS

No need to do some DLL loading/GetProcAddress.
The installer is for Windows XP+ (and Windows 7 on VLC4). So these API's
are always present.

- - - - -


1 changed file:

- extras/package/win32/NSIS/nsProcess/nsProcess.c


Changes:

=====================================
extras/package/win32/NSIS/nsProcess/nsProcess.c
=====================================
@@ -20,74 +20,12 @@
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
-#include <tlhelp32.h>
+#include <psapi.h>
 #include "pluginapi.h"
 
 /* Defines */
 #define NSIS_MAX_STRLEN 1024
 
-#define SystemProcessInformation     5
-#define STATUS_SUCCESS               0x00000000L
-#define STATUS_INFO_LENGTH_MISMATCH  0xC0000004L
-
-typedef struct _SYSTEM_THREAD_INFO {
-  FILETIME ftCreationTime;
-  DWORD dwUnknown1;
-  DWORD dwStartAddress;
-  DWORD dwOwningPID;
-  DWORD dwThreadID;
-  DWORD dwCurrentPriority;
-  DWORD dwBasePriority;
-  DWORD dwContextSwitches;
-  DWORD dwThreadState;
-  DWORD dwUnknown2;
-  DWORD dwUnknown3;
-  DWORD dwUnknown4;
-  DWORD dwUnknown5;
-  DWORD dwUnknown6;
-  DWORD dwUnknown7;
-} SYSTEM_THREAD_INFO;
-
-typedef struct _SYSTEM_PROCESS_INFO {
-  DWORD dwOffset;
-  DWORD dwThreadCount;
-  DWORD dwUnkown1[6];
-  FILETIME ftCreationTime;
-  DWORD dwUnkown2;
-  DWORD dwUnkown3;
-  DWORD dwUnkown4;
-  DWORD dwUnkown5;
-  DWORD dwUnkown6;
-  WCHAR *pszProcessName;
-  DWORD dwBasePriority;
-  DWORD dwProcessID;
-  DWORD dwParentProcessID;
-  DWORD dwHandleCount;
-  DWORD dwUnkown7;
-  DWORD dwUnkown8;
-  DWORD dwVirtualBytesPeak;
-  DWORD dwVirtualBytes;
-  DWORD dwPageFaults;
-  DWORD dwWorkingSetPeak;
-  DWORD dwWorkingSet;
-  DWORD dwUnkown9;
-  DWORD dwPagedPool;
-  DWORD dwUnkown10;
-  DWORD dwNonPagedPool;
-  DWORD dwPageFileBytesPeak;
-  DWORD dwPageFileBytes;
-  DWORD dwPrivateBytes;
-  DWORD dwUnkown11;
-  DWORD dwUnkown12;
-  DWORD dwUnkown13;
-  DWORD dwUnkown14;
-  SYSTEM_THREAD_INFO ati[ANYSIZE_ARRAY];
-} SYSTEM_PROCESS_INFO;
-
-
-/* Include conversion functions */
-//#define xatoi
-//#define xitoa
 
 /* Global variables */
 TCHAR szBuf[NSIS_MAX_STRLEN];
@@ -178,14 +116,14 @@ void NiceTerminate(DWORD id, BOOL bClose, BOOL *bSuccess, BOOL *bFailed)
 	if (bClose)
 		EnumWindows(EnumWindowsProc, (LPARAM)&window);
 	if (window.prev_hwnd != NULL)
-	{	  
+	{
 	  if (GetExitCodeProcess(hProc,&ec) && ec == STILL_ACTIVE)
 		if (WaitForSingleObject(hProc, 3000) == WAIT_OBJECT_0)
 		{
 		  *bSuccess = bDone = TRUE;
 		}
 		else;
-	  else 
+	  else
 	  {
 		  *bSuccess = bDone = TRUE;
 	  }
@@ -256,7 +194,6 @@ int FIND_PROC_BY_NAME(TCHAR *szProcessName, BOOL bTerminate, BOOL bClose)
 {
   TCHAR szName[MAX_PATH];
   OSVERSIONINFO osvi;
-  HMODULE hLib;
   HANDLE hProc;
   ULONG uError;
   BOOL bFound=FALSE;
@@ -271,83 +208,63 @@ int FIND_PROC_BY_NAME(TCHAR *szProcessName, BOOL bTerminate, BOOL bClose)
       osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
     return 605;
 
-  if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
+  size_t process_count = 512;
+  DWORD *processIDs = NULL;
+  for (;;)
   {
-    // WinNT/2000/XP
-
-    SYSTEM_PROCESS_INFO *spi;
-    SYSTEM_PROCESS_INFO *spiCount;
-    DWORD dwSize=0x4000;
-    DWORD dwData;
-    ULONG (WINAPI *NtQuerySystemInformationPtr)(ULONG, PVOID, LONG, PULONG);
-
-    if ((hLib=LoadLibraryW(L"NTDLL.DLL")) != NULL)
+    processIDs = realloc(processIDs, process_count * sizeof(DWORD));
+    if (unlikely(processIDs == NULL))
+        break;
+    DWORD readSize;
+    if (!EnumProcesses(processIDs, process_count*sizeof(DWORD), &readSize))
     {
-      NtQuerySystemInformationPtr=(ULONG(WINAPI *)(ULONG, PVOID, LONG, PULONG))GetProcAddress(hLib, "NtQuerySystemInformation");
-
-      if (NtQuerySystemInformationPtr)
-      {
-        while (1)
-        {
-          if ((spi=LocalAlloc(LMEM_FIXED, dwSize)) != NULL)
-          {
-            uError=(*NtQuerySystemInformationPtr)(SystemProcessInformation, spi, dwSize, &dwData);
-
-            if (uError == STATUS_SUCCESS) break;
-
-            LocalFree(spi);
-
-            if (uError != STATUS_INFO_LENGTH_MISMATCH)
-            {
-              uError=608;
-              break;
-            }
-          }
-          else
-          {
-            uError=608;
-            break;
-          }
-          dwSize*=2;
-        }
-      }
-      else uError=607;
-
-      FreeLibrary(hLib);
+        free(processIDs);
+        processIDs = NULL;
+        break;
+    }
+    if (readSize < process_count * sizeof(DWORD))
+    {
+        process_count = readSize / sizeof(DWORD);
+        break;
     }
-    else uError=606;
-
-    if (uError != STATUS_SUCCESS) return uError;
-
-    spiCount=spi;
 
-    while (1)
+    // there might be more processes
+    process_count *= 2;
+  }
+  if (processIDs != NULL)
+  {
+    const size_t cmpsize = lstrlen(szProcessName);
+    for (size_t i=0; i<process_count; i++)
     {
-      if (spiCount->pszProcessName)
+      hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processIDs[i]);
+      if (hProc != NULL)
       {
-
-#ifdef UNICODE
-	    lstrcpyn(szName, spiCount->pszProcessName, MAX_PATH);
-#else
-	    WideCharToMultiByte(CP_ACP, 0, spiCount->pszProcessName, -1, szName, MAX_PATH, NULL, NULL);
-#endif		
-
-        if (!lstrcmpi(szName, szProcessName))
+        DWORD proclen = sizeof(szName);
+        BOOL got = QueryFullProcessImageName(hProc, PROCESS_NAME_NATIVE, szName, &proclen);
+        CloseHandle(hProc);
+        if (got != FALSE)
         {
-          // Process found
-          bFound=TRUE;
 
-          if (bTerminate == TRUE)
+          if (proclen < cmpsize + 1)
+          {
+            continue;
+          }
+          if (szName[proclen - cmpsize - 1] == TEXT('\\') &&
+              !lstrcmpi(szProcessName, &szName[proclen - cmpsize]))
           {
-			  NiceTerminate(spiCount->dwProcessID, bClose, &bSuccess, &bFailed);
+            // Process found
+            bFound=TRUE;
+
+            if (bTerminate == TRUE)
+            {
+              NiceTerminate(processIDs[i], bClose, &bSuccess, &bFailed);
+            }
+            else break;
           }
-          else break;
         }
       }
-      if (spiCount->dwOffset == 0) break;
-      spiCount=(SYSTEM_PROCESS_INFO *)((char *)spiCount + spiCount->dwOffset);
     }
-    LocalFree(spi);
+    free(processIDs);
   }
 
   if (bFound == FALSE) return 603;



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/5c72487111bf072dec2d24e4a5bd67c2d10d5f4f

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/5c72487111bf072dec2d24e4a5bd67c2d10d5f4f
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list