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.

190 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. EmulateGetCommandLine.cpp
  5. Abstract:
  6. This app uses GetCommandLine() to figure out what the drive letter of the
  7. CD-ROM is. Unfortunately the behaviour of this API is different from Win9x
  8. to NT:
  9. Original command line:
  10. E:\Final Doom\Doom95.exe -dm -cdrom
  11. NT's GetCommandLine() returns:
  12. Doom95.exe -dm -cdrom
  13. Win9x's GetCommandLine() returns:
  14. E:\FINALD~1\DOOM95.EXE -dm -cdrom
  15. This app returns short pathnames for GetCommandLine and GetModuleFileName.
  16. Notes:
  17. This is a general purpose shim.
  18. Created:
  19. 01/03/2000 markder Created
  20. 09/26/2000 mnikkel GetModuleFileName added
  21. 11/10/2000 robkenny Fixed PREFIX bugs, removed W routines.
  22. 11/21/2000 prashkud Fixed the GetCommandLineA hook bug when the CommandLine
  23. had the executable name/path with spaces. Used
  24. AppAndCommandLine functions.
  25. 02/27/2001 robkenny Converted to use CString
  26. 05/02/2001 pierreys If buffer is too small, GetModuleFileNameA puts \0 at end of it like 9X.
  27. --*/
  28. #include "precomp.h"
  29. IMPLEMENT_SHIM_BEGIN(EmulateGetCommandLine)
  30. #include "ShimHookMacro.h"
  31. APIHOOK_ENUM_BEGIN
  32. APIHOOK_ENUM_ENTRY(GetCommandLineA)
  33. APIHOOK_ENUM_ENTRY(GetModuleFileNameA)
  34. APIHOOK_ENUM_END
  35. char * g_lpszCommandLine = NULL;
  36. /*++
  37. This stub function appends the commandline returned from GetCommandLine() to a
  38. pre-determined path to emulate Win9x behavior.
  39. --*/
  40. LPSTR
  41. APIHOOK(GetCommandLineA)(
  42. void
  43. )
  44. {
  45. // Been here, done that
  46. if (g_lpszCommandLine)
  47. {
  48. return g_lpszCommandLine;
  49. }
  50. LPSTR lpszOrig = ORIGINAL_API(GetCommandLineA)();
  51. // Seperate the app name and command line
  52. AppAndCommandLine AppCmdLine(NULL, lpszOrig);
  53. CSTRING_TRY
  54. {
  55. // retrieve the original command line
  56. CString csAppName(AppCmdLine.GetApplicationName());
  57. if (csAppName.Find(L' ') == -1)
  58. {
  59. // If no spaces in app name, return the original command line.
  60. g_lpszCommandLine = lpszOrig;
  61. }
  62. else
  63. {
  64. // Spaces found so return short app path name
  65. // and rest of original command line
  66. csAppName.GetShortPathName();
  67. csAppName += L" ";
  68. csAppName += AppCmdLine.GetCommandlineNoAppName();
  69. g_lpszCommandLine = csAppName.ReleaseAnsi();
  70. LOGN( eDbgLevelError,
  71. "[GetCommandLineA] Changed Command Line from <%s> to <%s>.",
  72. lpszOrig, g_lpszCommandLine);
  73. }
  74. }
  75. CSTRING_CATCH
  76. {
  77. g_lpszCommandLine = lpszOrig;
  78. }
  79. return g_lpszCommandLine;
  80. }
  81. DWORD
  82. APIHOOK(GetModuleFileNameA)(
  83. HMODULE hModule, // handle to module
  84. LPSTR lpFilename, // file name of module
  85. DWORD nSize // size of buffer
  86. )
  87. {
  88. CSTRING_TRY
  89. {
  90. CString csExeFileName;
  91. DWORD len;
  92. len = csExeFileName.GetModuleFileNameW(hModule);
  93. CString csLongFileName(csExeFileName);
  94. if (csExeFileName.Find(L' ') > -1)
  95. {
  96. // Spaces found so return short app path name
  97. // The return value
  98. len = csExeFileName.GetShortPathNameW();
  99. LOGN(
  100. eDbgLevelError,
  101. "[GetModuleFileNameA] Changed <%s> to <%s>.",
  102. csLongFileName.GetAnsi(), csExeFileName.GetAnsi());
  103. }
  104. //
  105. // From 9X's PELDR.C. If the buffer has no room for the '\0', 9X stuff the 0 at the
  106. // last byte.
  107. //
  108. if (nSize) {
  109. // len = pmte->iFileNameLen;
  110. if (len >= nSize) {
  111. len = nSize - 1;
  112. LOGN(eDbgLevelError,
  113. "[GetModuleFileNameA] Will shorten <%s> to %d characters.",
  114. csLongFileName.GetAnsi(), len);
  115. }
  116. RtlCopyMemory(lpFilename, csExeFileName.GetAnsi() /* pmte->cfhid.lpFilename */, len);
  117. lpFilename[len] = 0;
  118. }
  119. // Returned the double buffered name len.
  120. return len;
  121. }
  122. CSTRING_CATCH
  123. {
  124. // If error return original api.
  125. return ORIGINAL_API(GetModuleFileNameA)(
  126. hModule,
  127. lpFilename,
  128. nSize);
  129. }
  130. }
  131. /*++
  132. Register hooked functions
  133. --*/
  134. HOOK_BEGIN
  135. APIHOOK_ENTRY(KERNEL32.DLL, GetCommandLineA)
  136. APIHOOK_ENTRY(KERNEL32.DLL, GetModuleFileNameA)
  137. HOOK_END
  138. IMPLEMENT_SHIM_END