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.

143 lines
3.1 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include "psapi.h"
  6. DWORD
  7. WINAPI
  8. GetMappedFileNameW(
  9. HANDLE hProcess,
  10. LPVOID lpv,
  11. LPWSTR lpFilename,
  12. DWORD nSize
  13. )
  14. /*++
  15. Routine Description:
  16. The routine gets the file name associated with a mapped section
  17. Arguments:
  18. hProcess - Handle of the process to do the query for
  19. lpv - Address of mapped section to query
  20. lpFilename - Address of the buffer to hold the returned filename
  21. nSize - Size of the returned filename.
  22. Return Value:
  23. DWORD - Zero on error otherwise the size of the data returned.
  24. If the data is truncated the return size is the size of the passed in buffer.
  25. --*/
  26. {
  27. struct {
  28. OBJECT_NAME_INFORMATION ObjectNameInfo;
  29. WCHAR FileName[MAX_PATH];
  30. } s;
  31. NTSTATUS Status;
  32. SIZE_T ReturnLength;
  33. DWORD cb, CopySize;
  34. if (nSize == 0) {
  35. SetLastError (ERROR_INSUFFICIENT_BUFFER);
  36. return 0;
  37. }
  38. //
  39. // See if we can figure out the name associated with
  40. // this mapped region
  41. //
  42. Status = NtQueryVirtualMemory (hProcess,
  43. lpv,
  44. MemoryMappedFilenameInformation,
  45. &s.ObjectNameInfo,
  46. sizeof(s),
  47. &ReturnLength);
  48. if (!NT_SUCCESS (Status)) {
  49. SetLastError (RtlNtStatusToDosError (Status));
  50. return 0;
  51. }
  52. cb = s.ObjectNameInfo.Name.Length / sizeof (WCHAR);
  53. CopySize = cb;
  54. if (nSize < cb + 1) {
  55. CopySize = nSize - 1;
  56. cb = nSize;
  57. SetLastError (ERROR_INSUFFICIENT_BUFFER);
  58. } else {
  59. SetLastError (NO_ERROR);
  60. }
  61. CopyMemory (lpFilename, s.ObjectNameInfo.Name.Buffer, CopySize * sizeof (WCHAR));
  62. lpFilename[CopySize] = UNICODE_NULL;
  63. return cb;
  64. }
  65. DWORD
  66. WINAPI
  67. GetMappedFileNameA(
  68. HANDLE hProcess,
  69. LPVOID lpv,
  70. LPSTR lpFilename,
  71. DWORD nSize
  72. )
  73. /*++
  74. Routine Description:
  75. The routine gets the file name associated with a mapped section
  76. Arguments:
  77. hProcess - Handle of the process to do the query for
  78. lpv - Address of mapped section to query
  79. lpFilename - Address of the buffer to hold the returned filename
  80. nSize - Size of the returned filename.
  81. Return Value:
  82. DWORD - Zero on error otherwise the size of the data returned or required.
  83. If the return value is greater than the input buffer size then the data was truncated.
  84. --*/
  85. {
  86. LPWSTR lpwstr;
  87. DWORD cwch;
  88. DWORD cch;
  89. lpwstr = (LPWSTR) LocalAlloc(LMEM_FIXED, nSize * 2);
  90. if (lpwstr == NULL) {
  91. return(0);
  92. }
  93. cch = cwch = GetMappedFileNameW(hProcess, lpv, lpwstr, nSize);
  94. if (cwch < nSize) {
  95. //
  96. // Include NULL terminator
  97. //
  98. cwch++;
  99. }
  100. if (!WideCharToMultiByte(CP_ACP, 0, lpwstr, cwch, lpFilename, nSize, NULL, NULL)) {
  101. cch = 0;
  102. }
  103. LocalFree((HLOCAL) lpwstr);
  104. return(cch);
  105. }