#module
#uselib "kernel32.dll"
#cfunc CreateProcess "CreateProcessA" int,sptr,int,int,int,int,int,int,var,var
#cfunc WaitForSingleObject "WaitForSingleObject" int,int
#cfunc GetEnvironmentVariable "GetEnvironmentVariableA" var,var,int
#cfunc CreatePipe "CreatePipe" var,var,var,int
#cfunc CloseHandle "CloseHandle" int
#cfunc PeekNamedPipe "PeekNamedPipe" int,int,int,int,int,int
#cfunc ReadFile "ReadFile" int,var,int,var,int
#define SW_HIDE 0
#define SW_SHOW 5
#deffunc consexec var prm1,str prm2
dosexcmd = prm2 : if strlen(dosexcmd)==0 : return -1
#define MAX_PATH 260
sdim cp,MAX_PATH+2
ev="Comspec"
ret=GetEnvironmentVariable(ev,cp,MAX_PATH)
if ret==0 : return -1
filename=cp+" /c "+dosexcmd
#const MAX_BUF_SIZE 1024*1024
sdim szBuf,MAX_BUF_SIZE
sa=12,0,1 : ret=CreatePipe(hRead,hWrite,sa,MAX_BUF_SIZE)
;***** 子プロセスを作成 *****
#define STARTF_USESHOWWINDOW 1
#define STARTF_USESTDHANDLES $00000100
dim lpsiStartInfo,17 ;STARTUPINFO構造体
dim lppiProcInfo,4 ;PROCESS_INFOMATION構造体
lpsiStartInfo(0) = 68
lpsiStartInfo(11) = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW
lpsiStartInfo(12) = SW_HIDE
lpsiStartInfo(15) = hWrite
ret=CreateProcess(0,filename,0,0,1,0,0,0,lpsiStartInfo,lppiProcInfo)
if ret==0 : end
hprocess=lppiProcInfo(0)
ret=WaitForSingleObject(hprocess,$ffffffff)
ret=CloseHandle(hWrite)
rcvbuf="" ;Buffer Size HSP側で自動確保
repeat
await 10
;***** 標準出力パイプの内容を調べる *****
ret=PeekNamedPipe(hRead,0,0,0,dwStdOut,0)
ret=ReadFile(hRead,szBuf,4096,dwRead, 0)
if dwRead<4096 : memcpy szBuf, szBuf, dwRead, 0, 0 : poke szBuf, dwRead : rcvbuf+=szBuf : break
if dwRead==0 : break
rcvbuf+=szBuf
loop
prm1=rcvbuf
ret=CloseHandle(hRead)
sdim szBuf,0 : sdim cp,0 : sdim rcvbuf,0
return 0
#global
;***** Sample *****
screen 0,640,480,0,ginfo_dispx-640>>1,ginfo_dispy-480>>1
buf=""
cmd="dir"
input cmd,580,20,500
button gosub "コマンド送信",*send
stop
*send
consexec buf,cmd
mesbox buf,ginfo(10)-4,ginfo(11)-24,5
objsel 0 : hCld=objinfo(0,2)
objprm 2,buf
input cmd,580,20,500
return
参考:
http://hspnext.com/hspkura/hspkura02.htm#contents06