Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.1 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: DNLProc.cpp
  6. * Content: DirectPlay Lobby Process Functions
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 02/21/00 mjn Created
  12. * 05/08/00 rmt Bug #33616 -- Does not run on Win9X
  13. * 06/28/00 rmt Prefix Bug #38082
  14. * 07/12/00 rmt Fixed lobby launch so only compares first 15 chars (ToolHelp limitation).
  15. * 08/05/00 RichGr IA64: Use %p format specifier in DPFs for 32/64-bit pointers and handles.
  16. *@@END_MSINTERNAL
  17. *
  18. ***************************************************************************/
  19. #include "dnlobbyi.h"
  20. //**********************************************************************
  21. // Constant definitions
  22. //**********************************************************************
  23. //**********************************************************************
  24. // Macro definitions
  25. //**********************************************************************
  26. //**********************************************************************
  27. // Structure definitions
  28. //**********************************************************************
  29. //**********************************************************************
  30. // Variable definitions
  31. //**********************************************************************
  32. //**********************************************************************
  33. // Function prototypes
  34. //**********************************************************************
  35. //**********************************************************************
  36. // Function definitions
  37. //**********************************************************************
  38. #define PROCLIST_MAX_PATH 15
  39. #undef DPF_MODNAME
  40. #define DPF_MODNAME "DPLCompareFilenames"
  41. BOOL DPLCompareFilenames(WCHAR *const pwszFilename1,
  42. WCHAR *const pwszFilename2)
  43. {
  44. WCHAR *p1;
  45. WCHAR *p2;
  46. DNASSERT(pwszFilename1 != NULL);
  47. DNASSERT(pwszFilename2 != NULL);
  48. // Skip path
  49. if ((p1 = wcsrchr(pwszFilename1,L'\\')) == NULL)
  50. p1 = pwszFilename1;
  51. else
  52. p1++;
  53. if ((p2 = wcsrchr(pwszFilename2,L'\\')) == NULL)
  54. p2 = pwszFilename2;
  55. else
  56. p2++;
  57. // if (wcsnicmp(p1,p2,dwLen)==0)
  58. // return(TRUE);
  59. // return(FALSE);
  60. /*dwLen = wcslen(p1);
  61. if (dwLen == 0 || dwLen != wcslen(p2) )
  62. return(FALSE);
  63. while(dwLen)
  64. {
  65. if (towupper(*p1) != towupper(*p2))
  66. return(FALSE);
  67. p1++;
  68. p2++;
  69. dwLen--;
  70. }*/
  71. return (_wcsnicmp(p1,p2,PROCLIST_MAX_PATH) == 0);
  72. }
  73. // ToolHelp Function Pointers.
  74. #ifdef WINCE
  75. typedef BOOL (WINAPI *PFNPROCESS32FIRSTW)(HANDLE,LPPROCESSENTRY32);
  76. typedef BOOL (WINAPI *PFNPROCESS32NEXTW)(HANDLE,LPPROCESSENTRY32);
  77. #else
  78. typedef BOOL (WINAPI *PFNPROCESS32FIRSTW)(HANDLE,LPPROCESSENTRY32W);
  79. typedef BOOL (WINAPI *PFNPROCESS32NEXTW)(HANDLE,LPPROCESSENTRY32W);
  80. #endif // WINCE
  81. #undef DPF_MODNAME
  82. #define DPF_MODNAME "DPLGetProcessList"
  83. HRESULT DPLGetProcessList(WCHAR *const pwszProcess,
  84. DWORD *const prgdwPid,
  85. DWORD *const pdwNumProcesses)
  86. {
  87. HRESULT hResultCode;
  88. BOOL bReturnCode;
  89. HANDLE hSnapshot = NULL; // System snapshot
  90. PROCESSENTRY32 processEntry;
  91. DWORD dwNumProcesses;
  92. PWSTR pwszExeFile = NULL;
  93. DWORD dwExeFileLen;
  94. DPFX(DPFPREP, 3,"Parameters: pwszProcess [0x%p], prgdwPid [0x%p], pdwNumProcesses [0x%p]",
  95. pwszProcess,prgdwPid,pdwNumProcesses);
  96. // Set up to run through process list
  97. hResultCode = DPN_OK;
  98. dwNumProcesses = 0;
  99. hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD,0);
  100. if (hSnapshot < 0)
  101. {
  102. DPFERR("Could not create Snapshot");
  103. hResultCode = DPNERR_OUTOFMEMORY;
  104. goto CLEANUP_GETPROCESS;
  105. }
  106. // Search SnapShot for process list
  107. dwExeFileLen = 0;
  108. pwszExeFile = NULL;
  109. processEntry.dwSize = sizeof(PROCESSENTRY32);
  110. bReturnCode = Process32First(hSnapshot,&processEntry);
  111. DPFX(DPFPREP, 7," dwSize cntUsg PID cntThrds PPID PCB Flags Process");
  112. while (bReturnCode)
  113. {
  114. #ifdef UNICODE
  115. pwszExeFile = processEntry.szExeFile;
  116. #else
  117. // Grow ANSI string as required
  118. if (strlen(processEntry.szExeFile) + 1 > dwExeFileLen)
  119. {
  120. if (pwszExeFile)
  121. DNFree(pwszExeFile);
  122. dwExeFileLen = strlen(processEntry.szExeFile) + 1;
  123. if ((pwszExeFile = static_cast<WCHAR*>(DNMalloc(dwExeFileLen * sizeof(WCHAR)))) == NULL)
  124. {
  125. DPFERR("Could not allocate filename conversion buffer");
  126. hResultCode = DPNERR_OUTOFMEMORY;
  127. goto CLEANUP_GETPROCESS;
  128. }
  129. }
  130. if( FAILED( STR_jkAnsiToWide( pwszExeFile, processEntry.szExeFile, dwExeFileLen ) ) )
  131. {
  132. DPFERR( "Error converting ANSI filename to Unicode" );
  133. hResultCode = DPNERR_CONVERSION;
  134. goto CLEANUP_GETPROCESS;
  135. }
  136. #endif // !UNICODE
  137. // Valid process ?
  138. if (DPLCompareFilenames(pwszProcess,pwszExeFile))
  139. {
  140. // Update lpdwProcessIdList array
  141. if (prgdwPid != NULL && dwNumProcesses < *pdwNumProcesses)
  142. {
  143. prgdwPid[dwNumProcesses] = processEntry.th32ProcessID;
  144. }
  145. else
  146. {
  147. hResultCode = DPNERR_BUFFERTOOSMALL;
  148. }
  149. // Increase valid process count
  150. dwNumProcesses++;
  151. DPFX(DPFPREP, 7,"%8lx %4lx %8lx %4lx %8lx %8lx %8lx %hs",
  152. processEntry.dwSize,processEntry.cntUsage,processEntry.th32ProcessID,
  153. processEntry.cntThreads,processEntry.th32ParentProcessID,
  154. processEntry.pcPriClassBase,processEntry.dwFlags,processEntry.szExeFile);
  155. }
  156. // Get next process
  157. bReturnCode = Process32Next(hSnapshot,&processEntry);
  158. }
  159. if( *pdwNumProcesses < dwNumProcesses )
  160. {
  161. hResultCode = DPNERR_BUFFERTOOSMALL;
  162. }
  163. else
  164. {
  165. hResultCode = DPN_OK;
  166. }
  167. *pdwNumProcesses = dwNumProcesses;
  168. CLEANUP_GETPROCESS:
  169. if( hSnapshot != NULL )
  170. {
  171. #if defined(WINCE) && !defined(WINCE_ON_DESKTOP)
  172. CloseToolhelp32Snapshot(hSnapshot);
  173. #else // !WINCE
  174. CloseHandle(hSnapshot);
  175. #endif // WINCE
  176. }
  177. #ifndef UNICODE
  178. if (pwszExeFile)
  179. {
  180. DNFree(pwszExeFile);
  181. }
  182. #endif // UNICODE
  183. return hResultCode;
  184. }