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.

182 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. handle.c
  5. Abstract:
  6. Example of a test code for detached, background, no-window command
  7. exectution.
  8. Author:
  9. Vladimir Z. Vulovic (vladimv) 06 - November - 1992
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. 06-Nov-1992 vladimv
  14. Created
  15. --*/
  16. #include "atclient.h"
  17. #include <stdlib.h> // exit()
  18. #include <stdio.h> // printf
  19. #include <wincon.h> // FreeConsole()
  20. WCHAR *
  21. AsciizToUnicode(
  22. IN CHAR * Asciiz
  23. )
  24. /*++
  25. No attempt is made to clean up if we fail half way along. Cleanup
  26. will be done at the exit process time.
  27. --*/
  28. {
  29. UNICODE_STRING UnicodeString;
  30. BOOL success;
  31. RtlInitUnicodeString(
  32. &UnicodeString,
  33. NULL
  34. );
  35. //
  36. // This call will attempt to allocate memory for UNICODE string.
  37. //
  38. success = RtlCreateUnicodeStringFromAsciiz(
  39. &UnicodeString,
  40. Asciiz
  41. );
  42. if ( success != TRUE) {
  43. printf( "Failed to make unicode string out of %s\n", Asciiz);
  44. return( NULL);
  45. }
  46. return( UnicodeString.Buffer);
  47. }
  48. WCHAR **
  49. ArgvToUnicode(
  50. IN int argc,
  51. IN CHAR ** charArgv
  52. )
  53. /*++
  54. No attempt is made to clean up if we fail half way along. Cleanup
  55. will be done at the exit process time.
  56. --*/
  57. {
  58. WCHAR ** argv;
  59. int index;
  60. argv = (WCHAR **)LocalAlloc(
  61. LMEM_FIXED,
  62. argc * sizeof( WCHAR *)
  63. );
  64. if ( argv == NULL) {
  65. return( NULL);
  66. }
  67. for ( index = 0; index < argc; index++ ) {
  68. if ( ( argv[ index] = AsciizToUnicode( charArgv[ index])) == NULL) {
  69. return( NULL);
  70. }
  71. }
  72. return( argv);
  73. }
  74. VOID __cdecl
  75. main(
  76. int argc,
  77. CHAR ** charArgv
  78. )
  79. {
  80. #define THIRTY_SECONDS (30 * 1000)
  81. PROCESS_INFORMATION ProcessInformation;
  82. STARTUPINFO StartupInfo;
  83. HANDLE FileHandle;
  84. WCHAR ** argv;
  85. if ( argc < 2 || argc > 3) {
  86. printf(" Usage: handle command_to_execute [log_file_to_open]\n");
  87. exit( -1);
  88. }
  89. if ( ( argv = ArgvToUnicode( argc, charArgv)) == NULL) {
  90. printf( "Failed to map input strings to unicode.\n");
  91. exit( -1);
  92. }
  93. if ( !FreeConsole()) {
  94. KdPrint(( " FreeConsole() fails with WinError = %d\n", GetLastError()));
  95. exit( -1);
  96. }
  97. FileHandle = CreateFile(
  98. argc > 2 ? argv[ 2] : L"out.txt", // lpszName
  99. GENERIC_READ | GENERIC_WRITE, // fdwAccess
  100. FILE_SHARE_READ | FILE_SHARE_WRITE, // fdwShareMode
  101. NULL, // lpsa
  102. OPEN_ALWAYS, // fdwCreate
  103. FILE_ATTRIBUTE_NORMAL, // fdwAttrAndFlags
  104. NULL // hTemplateFile
  105. );
  106. if ( FileHandle == INVALID_HANDLE_VALUE) {
  107. KdPrint(( " CreateFile() fails with WinError = %d\n", GetLastError()));
  108. exit( -1);
  109. }
  110. if ( !SetStdHandle( STD_INPUT_HANDLE, FileHandle)) {
  111. KdPrint(( " SetStdHandle( STDIN) fails with WinError = %d\n", GetLastError()));
  112. exit( -1);
  113. }
  114. if ( !SetStdHandle( STD_OUTPUT_HANDLE, FileHandle)) {
  115. KdPrint(( " SetStdHandle( STDOUT) fails with WinError = %d\n", GetLastError()));
  116. exit( -1);
  117. }
  118. if ( !SetStdHandle( STD_ERROR_HANDLE, FileHandle)) {
  119. KdPrint(( " SetStdHandle( STDERR) fails with WinError = %d\n", GetLastError()));
  120. exit( -1);
  121. }
  122. GetStartupInfo( &StartupInfo);
  123. StartupInfo.lpTitle = NULL;
  124. if ( !CreateProcess(
  125. NULL, // image name is imbedded in the command line
  126. argv[ 1], // command line
  127. NULL, // pSecAttrProcess
  128. NULL, // pSecAttrThread
  129. TRUE, // this process will not inherit our handles
  130. DETACHED_PROCESS | CREATE_NO_WINDOW, // we have no access to the console either
  131. NULL, // pEnvironment
  132. NULL, // pCurrentDirectory
  133. &StartupInfo,
  134. &ProcessInformation
  135. )) {
  136. KdPrint(( " CreateProcess() fails with winError = %d\n", GetLastError()));
  137. exit( -1);
  138. }
  139. WaitForSingleObject( ProcessInformation.hProcess, THIRTY_SECONDS);
  140. exit( 0);
  141. }