Source code of Windows XP (NT5)
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.

132 lines
2.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. FailOpenFile.cpp
  5. Abstract:
  6. Force OpenFile to fail for the specified files.
  7. History:
  8. 01/31/2001 robkenny created
  9. 03/13/2001 robkenny Converted to CString
  10. --*/
  11. #include "precomp.h"
  12. #include "charvector.h"
  13. // This module has been given an official blessing to use the str routines.
  14. #include "LegalStr.h"
  15. IMPLEMENT_SHIM_BEGIN(FailOpenFile)
  16. #include "ShimHookMacro.h"
  17. APIHOOK_ENUM_BEGIN
  18. APIHOOK_ENUM_ENTRY(OpenFile )
  19. APIHOOK_ENUM_END
  20. CharVector * g_FailList = NULL;
  21. HFILE
  22. APIHOOK(OpenFile)(
  23. LPCSTR lpFileName, // file name
  24. LPOFSTRUCT lpReOpenBuff, // file information
  25. UINT uStyle // action and attributes
  26. )
  27. {
  28. int i;
  29. for (i = 0; i < g_FailList->Size(); ++i)
  30. {
  31. // Compare each fail name against the end of lpFileName
  32. const char * failName = g_FailList->Get(i);
  33. size_t failNameLen = strlen(failName);
  34. size_t fileNameLen = strlen(lpFileName);
  35. if (fileNameLen >= failNameLen)
  36. {
  37. if (_strnicmp(failName, lpFileName+fileNameLen-failNameLen, failNameLen) == 0)
  38. {
  39. // Force OpenFile to fail for this filename
  40. DPFN( eDbgLevelError, "Forcing OpenFile(%s) to fail", lpFileName);
  41. return FALSE;
  42. }
  43. }
  44. }
  45. HFILE returnValue = ORIGINAL_API(OpenFile)(lpFileName, lpReOpenBuff, uStyle);
  46. return returnValue;
  47. }
  48. /*++
  49. Parse the command line, push each filename onto the end of the g_FailList.
  50. --*/
  51. BOOL ParseCommandLine(const char * cl)
  52. {
  53. g_FailList = new CharVector;
  54. if (!g_FailList)
  55. {
  56. return FALSE;
  57. }
  58. if (cl != NULL && *cl)
  59. {
  60. int argc = 0;
  61. LPSTR * argv = _CommandLineToArgvA(cl, & argc);
  62. if (argv)
  63. {
  64. for (int i = 0; i < argc; ++i)
  65. {
  66. if (!g_FailList->Append(argv[i]))
  67. {
  68. // Memory failure
  69. delete g_FailList;
  70. g_FailList = NULL;
  71. break;
  72. }
  73. DPFN( eDbgLevelSpew, "Adding %s to fail list", argv[i]);
  74. }
  75. LocalFree(argv);
  76. }
  77. }
  78. return g_FailList != NULL;
  79. }
  80. /*++
  81. Handle attach and detach
  82. --*/
  83. BOOL
  84. NOTIFY_FUNCTION(DWORD fdwReason)
  85. {
  86. if (fdwReason == DLL_PROCESS_ATTACH)
  87. {
  88. return ParseCommandLine(COMMAND_LINE);
  89. }
  90. return TRUE;
  91. }
  92. /*++
  93. Register hooked functions
  94. --*/
  95. HOOK_BEGIN
  96. CALL_NOTIFY_FUNCTION
  97. APIHOOK_ENTRY(KERNEL32.DLL, OpenFile)
  98. HOOK_END
  99. IMPLEMENT_SHIM_END