<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK v2: Принять бинарный ввод через пайплайн "|"]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=18106&amp;type=atom" />
	<updated>2024-02-20T20:30:37Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=18106</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK v2: Принять бинарный ввод через пайплайн "|"]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=160372#p160372" />
			<content type="html"><![CDATA[<p>Честно говоря, никогда не пользовался FileOpen(&quot;*&quot;, &quot;r&quot;) для чтения stdin, и не знаю, как это работает внутри. Но есть такой код для чтения данных, передаваемых процессом, которой создаётся из командной строки:<br /></p><div class="codebox"><pre><code>#Requires AutoHotkey v2

MsgBox CmdRet(&#039;ping -n 2 google.com&#039;)

CmdRet(sCmd, callBackFunc := &#039;&#039;, encoding := &#039;&#039;) {
    static flags := [HANDLE_FLAG_INHERIT := 0x1, CREATE_NO_WINDOW := 0x8000000], STARTF_USESTDHANDLES := 0x100

    (encoding = &#039;&#039; &amp;&amp; encoding := &#039;cp&#039; . DllCall(&#039;GetOEMCP&#039;, &#039;UInt&#039;))
    DllCall(&#039;CreatePipe&#039;, &#039;PtrP&#039;, &amp;hPipeRead := 0, &#039;PtrP&#039;, &amp;hPipeWrite := 0, &#039;Ptr&#039;, 0, &#039;UInt&#039;, 0)
    DllCall(&#039;SetHandleInformation&#039;, &#039;Ptr&#039;, hPipeWrite, &#039;UInt&#039;, flags[1], &#039;UInt&#039;, flags[1])

    STARTUPINFO := Buffer(size := A_PtrSize*9 + 4*8, 0)
    NumPut(&#039;UInt&#039;, size, STARTUPINFO)
    NumPut(&#039;UInt&#039;, STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
    NumPut(&#039;Ptr&#039;, hPipeWrite, &#039;Ptr&#039;, hPipeWrite, STARTUPINFO, size - A_PtrSize*2)

    PROCESS_INFORMATION := Buffer(A_PtrSize*2 + 4*2, 0)
    if !DllCall(&#039;CreateProcess&#039;, &#039;Ptr&#039;, 0, &#039;Str&#039;, sCmd, &#039;Ptr&#039;, 0, &#039;Ptr&#039;, 0, &#039;UInt&#039;, true, &#039;UInt&#039;, flags[2]
                               , &#039;Ptr&#039;, 0, &#039;Ptr&#039;, 0, &#039;Ptr&#039;, STARTUPINFO, &#039;Ptr&#039;, PROCESS_INFORMATION)
    {
        DllCall(&#039;CloseHandle&#039;, &#039;Ptr&#039;, hPipeRead)
        DllCall(&#039;CloseHandle&#039;, &#039;Ptr&#039;, hPipeWrite)
        throw OSError(&#039;CreateProcess is failed&#039;)
    }
    DllCall(&#039;CloseHandle&#039;, &#039;Ptr&#039;, hPipeWrite)
    temp := Buffer(4096, 0), output := &#039;&#039;
    while DllCall(&#039;ReadFile&#039;, &#039;Ptr&#039;, hPipeRead, &#039;Ptr&#039;, temp, &#039;UInt&#039;, 4096, &#039;UIntP&#039;, &amp;size := 0, &#039;UInt&#039;, 0) {
        output .= stdOut := StrGet(temp, size, encoding)
        ( callBackFunc &amp;&amp; callBackFunc(stdOut) )
    }
    DllCall(&#039;CloseHandle&#039;, &#039;Ptr&#039;, NumGet(PROCESS_INFORMATION, &#039;Ptr&#039;))
    DllCall(&#039;CloseHandle&#039;, &#039;Ptr&#039;, NumGet(PROCESS_INFORMATION, A_PtrSize, &#039;Ptr&#039;))
    DllCall(&#039;CloseHandle&#039;, &#039;Ptr&#039;, hPipeRead)
    return output
}</code></pre></div><p>Приведённый выше вариант для текстовых данных, но вы можете поменять эту часть<br /></p><div class="codebox"><pre><code>    while DllCall(&#039;ReadFile&#039;, &#039;Ptr&#039;, hPipeRead, &#039;Ptr&#039;, temp, &#039;UInt&#039;, 4096, &#039;UIntP&#039;, &amp;size := 0, &#039;UInt&#039;, 0) {
        output .= stdOut := StrGet(temp, size, encoding)
        ( callBackFunc &amp;&amp; callBackFunc(stdOut) )
    }</code></pre></div><p>под свои нужды.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2024-02-20T20:30:37Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=160372#p160372</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK v2: Принять бинарный ввод через пайплайн "|"]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=160367#p160367" />
			<content type="html"><![CDATA[<p>Здравствуйте. Нужно принять из сторонней программы бинарные данные (файл) через пайплайн.<br />Я попробовал для теста:<br /></p><div class="codebox"><pre><code>stdin  := FileOpen(&quot;*&quot;, &quot;r&quot;)
outputdebug stdin.length
</code></pre></div><p>и запустил через консоль: program.exe | myscript.ahk<br />Получил ошибку AHK &quot;Error: (6) Неверный дескриптор.&quot; на первой строчке.</p><p>Как правильно это можно сделать?</p>]]></content>
			<author>
				<name><![CDATA[Serj-Lewa]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=43579</uri>
			</author>
			<updated>2024-02-20T10:11:19Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=160367#p160367</id>
		</entry>
</feed>
