dumadetours.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
  3. *
  4. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. // $Id$
  21. #include "stdafx.h"
  22. #include <stdio.h>
  23. #include <windows.h>
  24. #include <detours.h>
  25. #define arrayof(x) (sizeof(x)/sizeof(x[0]))
  26. int main(int argc, char **argv)
  27. {
  28. char* pszDllPath = "dumadetoursdll.dll";
  29. if(getenv("DUMA_DETOURS_DLL"))
  30. pszDllPath = getenv("DUMA_DETOURS_DLL");
  31. printf("\n| DUMA -- Detours DUMA Loader\n| Copyright (c) 2006 Michael Eddington\n\n");
  32. if (argc < 2)
  33. {
  34. printf("\nSyntax: dumadetours.exe [command line]\n\n");
  35. return 1;
  36. }
  37. //////////////////////////////////////////////////////////////////////////
  38. STARTUPINFO si;
  39. PROCESS_INFORMATION pi;
  40. char szCommand[2048];
  41. char szExe[1024];
  42. char szFullExe[1024] = "\0";
  43. char* pszFileExe = NULL;
  44. int cnt = 1;
  45. ZeroMemory(&si, sizeof(si));
  46. ZeroMemory(&pi, sizeof(pi));
  47. si.cb = sizeof(si);
  48. szCommand[0] = L'\0';
  49. strcpy(szExe, argv[cnt]);
  50. for (; cnt < argc; cnt++)
  51. {
  52. if (strchr(argv[cnt], ' ') != NULL || strchr(argv[cnt], '\t') != NULL)
  53. {
  54. strcat(szCommand, "\"");
  55. strcat(szCommand, argv[cnt]);
  56. strcat(szCommand, "\"");
  57. }
  58. else
  59. strcat(szCommand, argv[cnt]);
  60. if (cnt + 1 < argc)
  61. strcat(szCommand, " ");
  62. }
  63. printf("dumadetours.exe: Starting: [%s]\n\n", szCommand);
  64. fflush(stdout);
  65. SetLastError(0);
  66. SearchPath(NULL, szExe, ".exe", arrayof(szFullExe), szFullExe, &pszFileExe);
  67. #ifdef DUMA_DUMA_DETOURS_VERSION == 2.1
  68. if (!DetourCreateProcessWithDll(
  69. szFullExe[0] ? szFullExe : NULL // lpApplicationName
  70. , szCommand // lpCommandLine
  71. , NULL // lpProcessAttributes
  72. , NULL // lpThreadAttributes
  73. , TRUE // bInheritHandles
  74. , CREATE_DEFAULT_ERROR_MODE // dwCreationFlags
  75. , NULL // lpEnvironment
  76. , NULL // lpCurrentDirectory
  77. , &si // lpStartupInfo
  78. , &pi // lpProcessInformation
  79. , NULL // lpDetouredDllPath
  80. , pszDllPath // lpDllName
  81. , NULL
  82. ) ) // pfCreateProcessW
  83. #else
  84. if (!DetourCreateProcessWithDll(
  85. szFullExe[0] ? szFullExe : NULL
  86. , szCommand
  87. , NULL
  88. , NULL
  89. , TRUE
  90. , CREATE_DEFAULT_ERROR_MODE
  91. , NULL
  92. , NULL
  93. , &si
  94. , &pi
  95. , pszDllPath
  96. , NULL
  97. ) )
  98. #endif
  99. {
  100. printf("dumadetours.exe: DetourCreateProcessWithDll failed: %d\n", GetLastError());
  101. ExitProcess(2);
  102. }
  103. WaitForSingleObject(pi.hProcess, INFINITE);
  104. DWORD dwResult = 0;
  105. if (!GetExitCodeProcess(pi.hProcess, &dwResult))
  106. {
  107. printf("dumadetours.exe: GetExitCodeProcess failed: %d\n", GetLastError());
  108. dwResult = 3;
  109. }
  110. return dwResult;
  111. }
  112. // end