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.

191 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1995-1999 Microsoft Corporation
  3. Module Name:
  4. exec.cxx
  5. Abstract:
  6. This module contains an NTSD debugger extension for executing
  7. external commands.
  8. Author:
  9. Keith Moore (keithmo) 12-Nov-1997
  10. Revision History:
  11. --*/
  12. #include "inetdbgp.h"
  13. /************************************************************
  14. * Execute
  15. ************************************************************/
  16. DECLARE_API( exec )
  17. /*++
  18. Routine Description:
  19. This function is called as an NTSD extension to ...
  20. Arguments:
  21. hCurrentProcess - Supplies a handle to the current process (at the
  22. time the extension was called).
  23. hCurrentThread - Supplies a handle to the current thread (at the
  24. time the extension was called).
  25. CurrentPc - Supplies the current pc at the time the extension is
  26. called.
  27. lpExtensionApis - Supplies the address of the functions callable
  28. by this extension.
  29. lpArgumentString - Supplies the asciiz string that describes the
  30. ansi string to be dumped.
  31. Return Value:
  32. None.
  33. --*/
  34. {
  35. BOOL result;
  36. STARTUPINFO startInfo;
  37. PROCESS_INFORMATION processInfo;
  38. INIT_API();
  39. //
  40. // Skip leading blanks.
  41. //
  42. while( *lpArgumentString == ' ' ||
  43. *lpArgumentString == '\t' ) {
  44. lpArgumentString++;
  45. }
  46. //
  47. // Use "cmd.exe" by default.
  48. //
  49. if( *lpArgumentString == '\0' ) {
  50. lpArgumentString = "cmd";
  51. }
  52. //
  53. // Set the prompt environment variable so the user will have clue.
  54. //
  55. SetEnvironmentVariable(
  56. "PROMPT",
  57. "!inetdbg.exec - $p$g"
  58. );
  59. //
  60. // Launch it.
  61. //
  62. ZeroMemory(
  63. &startInfo,
  64. sizeof(startInfo)
  65. );
  66. ZeroMemory(
  67. &processInfo,
  68. sizeof(processInfo)
  69. );
  70. startInfo.cb = sizeof(startInfo);
  71. result = CreateProcess(
  72. NULL, // lpszImageName
  73. lpArgumentString, // lpszCommandLine
  74. NULL, // lpsaProcess
  75. NULL, // lpsaThread
  76. TRUE, // fInheritHandles
  77. 0, // fdwCreate
  78. NULL, // lpvEnvironment
  79. NULL, // lpszCurDir
  80. &startInfo, // lpsiStartInfo
  81. &processInfo // lppiProcessInfo
  82. );
  83. if( result ) {
  84. //
  85. // Wait for the child process to terminate, then cleanup.
  86. //
  87. WaitForSingleObject( processInfo.hProcess, INFINITE );
  88. CloseHandle( processInfo.hProcess );
  89. CloseHandle( processInfo.hThread );
  90. } else {
  91. //
  92. // Could not launch the process.
  93. //
  94. dprintf(
  95. "cannot launch \"%s\", error %lu\n",
  96. lpArgumentString,
  97. GetLastError()
  98. );
  99. }
  100. } // DECLARE_API( exec )
  101. /*
  102. CallExtension allows one extension to call another extension in a
  103. different DLL. From TomCan.
  104. DECLARE_API(test)
  105. {
  106. NTSTATUS status = ERROR_SUCCESS;
  107. status = DO_EXTENSION("kdextx86.dll", "pool", args);
  108. dprintf("\n\nStatus: %08X\n", status);
  109. }
  110. */
  111. NTSTATUS
  112. CallExtension(
  113. PCSTR szModuleName,
  114. PCSTR szFunction,
  115. HANDLE hCurrentProcess,
  116. HANDLE hCurrentThread,
  117. ULONG dwCurrentPc,
  118. ULONG dwProcessor,
  119. PCSTR args)
  120. {
  121. PWINDBG_EXTENSION_FUNCTION pFn = NULL;
  122. HINSTANCE hlib = NULL;
  123. NTSTATUS status = ERROR_SUCCESS;
  124. hlib = LoadLibrary(szModuleName);
  125. if (hlib != NULL)
  126. {
  127. pFn = (PWINDBG_EXTENSION_FUNCTION)GetProcAddress(hlib, szFunction);
  128. if (pFn != NULL)
  129. {
  130. (pFn)(hCurrentProcess, hCurrentThread, dwCurrentPc,
  131. dwProcessor, args);
  132. }
  133. FreeLibrary(hlib);
  134. }
  135. status = GetLastError();
  136. return status;
  137. }