JackHaxor

Console UDF

Jun 17th, 2017
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.58 KB | None | 0 0
  1. ; #INDEX# =======================================================================================================================
  2. ; Title .........: Console
  3. ; AutoIt Version : 3.3.8.1+
  4. ; Language ......: English
  5. ; Description ...: Functions that assist with native consoles.
  6. ; Author(s) .....: Janus Thorborg (Shaggi)
  7. ; ===============================================================================================================================
  8. #include-once
  9. #include <WinApiError.au3>
  10. #OnAutoItStartRegister "__Console__StartUp"
  11.  
  12. ; #CURRENT# =====================================================================================================================
  13. ;Cout
  14. ;Cin
  15. ;Cerr
  16. ;Getch
  17. ;system
  18. ;RegisterConsoleEvent
  19. ; ===============================================================================================================================
  20.  
  21. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  22. ;__Console__CreateConsole
  23. ;__Console__KillConsole
  24. ;__Console__StartUp
  25. ;__Console__ShutDown
  26. ;__Console__GetStdHandle
  27. ;__Console__HandlerRoutine
  28. ; ===============================================================================================================================
  29.  
  30. ; #VARIABLES# ===================================================================================================================
  31. ; Don't touch these.
  32. Global $__Dll_Kernel32, $__Amount__Startup_Console
  33. Global $__Console__hCtrlHandler = 0
  34. Global $_Included_Console = True
  35. ; $_bConsole__PrintToStdStreams will cause the output functions to write to autoit's own streams, too.
  36. Global $_bConsole__PrintToStdStreams = False
  37. ; ===============================================================================================================================
  38. ; #ENUMS# =======================================================================================================================
  39. Global Enum $sigCtrlC = 0, $sigCtrlBreak, $sigCtrlClose, $sigCtrlLogOff = 5, $sigCtrlShutDown = 6
  40. Global Enum $_eWrite = 0, $_eRead, $_eSetCT, $_eGetCM, $_eSetCM
  41. Global Enum $_cOut, $_cIn, $_cErr
  42. ; ===============================================================================================================================
  43. ; #TABLES# ======================================================================================================================
  44. ; These tables, on startup, get initialized to a table with function pointers and handles.
  45. Global Const $_sfTable[5] = ["WriteConsoleW","ReadConsoleW", "SetConsoleTextAttribute", "GetConsoleMode", "SetConsoleMode"]
  46. Global $_pfTable[5]
  47. Global $__Console__Handlers[2][2]
  48. Global $__CStreams[3]
  49. ; ===============================================================================================================================
  50. ; #CONSTANTS# ===================================================================================================================
  51. ; Thanks to Matt Diesel (Mat) for writing these down.
  52. ; Attributes flags (colors)
  53. ; WinCon.h (153 - 160)
  54. Global Const $FOREGROUND_BLUE = 0x0001 ; text color contains blue.
  55. Global Const $FOREGROUND_GREEN = 0x0002 ; text color contains green.
  56. Global Const $FOREGROUND_RED = 0x0004 ; text color contains red.
  57. Global Const $FOREGROUND_INTENSITY = 0x0008 ; text color is intensified.
  58. Global Const $BACKGROUND_BLUE = 0x0010 ; background color contains blue.
  59. Global Const $BACKGROUND_GREEN = 0x0020 ; background color contains green.
  60. Global Const $BACKGROUND_RED = 0x0040 ; background color contains red.
  61. Global Const $BACKGROUND_INTENSITY = 0x0080 ; background color is intensified.
  62. ; Attributes flags
  63. ; WinCon.h (161 - 169)
  64. Global Const $COMMON_LVB_LEADING_BYTE = 0x0100 ; Leading Byte of DBCS
  65. Global Const $COMMON_LVB_TRAILING_BYTE = 0x0200 ; Trailing Byte of DBCS
  66. Global Const $COMMON_LVB_GRID_HORIZONTAL = 0x0400 ; DBCS: Grid attribute: top horizontal.
  67. Global Const $COMMON_LVB_GRID_LVERTICAL = 0x0800 ; DBCS: Grid attribute: left vertical.
  68. Global Const $COMMON_LVB_GRID_RVERTICAL = 0x1000 ; DBCS: Grid attribute: right vertical.
  69. Global Const $COMMON_LVB_REVERSE_VIDEO = 0x4000 ; DBCS: Reverse fore/back ground attribute.
  70. Global Const $COMMON_LVB_UNDERSCORE = 0x8000 ; DBCS: Underscore.
  71. Global Const $COMMON_LVB_SBCSDBCS = 0x0300 ; SBCS or DBCS flag.
  72. ; ===============================================================================================================================
  73. ; #STRUCTURES# ==================================================================================================================
  74. ; $tag_CONSOLE_SCREEN_BUFFER_INFO
  75. ; $tagCHAR_INFO_W
  76. ; $tagPSMALL_RECT
  77. ; ===============================================================================================================================
  78. ; These are merely provided for convinience, they aren't used (yet)
  79. Global Const $tag_CONSOLE_SCREEN_BUFFER_INFO = "short dwSizeX; short dwSizeY; short dwCursorPositionX;short dwCursorPositionY; word wAttributes;" & _
  80. "SHORT srWindowLeft; SHORT srWindowRight; SHORT srWindowLeft; SHORT srWindowBottom;" & _
  81. "short dwMaximumWindowSizeX; short dwMaximumWindowSizeY"
  82. Global Const $tagCHAR_INFO_W = "WCHAR UnicodeChar; WORD Attributes"
  83. Global Const $tagPSMALL_RECT = "SHORT Left; SHORT Right; SHORT Left; SHORT Bottom;"
  84. ; ===============================================================================================================================
  85. ; #FUNCTION# ====================================================================================================================
  86. ; Name...........: system
  87. ; Description ...: Invokes the command processor to execute a command. Once the command execution has terminated, the processor
  88. ; gives the control back to the program, returning an int value, whose interpretation is system-dependent.
  89. ; Syntax.........: system($szCommand)
  90. ; Parameters ....: $szString - A string containing a system command to be executed.
  91. ; Return values .: Success - Depends on command given.
  92. ; Failure - Depends on command given.
  93. ; Author ........: Janus Thorborg (Shaggi)
  94. ; Modified.......: 06/08/2012
  95. ; Remarks .......: Common use is system("pause") or system("cls").
  96. ; Related .......: RunWait
  97. ; Link ..........: http://www.cplusplus.com/reference/clibrary/cstdlib/system/
  98. ; Example .......: No
  99. ; ===============================================================================================================================
  100. Func system($szCommand)
  101. If $szCommand Then
  102. If Not $__Amount__Startup_Console Then
  103. __Console__CreateConsole()
  104. $__Amount__Startup_Console += 1
  105. EndIf
  106. Return RunWait(@ComSpec & " /c " & $szCommand, @ScriptDir, Default, 0x10)
  107. EndIf
  108. Return False
  109. EndFunc ;==>system
  110. ; #FUNCTION# ====================================================================================================================
  111. ; Name...........: Cout
  112. ; Description ...: Writes a UNICODE string to the Standard Output Stream, with optional attributes. Similar to std::cout in C++ and
  113. ; ConsoleWrite().
  114. ; Syntax.........: Cout($szString [, $iAttr = -1])
  115. ; Parameters ....: $szString - A string to write to the Standard Output Stream.
  116. ; $iAttr - If supplied, the function sets the current text attributes to this before writing,
  117. ; and resets it back to normal after writing. Attributes (Thanks to Matt Diesel (Mat)):
  118. ; |FOREGROUND_BLUE - Text color contains blue.
  119. ; |FOREGROUND_GREEN - Text color contains green.
  120. ; |FOREGROUND_RED - Text color contains red.
  121. ; |FOREGROUND_INTENSITY - Text color is intensified.
  122. ; |BACKGROUND_BLUE - Background color contains blue.
  123. ; |BACKGROUND_GREEN - Background color contains green.
  124. ; |BACKGROUND_RED - Background color contains red.
  125. ; |BACKGROUND_INTENSITY - Background color is intensified.
  126. ; BitOR these together, if more than one attribute is used.
  127. ; Return values .: Success - True
  128. ; Failure - False - @error is set and DllCall() @error is kept in @extended.
  129. ; Author ........: Janus Thorborg (Shaggi)
  130. ; Modified.......: 09/07/2011
  131. ; Remarks .......:
  132. ; Related .......: Cerr
  133. ; Link ..........: http://msdn.microsoft.com/en-us/library/ms687401(VS.85).aspx
  134. ; Example .......: No
  135. ; ===============================================================================================================================
  136. Func Cout($szString, $iAttr = -1)
  137. If Not $__Amount__Startup_Console Then
  138. __Console__CreateConsole()
  139. $__Amount__Startup_Console += 1
  140. EndIf
  141. Local $lpBuffer = DllStructCreate("wchar[" & StringLen($szString) + 1 & "]")
  142. DllStructSetData($lpBuffer, 1, $szString)
  143. Local $lpNumberOfCharsWritten = 0
  144. If $_bConsole__PrintToStdStreams Then ConsoleWrite($szString)
  145. Switch $iAttr
  146. Case -1
  147. Local $aResult = DllCallAddress("BOOL", $_pfTable[$_eWrite], _
  148. "handle", $__CStreams[$_cOut], _
  149. "ptr", DllStructGetPtr($lpBuffer), _
  150. "dword", StringLen($szString), _
  151. "dword*", $lpNumberOfCharsWritten, _
  152. "ptr", 0)
  153. Return $aResult[0]
  154. Case Else
  155. Local $aResult1 = DllCallAddress("BOOL", $_pfTable[$_eSetCT], _
  156. "handle", $__CStreams[$_cOut], "word", $iAttr)
  157.  
  158. Local $aResult2 = DllCallAddress("BOOL", $_pfTable[$_eWrite], _
  159. "handle", $__CStreams[$_cOut], _
  160. "ptr", DllStructGetPtr($lpBuffer), _
  161. "dword", StringLen($szString), _
  162. "dword*", $lpNumberOfCharsWritten, _
  163. "ptr", 0)
  164.  
  165. Local $aResult3 = DllCallAddress("BOOL", $_pfTable[$_eSetCT], _
  166. "handle", $__CStreams[$_cOut], "word", 0x7)
  167. Switch $aResult2[0]
  168. Case 0
  169. Return SetError(1,@error,False)
  170. Case Else
  171. Return (($aResult1[0] <> 0) AND ($aResult3[0] <> 0))
  172. EndSwitch
  173. EndSwitch
  174. Return False
  175. EndFunc ;==>Cout
  176. ; #FUNCTION# ====================================================================================================================
  177. ; Name...........: Cerr
  178. ; Description ...: Writes a UNICODE string to the Standard Error Stream, with optional attributes. Similar to std::cerr in C++ and
  179. ; ConsoleWriteError().
  180. ; Syntax.........: Cerr($szString [, $iAttr = -1])
  181. ; Parameters ....: $szString - A string to write to the Standard Error Stream.
  182. ; $iAttr - If supplied, the function sets the current text attributes to this before writing,
  183. ; and resets it back to normal after writing. Attributes (Thanks to Matt Diesel (Mat)):
  184. ; |FOREGROUND_BLUE - Text color contains blue.
  185. ; |FOREGROUND_GREEN - Text color contains green.
  186. ; |FOREGROUND_RED - Text color contains red.
  187. ; |FOREGROUND_INTENSITY - Text color is intensified.
  188. ; |BACKGROUND_BLUE - Background color contains blue.
  189. ; |BACKGROUND_GREEN - Background color contains green.
  190. ; |BACKGROUND_RED - Background color contains red.
  191. ; |BACKGROUND_INTENSITY - Background color is intensified.
  192. ; BitOR these together, if more than one attribute is used.
  193. ; Return values .: Success - True
  194. ; Failure - False - @error is set - see @extended for DllCall() @error.
  195. ; Author ........: Janus Thorborg (Shaggi)
  196. ; Modified.......: 09/07/2011
  197. ; Remarks .......:
  198. ; Related .......: Cout
  199. ; Link ..........: http://msdn.microsoft.com/en-us/library/ms687401(VS.85).aspx
  200. ; Example .......: No
  201. ; ===============================================================================================================================
  202. Func Cerr($szString, $iAttr = -1)
  203. If Not $__Amount__Startup_Console Then
  204. __Console__CreateConsole()
  205. $__Amount__Startup_Console += 1
  206. EndIf
  207. Local $lpBuffer = DllStructCreate("wchar[" & StringLen($szString) + 1 & "]")
  208. DllStructSetData($lpBuffer, 1, $szString)
  209. Local $lpNumberOfCharsWritten = 0
  210. If $_bConsole__PrintToStdStreams Then ConsoleWrite($szString)
  211. Switch $iAttr
  212. Case -1
  213. Local $aResult = DllCallAddress("BOOL", $_pfTable[$_eWrite], _
  214. "handle", $__CStreams[$_cErr], _
  215. "ptr", DllStructGetPtr($lpBuffer), _
  216. "dword", StringLen($szString), _
  217. "dword*", $lpNumberOfCharsWritten, _
  218. "ptr", 0)
  219. Return $aResult[0]
  220. Case Else
  221. Local $aResult1 = DllCallAddress("BOOL", $_pfTable[$_eSetCT], _
  222. "handle", $__CStreams[$_cErr], "word", $iAttr)
  223.  
  224. Local $aResult2 = DllCallAddress("BOOL", $_pfTable[$_eWrite], _
  225. "handle", $__CStreams[$_cErr], _
  226. "ptr", DllStructGetPtr($lpBuffer), _
  227. "dword", StringLen($szString), _
  228. "dword*", $lpNumberOfCharsWritten, _
  229. "ptr", 0)
  230.  
  231. Local $aResult3 = DllCallAddress("BOOL", $_pfTable[$_eSetCT], _
  232. "handle", $__CStreams[$_cErr], "word", 0x7)
  233. Switch $aResult2[0]
  234. Case 0
  235. Return SetError(1,@error,False)
  236. Case Else
  237. Return (($aResult1[0] <> 0) AND ($aResult3[0] <> 0))
  238. EndSwitch
  239. EndSwitch
  240. Return False
  241. EndFunc ;==>Cerr
  242. ; #FUNCTION# ====================================================================================================================
  243. ; Name...........: Cin
  244. ; Description ...: Retrieves a UNICODE string from the Standard Input Stream, with optional size. Similar to std::cin in C++.
  245. ; Syntax.........: Cin(ByRef $szString [, $iSize = 128])
  246. ; Parameters ....: $szString - A string the content is copied to.
  247. ; $iSize - If supplied, the function sets the maximal size of the characters read to this.
  248. ; Return values .: Success - Actual amount of characters read.
  249. ; Failure - False - @error is set and @extended holds DllCall() @error
  250. ; Author ........: Janus Thorborg (Shaggi)
  251. ; Modified.......: 09/07/2011
  252. ; Remarks .......: Returns once something has been typed into console AND enter is pressed.
  253. ; Related .......: Getch
  254. ; Link ..........: http://msdn.microsoft.com/en-us/library/ms684958(VS.85).aspx
  255. ; Example .......: No
  256. ; ===============================================================================================================================
  257. Func Cin(ByRef $szString, $iSize = 128)
  258. If Not $__Amount__Startup_Console Then
  259. __Console__CreateConsole()
  260. $__Amount__Startup_Console += 1
  261. EndIf
  262. Local $lpBuffer = DllStructCreate("wchar[" & $iSize + 3 & "]")
  263. Local $lpNumberOfCharsRead = 0
  264. Local $aResult = DllCallAddress("BOOL", $_pfTable[$_eRead], _
  265. "handle", $__CStreams[$_cIn], _
  266. "ptr", DllStructGetPtr($lpBuffer), _
  267. "dword", DllStructGetSize($lpBuffer), _
  268. "dword*", $lpNumberOfCharsRead, _
  269. "ptr", 0)
  270. Select
  271. Case Not $aResult[0]
  272. Return SetError(1,@error,False)
  273. Case Else
  274. $szString = StringTrimRight(DllStructGetData($lpBuffer, 1),2)
  275. Return $aResult[4]
  276. EndSelect
  277. EndFunc ;==>Cin
  278. ; #FUNCTION# ====================================================================================================================
  279. ; Name...........: Getch
  280. ; Description ...: Retrieves 1 unicode character from the input buffer. Blocks.
  281. ; Syntax.........: Getch()
  282. ; Parameters ....:
  283. ; Return values .: Success - A single wide character.
  284. ; Failure - False and @error is set - see @extended for DllCall() @error.
  285. ; Author ........: Janus Thorborg (Shaggi)
  286. ; Modified.......: 09/07/2011
  287. ; Remarks .......: Returns once something has been typed into console. Doesn't work with Esc, arrows or F1-12. Don't use it in
  288. ; callback events, it will halt the console!
  289. ; Related .......: Cin
  290. ; Link ..........:
  291. ; Example .......: No
  292. ; ===============================================================================================================================
  293. Func Getch()
  294. If Not $__Amount__Startup_Console Then
  295. __Console__CreateConsole()
  296. $__Amount__Startup_Console += 1
  297. EndIf
  298. Local $mode, $Char, $Count, $lpNumberOfCharsRead
  299. Local $Ret = DllCallAddress("BOOL", $_pfTable[$_eGetCM], _
  300. "handle",$__CStreams[$_cIn],"dword*",$mode)
  301. If @Error OR NOT $Ret[0] Then Return SetError(1,@error,False)
  302. $Mode = $Ret[2]
  303. $Ret = DllCallAddress("BOOL", $_pfTable[$_eSetCM], _
  304. "handle",$__CStreams[$_cIn],"dword",0)
  305. If @Error OR NOT $Ret[0] Then Return SetError(2,@error,False)
  306. Local $aResult = DllCallAddress("BOOL", $_pfTable[$_eRead], _
  307. "handle", $__CStreams[$_cIn], _
  308. "int*", $Char, _
  309. "dword", 2, _
  310. "int*", $lpNumberOfCharsRead, _
  311. "ptr", 0)
  312. If @Error OR NOT $aResult[0] Then Return SetError(3,@error,False)
  313. Local $Return = ChrW($aResult[2])
  314. $Ret = DllCallAddress("BOOL", $_pfTable[$_eSetCM], _
  315. "handle",$__CStreams[$_cIn],"dword",$Mode)
  316. If @Error OR NOT $Ret[0] Then return SetError(4,@error,False)
  317. Return $Return
  318. EndFunc ;==>Getch
  319. ; #FUNCTION# ====================================================================================================================
  320. ; Name...........: RegisterConsoleEvent
  321. ; Description ...: Registers a function to be called when a specified signal is emitted from the system.
  322. ; Syntax.........: RegisterConsoleEvent($fFunc [, $dwSig = $sigCtrlClose [, $bRegisterExit = True]])
  323. ; Parameters ....: $fFunc - Either a string with the function name, or a function (only applies to beta).
  324. ; $dwSig - The signal the function is associated with. Can be one of the following values:
  325. ; |$sigCtrlC - A CTRL+C signal was received.
  326. ; |$sigCtrlBreak - A CTRL+BREAK signal was received.
  327. ; |$sigCtrlClose - A signal that the system sends to all processes attached to a console
  328. ; when the user closes the console (either by clicking Close on the console
  329. ; window's window menu, or by clicking the End Task button command
  330. ; from Task Manager).
  331. ; |$sigCtrlLogOff - A signal that the system sends to all console processes when a user
  332. ; is logging off. This signal does not indicate which user is logging off,
  333. ; so no assumptions can be made.
  334. ; Note that this signal is received only by services. Interactive
  335. ; applications are terminated at logoff, so they are not present
  336. ; when the system sends this signal.
  337. ; |$sigCtrlShutDown - A signal that the system sends when the system is shutting down.
  338. ; Interactive applications are not present by the time the system sends
  339. ; this signal, therefore it can be received only be services in this
  340. ; situation. Services also have their own notification mechanism
  341. ; for shutdown events.
  342. ; $bRegisterExit - If true, registers the function to be called OnAutoItExit also.
  343. ; Return values .: Success - True
  344. ; Failure - False and @error is set - see @extended for DllCall() @error.
  345. ; Author ........: Janus Thorborg (Shaggi)
  346. ; Modified.......: 18/06/2012
  347. ; Remarks .......: Note that if only a function is passed, it is considered to be called on the close event, and the same function
  348. ; is also registrered for normal AutoIt exit, so it gets called no matter what. Currently, there is no way to
  349. ; terminate AutoIt normally (ie. call normal OnExit handlers) on close event, so this must be used in case of
  350. ; something vital that has to be cleaned up on exit.
  351. ; Related .......: OnAutoItExitRegister
  352. ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683242(v=vs.85).aspx
  353. ; Example .......: No
  354. ; ===============================================================================================================================
  355. Func RegisterConsoleEvent($fFunc, $dwSig = $sigCtrlClose, $bRegisterExit = True)
  356. If Not $__Amount__Startup_Console Then
  357. __Console__CreateConsole()
  358. $__Amount__Startup_Console += 1
  359. EndIf
  360. #cs
  361. Check bounds in the function table, resize if needed
  362. #ce
  363. $nCap = UBound($__Console__Handlers) -1
  364. $nAmountNeeded = $__Console__Handlers[0][0] + 1
  365. If $nAmountNeeded > $nCap Then
  366. ReDim $__Console__Handlers[$nCap + 2][2]
  367. EndIf
  368. $__Console__Handlers[0][0] += 1
  369.  
  370. #cs
  371. Has the handler been registrered yet? If not, do it. Else pass.
  372. #ce
  373. If NOT $__Console__hCtrlHandler Then
  374. $__Console__hCtrlHandler = DllCallBackRegister("__Console__HandlerRoutine","bool","dword")
  375. Local $pCtrlHandler = DllCallBackGetPtr($__Console__hCtrlHandler)
  376. $aRet = DllCall($__Dll_Kernel32,"bool","SetConsoleCtrlHandler","ptr",$pCtrlHandler,"bool",1)
  377. If @Error OR NOT $aRet[0] OR _WinApi_GetLastError() Then Return SetError(_WinApi_GetLastError(), @extended, False)
  378. EndIf
  379.  
  380. #cs
  381. Register the event and the function
  382. #ce
  383. $__Console__Handlers[ $__Console__Handlers[0][0] ][0] = $dwSig
  384. $__Console__Handlers[ $__Console__Handlers[0][0] ][1] = $fFunc
  385.  
  386. If $bRegisterExit AND $dwSig = $sigCtrlClose Then OnAutoItExitRegister($fFunc)
  387. Return True
  388. EndFunc
  389. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  390. ; Name...........: __Console__HandlerRoutine
  391. ; Description ...: A callback called on system-generated signals. Calls any event handlers registrered using RegisterConsoleEvent.
  392. ; Syntax.........: __Console__HandlerRoutine()
  393. ; Parameters ....: $dwSig - the generated signal.
  394. ; Return values .: None
  395. ; Author ........: Janus Thorborg (Shaggi)
  396. ; Modified.......: 18/06/2012
  397. ; ===============================================================================================================================
  398. Func __Console__HandlerRoutine($dwSig)
  399. Local $fFunc
  400. For $i = 1 to UBound($__Console__Handlers) - 1
  401. If $dwSig = $__Console__Handlers[$i][0] AND $__Console__Handlers[$i][1] <> "" Then
  402. If VarGetType($__Console__Handlers[$i][1]) = "string" Then ; string name passed
  403. Call($__Console__Handlers[$i][1])
  404. ElseIf VarGetType($__Console__Handlers[$i][1]) = "userfunction" Then ; function passed, applies to beta.
  405. $fFunc = $__Console__Handlers[$i][1]
  406. $fFunc()
  407. EndIf
  408. EndIf
  409. Next
  410. __Console__ShutDown()
  411. Exit
  412. Return False
  413. EndFunc
  414. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  415. ; Name...........: __Console_StartUp()
  416. ; Description ...: Checks if running under SciTE, if, then executes the script via ShellExecute so own console can be opened.
  417. ; Exits with the errorcode the executed script did.
  418. ; Syntax.........: __Console_StartUp()
  419. ; Parameters ....: None
  420. ; Return values .: None
  421. ; Author ........: Janus Thorborg (Shaggi)
  422. ; Modified.......: 16/03/2011
  423. ; Remarks .......: This function is used internally. Called automatically on AutoIt startup.
  424. ; Related .......:
  425. ; Link ..........:
  426. ; Example .......:
  427. ; ===============================================================================================================================
  428. Func __Console__StartUp()
  429. Local $bIsRunningFromScite = StringInStr($CmdLineRaw, "/ErrorStdOut")
  430. Local $bIsRecursed = Execute(StringLeft($Cmdline[$Cmdline[0]],StringLen("/Console=")))
  431. If ($bIsRunningFromScite > 0) AND NOT $bIsRecursed Then
  432. Local $szCommandLine = '"' & @AutoItExe & '" "' & @ScriptFullPath & '" /Console=True'
  433. ConsoleWrite(@CRLF & "!<Console.au3>:" & @CRLF & @TAB & "Launching process on own..." & @CRLF & "+" & @TAB & "CmdLine:" & $szCommandLine & @CRLF)
  434. Local $iReturnCode = RunWait($szCommandline)
  435. ConsoleWrite(@CRLF & ">" & @TAB & @ScriptName & " returned " & $iReturnCode & " (0x" & Hex($iReturnCode, 8) & ")" & @CRLF)
  436. Exit $iReturnCode
  437. EndIf
  438. Global $__Dll_Kernel32 = DllOpen("kernel32.dll")
  439. OnAutoItExitRegister("__Console__ShutDown")
  440. EndFunc ;==>__Console_StartUp
  441. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  442. ; Name...........: __Console_ShutDown()
  443. ; Description ...: If a console is present, it detaches and closes any handles opened.
  444. ; Syntax.........: __Console_ShutDown()
  445. ; Parameters ....: None
  446. ; Return values .: None
  447. ; Author ........: Janus Thorborg (Shaggi)
  448. ; Modified.......: 15/03/2011
  449. ; Remarks .......: This function is used internally. Called automatically on AutoIt shutdown.
  450. ; Related .......:
  451. ; Link ..........:
  452. ; Example .......:
  453. ; ===============================================================================================================================
  454. Func __Console__ShutDown()
  455. If $__Amount__Startup_Console Then
  456. For $cStream in $__CStreams
  457. DllCall($__Dll_Kernel32,"BOOL","CloseHandle","handle",$CStream)
  458. Next
  459. __Console__KillConsole()
  460. EndIf
  461. DllClose($__Dll_Kernel32)
  462. EndFunc ;==>__Console_ShutDown
  463. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  464. ; Name...........: __Console_CreateConsole()
  465. ; Description ...: Allocates an console, and opens up handles for the three standard streams: Input, Output and Error.
  466. ; Syntax.........: __Console_CreateConsole()
  467. ; Parameters ....: None
  468. ; Return values .: Success - True
  469. ; Failure - False
  470. ; Author ........: Janus Thorborg (Shaggi)
  471. ; Modified.......: 18/06/2012
  472. ; Remarks .......: This function is used internally. Called automatically the first time any of the Cin, Cerr or Cout funcs is used.
  473. ; Related .......:
  474. ; Link ..........:
  475. ; Example .......:
  476. ; ===============================================================================================================================
  477. Func __Console__CreateConsole()
  478. If Not $__Amount__Startup_Console Then
  479. $__Amount__Startup_Console += 1
  480. Local $aResult = DllCall($__Dll_Kernel32, "BOOL", "AllocConsole")
  481. Local $fpTemp, $mKernelHandle = DllCall($__Dll_Kernel32, _
  482. "HANDLE","GetModuleHandleW", _
  483. "wstr", "Kernel32.dll")
  484. If @Error Or NOT $mKernelHandle[0] Then
  485. Exit(0xF)
  486. EndIf
  487. For $i = 0 To 4
  488. $fpTemp = DllCall($__Dll_Kernel32, _
  489. "ptr", "GetProcAddress", _
  490. "HANDLE", $mKernelHandle[0], _
  491. "str", $_sfTable[$i])
  492. If @Error Or NOT $fpTemp[0] Then
  493. Exit(0xF + $i)
  494. EndIf
  495. $_pfTable[$i] = $fpTemp[0]
  496. Next
  497. $__CStreams[$_cOut] = __Console__GetStdHandle()
  498. $__CStreams[$_cIn] = __Console__GetStdHandle(-10)
  499. $__CStreams[$_cErr] = __Console__GetStdHandle(-12)
  500. Return $aResult[0]
  501. EndIf
  502. EndFunc ;==>__Console__CreateConsole
  503. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  504. ; Name...........: __Console_ShutDown()
  505. ; Description ...: Frees the console from the process.
  506. ; Syntax.........: __Console_ShutDown()
  507. ; Parameters ....: None
  508. ; Return values .: None
  509. ; Author ........: Janus Thorborg (Shaggi)
  510. ; Modified.......: 15/03/2011
  511. ; Remarks .......: This function is used internally. Called automatically on AutoIt shutdown.
  512. ; Related .......:
  513. ; Link ..........:
  514. ; Example .......:
  515. ; ===============================================================================================================================
  516. Func __Console__KillConsole()
  517. Local $aResult = DllCall($__Dll_Kernel32, "BOOL", "FreeConsole")
  518. Return $aResult[0]
  519. EndFunc ;==>__Console__KillConsole
  520. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  521. ; Name...........: __Console_GetStdHandle()
  522. ; Description ...: Returns an handle to the desired standard stream.
  523. ; Syntax.........: __Console_GetStdHandle()
  524. ; Parameters ....: None
  525. ; Return values .: Success - A handle to the stream.
  526. ; Failure - 0
  527. ; Author ........: Janus Thorborg (Shaggi)
  528. ; Modified.......: 15/03/2011
  529. ; Remarks .......: This function is used internally. Called automatically the first time any of the Cin, Cerr or Cout funcs is used.
  530. ; Related .......:
  531. ; Link ..........:
  532. ; Example .......:
  533. ; ===============================================================================================================================
  534. Func __Console__GetStdHandle($nStdHandle = -11)
  535. Local $aResult = DllCall($__Dll_Kernel32, "handle", "GetStdHandle", _
  536. "dword", $nStdHandle)
  537. Return $aResult[0]
  538. EndFunc ;==>__Console__GetStdHandle
Advertisement
Add Comment
Please, Sign In to add comment