Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

StdoutToVar


  • Please log in to reply
121 replies to this topic
User2001
  • Guests
  • Last active:
  • Joined: --
I dont really want to use telnet, but a telnetlike application. Can you tell me, how i just get the text of a running application (CMD) into a variable using this function?
I tried the example script, but it seems like interacting with my application, because Im getting a "loopcommand error". Does this function interact in anyway with my app?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

I tried the example script, but it seems like interacting with my application, because Im getting a "loopcommand error". Does this function interact in anyway with my app?

No, it just reads the Stdout pipe. There is nothing to cause Loop error. Please be sure you're using the latest build of AHK, or post your code.

  • Guests
  • Last active:
  • Joined: --
I updated AHK to the newest version now and you script doesnt work any longer?!

I think this should work? But it says: "Error Call To Nonexistent Function" Specifically: COM_Init()

sCmd :=   "ipconfig /all"
;sDir := ""
;sInput := ""

MsgBox,   % StdCreateProcess(sCmd, sDir, sInput)   ; StdCreateProcessCOM(sCmd, sDir, sInput) 


StdCreateProcess(sCmd, sDir = "", sInput = "")
{
	If	sInput <>
	CreatePipe(hStdInRd , hStdInWr ),	SetHandleInformation(hStdInRd )
	CreatePipe(hStdOutRd, hStdOutWr),	SetHandleInformation(hStdOutWr)

	VarSetCapacity(pi, 16, 0)
	NumPut(VarSetCapacity(si, 68, 0), si)	; size of si
	NumPut(0x100	, si, 44)		; STARTF_USESTDHANDLES
	NumPut(hStdInRd	, si, 56)		; hStdInput
	NumPut(hStdOutWr, si, 60)		; hStdOutput
	NumPut(hStdOutWr, si, 64)		; hStdError
	If Not	DllCall("CreateProcess", "Uint", 0, "Uint", &sCmd, "Uint", 0, "Uint", 0, "int", True, "Uint", 0x08000000, "Uint", 0, "Uint", sDir ? &sDir : 0, "Uint", &si, "Uint", &pi)	; bInheritHandles and CREATE_NO_WINDOW
		ExitApp
	CloseHandle(NumGet(pi,0)),	CloseHandle(NumGet(pi,4))

	If	sInput <>
	StdInput( sInput , hStdInRd , hStdInWr )
	StdOutput(sOutput, hStdOutRd, hStdOutWr)
	Return	sOutput
}

StdCreateProcessCOM(sCmd, sDir = "", sInput = "")
{
	COM_Init()
	pwsh :=	COM_CreateObject("WScript.Shell")
	sDir ?	COM_Invoke(pwsh, "CurrentDirectory=", sDir) : ""
	pexec:=	COM_Invoke(pwsh, "Exec", sCmd)
	WinWaitActive, ahk_class ConsoleWindowClass,, 1
	WinHide

	If	sInput <>
	pin  :=	COM_Invoke(pexec, "StdIn"), COM_Invoke(pin, "Write", sInput), COM_Invoke(pin, "Close"), COM_Release(pin)
	pout :=	COM_Invoke(pexec, "StdOut")	; perr := COM_Invoke(pexec, "StdErr")
	Loop
		If	COM_Invoke(pout, "AtEndOfStream")=0
			sOutput	.= COM_Invoke(pout, "ReadLine") . "`r`n"
		Else	Break
	COM_Invoke(pout, "Close"), COM_Release(pout)

	Loop
		If	COM_Invoke(pexec, "Status")=0
			Sleep,	100
		Else	Break
;	COM_Invoke(pexec, "Terminate")
	COM_Release(pexec)
	COM_Release(pwsh)
	COM_Term()
	Return	sOutput
}

StdInput(ByRef sInput, hStdInRd, hStdInWr)
{
	CloseHandle(hStdInRd)
	WriteFile(hStdInWr, &sInput, StrLen(sInput))
	CloseHandle(hStdInWr)
}

StdOutput(ByRef sOutput, hStdOutRd, hStdOutWr)
{
	CloseHandle(hStdOutWr)
	VarSetCapacity(sTemp, 4095)
	Loop
		If	nSize:=ReadFile(hStdOutRd, &sTemp, 4095)
			NumPut(0, sTemp, nSize, "char"), VarSetCapacity(sTemp, -1), sOutput .= sTemp
		Else	Break
	CloseHandle(hStdOutRd)
}

CreatePipe(ByRef hRead, ByRef hWrite)
{
	Return	DllCall("CreatePipe", "UintP", hRead, "UintP", hWrite, "Uint", 0, "Uint", 0)
}

