000
27.10.2003, 11:42 Uhr
FloSoft
Medialer Over-Flow (Administrator)
|
Hallo, ich erstelle eine Pipe mit folgender Funktion:
C++: |
int StartPipe(CString strCommand, CString *strOutput) { CString strDir = GetWorkingDir(); // Funktion liefert Arbeitsverzeichnis der Anwendung
HANDLE readHandle = NULL; HANDLE writeHandle = NULL; HANDLE stdHandle = NULL; DWORD bytesRead = 0; SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi; STARTUPINFO si;
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES)); ZeroMemory(&pi,sizeof(PROCESS_INFORMATION)); ZeroMemory(&si,sizeof(STARTUPINFO));
sa.bInheritHandle=true; sa.lpSecurityDescriptor=NULL; sa.nLength=sizeof(SECURITY_ATTRIBUTES); si.cb=sizeof(STARTUPINFO); si.dwFlags=STARTF_USESHOWWINDOW; si.wShowWindow=SW_HIDE;
if (!CreatePipe(&readHandle,&writeHandle,&sa,NULL)) { OutputDebugString("cmd: CreatePipe failed!\n"); return 0; } stdHandle=GetStdHandle(STD_OUTPUT_HANDLE);
if (!SetStdHandle(STD_OUTPUT_HANDLE,writeHandle)) { OutputDebugString("cmd: SetStdHandle(writeHandle) failed!\n"); return 0; } char *cmd = strCommand.GetBuffer(strCommand.GetLength());
if (!CreateProcess(NULL,cmd,NULL,NULL,TRUE,DETACHED_PROCESS,NULL,strDir,&si,&pi)) { OutputDebugString("cmd: CreateProcess failed!\n"); return 0; } strCommand.ReleaseBuffer();
DWORD BRead; DWORD BTotal; DWORD BLeft;
char output[5000]; if(PeekNamedPipe(readHandle,output,sizeof(output),&BRead,&BTotal,&BLeft)) { if(BLeft > 0) { if (!ReadFile(readHandle,output,BLeft,&bytesRead,NULL)) { OutputDebugString("cmd: ReadFile failed!\n"); return 0; } } output[bytesRead]='\0'; strOutput->Format("%s", output); } if(strOutput->IsEmpty()) strOutput->Format("No Data received");
if (!SetStdHandle(STD_OUTPUT_HANDLE,stdHandle)) { OutputDebugString("cmd: SetStdHandle(stdHandle) failed!\n"); return 0; } if (!CloseHandle(readHandle)) { OutputDebugString("cmd: CloseHandle(readHandle) failed!\n"); } if (!CloseHandle(writeHandle)) { OutputDebugString("cmd: CloseHandle(writeHandle) failed!\n"); } return 1; }
|
Nur: BLeft und BTotal ist immer 0! Kann es sein das es daran liegt das das Client-Programm zu schnell sich beendet? -- class God : public ChuckNorris { }; |