What i'm trying to do is write text to gzip.exe StdIn and redirect its StdOut to a file, all this using WINAPI functions and visual Foxpro 9.
The problem is that I always get "Stdin: Bad file descriptor" error and no output. What I expect to have is the file associated with StdOut filled with data and the file associated with StdErr empty. Instead I always endup with the file associated with StdOut empty and the file associated with StdErr filled with:
standard input: Bad file descriptor gzip:"
Here is my code:
*----------------------------------------------------------------
LOCAL cBuffer, nBytesRead, newBufSize, nBytesWritten
LOCAL hReadPipe, hWritePipe, res, cSecAttr, cFile, hFile, process_info, start_info, hProcess, hThread
*---------------------------------------------------
#DEFINE BUFF_SIZE 1024
#DEFINE WAIT_TIMEOUT 0x00000102
#DEFINE WAIT_INTERVAL 5000
#DEFINE INFINITE 0xFFFFFFFF
*---------------------------------------------------
#DEFINE HANDLE_FLAG_INHERIT 0x00000001
*---------------------------------------------------
#DEFINE FILE_APPEND_DATA 4
#DEFINE FILE_SHARE_READ 0x00000001
#DEFINE FILE_SHARE_WRITE 0x00000002
#DEFINE CREATE_ALWAYS 2
#DEFINE FILE_ATTRIBUTE_NORMAL 0x80
#DEFINE INVALID_HANDLE_VALUE -1
#DEFINE GENERIC_READ 0x80000000
#DEFINE GENERIC_WRITE 0x40000000
*---------------------------------------------------
#DEFINE STARTF_USESTDHANDLES 0x00000100
#DEFINE STARTF_USESHOWWINDOW 0x00000001
#DEFINE CREATE_NO_WINDOW 0x08000000
*---------------------------------------------------
DECLARE INTEGER WaitForSingleObject IN kernel32.DLL ;
INTEGER hHandle, ;
INTEGER dwMilliseconds
DECLARE INTEGER GetLastError IN kernel32.DLL
DECLARE INTEGER CloseHandle IN kernel32.DLL INTEGER hObject
DECLARE INTEGER CreatePipe IN kernel32;
INTEGER @ hReadPipe, ;
INTEGER @ hWritePipe, ;
STRING @ lpPipeAttributes, ;
LONG nSize
DECLARE INTEGER SetHandleInformation IN Kernel32 ;
INTEGER hObject, ;
LONG dwMask, ;
LONG dwFlags
DECLARE INTEGER ReadFile IN kernel32 ;
INTEGER hFile, ;
STRING @ lpBuffer, ;
INTEGER nNumberOfBytesToRead, ;
INTEGER @ lpNumberOfBytesRead, ;
INTEGER lpOverlapped
DECLARE INTEGER WriteFile IN Kernel32 ;
INTEGER hFile, ;
STRING lpBuffer, ;
INTEGER nBt2Write, ;
INTEGER @ lpBtWritten, ;
INTEGER lpOverlapped
DECLARE INTEGER CreateFile IN Kernel32.DLL ;
STRING lpFileName, ;
INTEGER dwDesiredAccess, ;
INTEGER dwShareMode, ;
STRING lpSecurityAttributes, ;
INTEGER dwCreationDisposition, ;
INTEGER dwFlagsAndAttributes, ;
INTEGER hTemplateFile
DECLARE INTEGER CreateProcess IN kernel32.DLL ;
INTEGER lpApplicationName, ;
STRING lpCommandLine, ;
INTEGER lpProcessAttributes, ;
INTEGER lpThreadAttributes, ;
INTEGER bInheritHandles, ;
INTEGER dwCreationFlags, ;
INTEGER lpEnvironment, ;
INTEGER lpCurrentDirectory, ;
STRING @lpStartupInfo, ;
STRING @lpProcessInformation
*-------------------------------------------------------
hReadPipe=0
hWritePipe=0
cSecAttr=BINTOC(12,'4RS')+BINTOC(0,'4RS')+BINTOC(1,'4RS')
res=CreatePipe(@hReadPipe,@hWritePipe,cSecAttr,BUFF_SIZE)
res=SetHandleInformation(hReadPipe, HANDLE_FLAG_INHERIT,0)
res=SetHandleInformation(hWritePipe, HANDLE_FLAG_INHERIT,0)
*-------------------------------------------------------
cSecAttr=BINTOC(12,'4RS')+BINTOC(0,'4RS')+BINTOC(1,'4RS')
cFile="d:\ggzz.sql.gz"
hFile=0
hFile=CreateFile(cFile,BITOR(GENERIC_READ,GENERIC_WRITE),BITOR(FILE_SHARE_READ,FILE_SHARE_WRITE),cSecAttr,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0)
*-------------------------------------------------------
cSecAttr=BINTOC(12,'4RS')+BINTOC(0,'4RS')+BINTOC(1,'4RS')
cFile="d:\gzip.err"
hGzipErr=0
hGzipErr=CreateFile(cFile,BITOR(GENERIC_READ,GENERIC_WRITE),BITOR(FILE_SHARE_READ,FILE_SHARE_WRITE),cSecAttr,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0)
*-------------------------------------------------------
cKmd="d:\gzip\bin\gzip.exe"
process_info = REPLICATE(CHR(0), 16)
start_info = BINTOC(68,'4RS') + REPLICATE(CHR(0), 64) start_info=LEFT(start_info,44)+BINTOC(BITOR(STARTF_USESTDHANDLES,STARTF_USESHOWWINDOW),'4RS')+REPLICATE(CHR(0), 20)
start_info=LEFT(start_info,56)+BINTOC(hReadPipe,'4RS')+BINTOC(hFile,'4RS')+BINTOC(hGzipErr,'4RS')
res = CreateProcess(0,cKmd, 0, 0, 1, 0, 0, 0, @start_info, @process_info)
hProcess=CTOBIN(SUBSTR(process_info, 1, 4),'4RS')
hThread=CTOBIN(SUBSTR(process_info, 5, 4),'4RS')
DIMENSION aaa(3)
aaa(1)="11111111111111111111111"
aaa(2)="222222222222222222222222"
aaa(3)="3333333333333333333333"
FOR i=1 TO ALEN(aaa)
cBuffer=aaa(i)
newBufsize=LEN(cBuffer)
nBytesWritten=0
res=WriteFile(hWritePipe,cBuffer,newBufsize,@nBytesWritten,0)
IF nBytesWritten=0
EXIT
ENDIF
ENDFOR
=WaitForSingleObject(hProcess,INFINITE)
res=CloseHandle(hFile)
res=CloseHandle(hThread)
res=CloseHandle(hProcess)
res=CloseHandle(hReadPipe)
res=CloseHandle(hWritePipe)
CLEAR DLLS "CreatePipe","GetLastError", "CreateFile", "CreateProcess", "CloseHandle","SetHandleInformation", ; "ReadFile", "WriteFile"
User contributions licensed under CC BY-SA 3.0