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.

122 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. SearchPathInAppPaths.cpp
  5. Abstract:
  6. An application might use SearchPath to determine if a specific EXE is found
  7. in the current path. Some applications have registered their path with the
  8. shell in "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths"
  9. If SearchPath fails, we'll check to see if the applications has registered a path.
  10. History:
  11. 03/03/2000 robkenny Created
  12. --*/
  13. #include "precomp.h"
  14. #include <stdio.h>
  15. IMPLEMENT_SHIM_BEGIN(SearchPathInAppPaths)
  16. #include "ShimHookMacro.h"
  17. APIHOOK_ENUM_BEGIN
  18. APIHOOK_ENUM_ENTRY(SearchPathA)
  19. APIHOOK_ENUM_END
  20. DWORD
  21. APIHOOK(SearchPathA)(
  22. LPCSTR lpPath, // search path
  23. LPCSTR lpFileName, // file name
  24. LPCSTR lpExtension, // file extension
  25. DWORD nBufferLength, // size of buffer
  26. LPSTR lpBuffer, // found file name buffer
  27. LPSTR *lpFilePart // file component
  28. )
  29. {
  30. DWORD returnValue = ORIGINAL_API(SearchPathA)(
  31. lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart);
  32. if (returnValue == 0 && lpFileName != NULL)
  33. {
  34. // Search failed, look in the registry.
  35. // First look for lpFileName. If that fails, append lpExtension and look again.
  36. CSTRING_TRY
  37. {
  38. CString csReg(L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
  39. csReg += lpFileName;
  40. // Try looking for lpFileName exactly
  41. CString csValue;
  42. LONG success = RegQueryValueExW(csValue, HKEY_LOCAL_MACHINE, csReg, NULL);
  43. if (success == ERROR_SUCCESS)
  44. {
  45. // Found the value in the registry.
  46. // Verify that there is enough space in the output buffer
  47. if (nBufferLength < (DWORD)csValue.GetLength())
  48. {
  49. // The return value is the size necessary to hold the path.
  50. returnValue = csValue.GetLength() + 1;
  51. }
  52. else
  53. {
  54. StringCchCopyA(lpBuffer, nBufferLength, csValue.GetAnsi());
  55. returnValue = csValue.GetLength();
  56. }
  57. }
  58. if (returnValue == 0 && lpExtension)
  59. {
  60. // Append the extension onto the filename and try again
  61. csReg += lpExtension;
  62. LONG success = RegQueryValueExW(csValue, HKEY_LOCAL_MACHINE, csReg, NULL);
  63. if (success == ERROR_SUCCESS && csValue.GetLength() > 0)
  64. {
  65. // Found the value in the registry.
  66. // Verify that there is enough space in the output buffer
  67. if (nBufferLength < (DWORD)csValue.GetLength())
  68. {
  69. // The return value is the size necessary to hold the path.
  70. returnValue = csValue.GetLength() + 1;
  71. }
  72. else
  73. {
  74. StringCchCopyA(lpBuffer, nBufferLength, csValue.GetAnsi());
  75. returnValue = csValue.GetLength();
  76. }
  77. }
  78. }
  79. }
  80. CSTRING_CATCH
  81. {
  82. // Do nothing
  83. }
  84. }
  85. return returnValue;
  86. }
  87. /*++
  88. Register hooked functions
  89. --*/
  90. HOOK_BEGIN
  91. APIHOOK_ENTRY(KERNEL32.DLL, SearchPathA)
  92. HOOK_END
  93. IMPLEMENT_SHIM_END