ReadFile(hFile, pBuffer, nSize)
{
	If	DllCall("ReadFile" , "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize, "Uint", 0)
	Return	nSize
}

WriteFile(hFile, pBuffer, nSize)
{
	If	DllCall("WriteFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize, "Uint", 0)
	Return	nSize
}

GetHandleInformation(Handle)
{
	If	DllCall("GetHandleInformation", "Uint", Handle, "UintP", nFlags)
	Return	nFlags
}

SetHandleInformation(Handle, nFlags = 1)	; HANDLE_FLAG_INHERIT = 1
{
	Return	DllCall("SetHandleInformation", "Uint", Handle, "Uint", nFlags, "Uint", nFlags)
}

CloseHandle(Handle)
{
	Return	DllCall("CloseHandle", "Uint", Handle)
}



Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

But it says: "Error Call To Nonexistent Function" Specifically: COM_Init()

StdCreateProcessCOM needs COM Standard Library. You may remove this function from the script if you're not going to use it. And, please remove the script StdoutToVar.ahk from your post. You only need to post your own code.

Rizzo
  • Guests
  • Last active:
  • Joined: --
What is the difference between StdCreateProcess and StdCreateProcessCOM?

I am using an older version of the script and it works well. Are there any advantages of the update other than that I need to write fewer lines of code to use it?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

What is the difference between StdCreateProcess and StdCreateProcessCOM?

I am using an older version of the script and it works well. Are there any advantages of the update other than that I need to write fewer lines of code to use it?

StadCreateProcessCOM uses COM. The update was not bug-fixing, was streamlining. So, you don't have to update it if you don't want to although it's recommended.

Rizzo
  • Guests
  • Last active:
  • Joined: --
Are there any advantages with using the COM or not using the COM?

automaticman
  • Members
  • 658 posts
  • Last active: Nov 20 2012 06:10 PM
  • Joined: 27 Oct 2006

... I'm sure you have better things to do with your time, but the topmost post could use a bit more introduction (even a single sentence), and possibly a simple usage example. When I examine a post for accessibility and presentation, I ask:

1) What is somone's first impression upon viewing this post?
2) Can someone find out the gist of the post in under 30 seconds? (This helps them decide whether to continue reading the whole thing.)
...

That's maybe also a sign for potential lack of automation/automation market hole for making things easier. :) (Like an .ahk solution which works as a posting template so anyone can fastly fill in the form or just leave them empty or very short limited info it you have less/no time.) See also my previous posting here: <!-- m -->http://www.autohotke... ... 089#179089<!-- m -->

It's not only important to have solutions, it's also important to allow/make creative-use of these solutions breaking their initial application spaces.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
Streaming ability was added to StdCreateProcess(). If specify the parameter bStream to True it'll create a console window and display line-by-line the result, in addition to returning the result as a whole.
; Streaming example
sCmd    := "ping www.autohotkey.com"
bStream := True


ahklerner
  • Members
  • 1386 posts
  • Last active: Oct 08 2014 10:29 AM
  • Joined: 26 Jun 2006
you should make this stdlib compatible.
edit:
thanks again for you contributions to the forum.
Posted Image
ʞɔпɟ əɥʇ ʇɐɥʍ

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

you should make this stdlib compatible.

OK, I modified it to be Standard Library compatible, and to use the existing one if allowed when AHK is already attached to a console.

thanks again for you contributions to the forum.

You're welcome and thanks.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
ladies and gentlemen ........(crowd gets quiet with anticipation)
Sean!!!!!!!!!! :D
hours later the cheers die down and someone points out soemthing else he did while back and it all starts again
I think the ahk community should have a Sean day
thats it July 12 is National Sean Day
Never lose.
WIN or LEARN.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Hey, I just tried the new version and the Sort example doesn't work anymore. In fact, the AutoHotkey program is stopped before the function returns any value.

Sort example from this post:
<!-- m -->http://www.autohotke... ... 878#162878<!-- m -->

Thanks for your help.



Edit: My fault, please disregard. The parameters have changed in the new version.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Edit: My fault, please disregard. The parameters have changed in the new version.

Sorry, I should've mentioned that. I updated the examples accordingly.
BTW, I usually don't change the parameters' order, however, I decided to do it in this case as I thought bStream would be used more frequently than sDir & sInput.

PS. I also updated the function StdoutToVar_CreateProcessCOM() in the script to accommodate the new bStream option.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
removed by poster due to protest from ahk communitysorry to have offended :D
Never lose.
WIN or LEARN.