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.

288 lines
6.9 KiB

  1. // conexec.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "process.h" // for CreateProcess? //for _spawnv //windows.h" // for CreateThread()
  5. #include "mscoree.h"
  6. #import <mscorlib.tlb> raw_interfaces_only high_property_prefixes("_get","_put","_putref")
  7. using namespace ComRuntimeLibrary;
  8. #import <asmexec.tlb>
  9. // !! required !!
  10. #include "Debug/asmexec.tlh"
  11. using namespace asmexec ; //ComHost;
  12. /*#ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif*/
  17. //#using <mscorlib.dll>
  18. //{5F078073-5DCE-36EF-8A36-C03C30980216}
  19. extern const GUID __declspec(selectany) CLSID_AsmExecute = { 0x5f078073, 0x5dce, 0x36ef, { 0x8a, 0x36, 0xc0, 0x3c, 0x30, 0x98, 0x02, 0x16 } };
  20. //{229C7FE0-4744-30C8-81F4-BB0541469CA9}
  21. extern const GUID __declspec(selectany) IID__AsmExecute = { 0x229c7fe0, 0x4744, 0x30c8, { 0x81, 0xf4, 0xbb, 0x05, 0x41, 0x46, 0x9c, 0xa9 } };
  22. //class _AsmExecute;
  23. HRESULT _hr;
  24. _AsmExecute* _pAsmExecute;
  25. bool init()
  26. {
  27. bool ret = true;
  28. //
  29. // Initialize COM
  30. //
  31. _hr = ::CoInitialize(NULL);
  32. if(FAILED(_hr))
  33. {
  34. printf("COM CoInitialize failed...\n");
  35. ret = false;
  36. goto exit;
  37. }
  38. printf("COM CoInitialize succeed\n");
  39. exit:
  40. return ret;
  41. }
  42. bool final()
  43. {
  44. if(SUCCEEDED(_hr))
  45. {
  46. ::CoUninitialize();
  47. printf("COM CoUninitialize called\n");
  48. }
  49. return true;
  50. }
  51. int main(int argc, char* argv[])
  52. {
  53. int ret = -1;
  54. HRESULT hr;
  55. _hr = E_FAIL;
  56. _pAsmExecute = NULL;
  57. printf("CONsoleEXEC\n");
  58. if (argc < 3)
  59. {
  60. printf("syntax: Codebase [+]Flag [Zone] [uniqueId Site]\n + - spawn a new process\n default - execute in this process\n");
  61. goto exit;
  62. }
  63. else
  64. {
  65. for (int i = 1; i < argc; i++)
  66. {
  67. switch(i)
  68. {
  69. case 1:
  70. printf(" Codebase- %s", argv[i]);
  71. break;
  72. case 2:
  73. printf(" Flag- %s", argv[i]);
  74. break;
  75. case 3:
  76. printf(" Zone- %s", argv[i]);
  77. break;
  78. case 4:
  79. printf(" uniqueId- %s", argv[i]);
  80. break;
  81. case 5:
  82. printf(" Site- %s", argv[i]);
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. printf("\n");
  89. }
  90. if (!init())
  91. {
  92. goto exit;
  93. }
  94. if (*argv[2] == '+')
  95. {
  96. // spawn a new process
  97. // printf("Spawnv...\n" );
  98. printf("CreateProcess...\n");
  99. /* // a hack to remove the flag in the spawn process... BUGBUG? use sprintf and _spawnl?
  100. *argv[2] = ' ';
  101. passing the original argv does not work if it originally contains a quoted path, eg. "c:\program files\a.exe"
  102. background: argv[0] is the path to the running program itself
  103. note: if you type> prog "ar g1" arg2
  104. this becomes argv[0] = "prog", argv[1] = "ar g1", arg2 = "arg2"
  105. if you type> "De ug\prog" "ar g1"
  106. this becomes argv[0] = "De ug\prog", argv[1] = "ar g1"
  107. however, spawn will only take non-quoted path (even if there are spaces in it) for the program path
  108. but the spawned conexec choks on the space inside argv[0] (program path) and instead for it
  109. this becomes argv[0] = "De", argv[1] = " ug\prog", argv[2] = "ar g1" - and thus crashes badly
  110. Therefore, keep the original argv[0] for spawn program path, but add quotes when pass as arguments*/
  111. /* char buf0[1025];
  112. char* ptr;
  113. char buf1[1025];
  114. char buf2[1025];
  115. // _spawn limits to 1024+'\0' for total of argv
  116. _snprintf(buf0, 1025, "\"%s\"", argv[0]);
  117. _snprintf(buf1, 1025, "\"%s\"", argv[1]);
  118. _snprintf(buf2, 1025, "%s", argv[2]+1);
  119. // error checking?
  120. // BUGBUG? free?
  121. ptr = argv[0];
  122. argv[0] = buf0;
  123. argv[1] = buf1;
  124. argv[2] = buf2;
  125. // _P_OVERLAY? _P_WAIT?
  126. if (_spawnv(_P_NOWAIT, ptr, argv) == -1)
  127. {
  128. printf("Spawnv failed. errno = %x\n", errno);
  129. goto exit;
  130. }
  131. // how to start in a new console??
  132. printf("Spawnv succeed. This process will terminate\n" );
  133. ret = 0;
  134. goto exit;*/
  135. STARTUPINFO si;
  136. PROCESS_INFORMATION pi;
  137. ZeroMemory( &si, sizeof(si) );
  138. si.cb = sizeof(si);
  139. ZeroMemory( &pi, sizeof(pi) );
  140. // char szAppName[1025];
  141. char szCmdLine[1025];
  142. int len = 0;
  143. /* if ((len = _snprintf(szAppName, 1025, "\"%s\"", argv[0])) <= 0)
  144. {
  145. printf("Application name too long > 1024\n");
  146. goto exit;
  147. }
  148. strcpy(szCmdLine, szAppName);
  149. printf("\n\n%d\n", len);
  150. szCmdLine[len] = ' ';
  151. szCmdLine[len+1] = '0';
  152. szCmdLine[len+2] = '\0';*/
  153. for (int i = 0; i < argc; i++)
  154. {
  155. // a small hack: "conexec.exe" "asm.exe" 0<-- 2 spaces before "0"
  156. // for this for-loop to work w/o another condition for i=0
  157. if ((len += _snprintf(szCmdLine+len,
  158. 1025-len,
  159. (i <= 1 ? "\"%s\" " : " %s"),
  160. (i == 2 ? argv[i] + 1 : argv[i]))) >= 1025)
  161. {
  162. printf("Command line too long > 1024\n");
  163. goto exit;
  164. }
  165. }
  166. // Start the child process.
  167. if( !CreateProcess( NULL, // Use command line instead to avoid ambigous naming and allow the use of quotes.//szAppName, //argv[0], // Myself. This CANNOT have quotes
  168. szCmdLine, // Command line.
  169. NULL, // Process handle not inheritable. security??
  170. NULL, // Thread handle not inheritable. security??
  171. FALSE, // Set handle inheritance to FALSE.
  172. CREATE_NEW_CONSOLE, //DETACHED_PROCESS, // No access to current console.
  173. NULL, // Use parent's environment block.
  174. NULL, // Use parent's starting directory. ??
  175. &si, // Pointer to STARTUPINFO structure.
  176. &pi ) // Pointer to PROCESS_INFORMATION structure.
  177. )
  178. {
  179. printf("CreateProcess failed. Error code = %x\n", GetLastError());
  180. goto exit;
  181. }
  182. // Wait until child process exits.
  183. // WaitForSingleObject( pi.hProcess, INFINITE );
  184. // Close process and thread handles.
  185. CloseHandle( pi.hProcess );
  186. CloseHandle( pi.hThread );
  187. printf("CreateProcess succeed. This process will terminate\n" );
  188. ret = 0;
  189. goto exit;
  190. }
  191. // return data...?
  192. hr = CoCreateInstance(CLSID_AsmExecute, NULL,CLSCTX_INPROC_SERVER,IID__AsmExecute,(void**)&_pAsmExecute);
  193. if (FAILED(hr))
  194. {
  195. printf("AsmExecute CoCreateInstance failed...\n");
  196. goto exit;
  197. }
  198. printf("AsmExecute CoCreateInstance succeed.\n");
  199. printf("Calling AsmExecute.Execute()...\n");
  200. try
  201. {
  202. if (argc == 3)
  203. {
  204. // use _bstr_t?
  205. hr = _pAsmExecute->Execute(argv[1], argv[2]);
  206. }
  207. else if (argc == 4)
  208. {
  209. hr = _pAsmExecute->Execute_2(argv[1], argv[2], argv[3]);
  210. }
  211. else if (argc == 6) //BUGBUG > args?
  212. {
  213. hr = _pAsmExecute->Execute_3(argv[1], argv[2], argv[3], argv[4], argv[5]);
  214. }
  215. }
  216. catch (_com_error &e)
  217. {
  218. // _com_issue_errorex throws _com_error
  219. printf("... AsmExecute Execute failed; Code = %08lx\n", e.Error());
  220. printf(" Code meaning = %s\n", (char*) e.ErrorMessage());
  221. printf(" Source = %s\n", (char*) e.Source());
  222. printf(" Description = %s\n", (char*) e.Description());
  223. goto exit;
  224. }
  225. if (FAILED(hr))
  226. {
  227. // !! should never be here !!
  228. printf("... AsmExecute Execute failed; hr = %x\n", hr);
  229. goto exit;
  230. }
  231. printf("... AsmExecute Execute succeed.\n");
  232. ret = 0;
  233. exit:
  234. if (_pAsmExecute != NULL)
  235. {
  236. _pAsmExecute->Release();
  237. _pAsmExecute = NULL;
  238. }
  239. // BUGBUG: relesase domains?
  240. final();
  241. return ret;
  242. }