您当前的位置: 首页 > 

蓝不蓝编程

暂无认证

  • 5浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

获取控制台程序输入信息

蓝不蓝编程 发布时间:2011-02-01 20:35:00 ,浏览量:5

文件ProgramExec.h

//---------------------------------------------------------------------------

#ifndef ProgramExecH#define ProgramExecH

#include "MyUnicodeString.h"//---------------------------------------------------------------------------DWORD WINAPI ReadFromPipe(LPVOID args);

/* globals */

#define CHUNK 25#define STATICBUFFERSIZE    1000typedef struct { HANDLE pipe; char buffer[STATICBUFFERSIZE];} pipeinfo;

pipeinfo Out = { INVALID_HANDLE_VALUE, '/0' };pipeinfo Err = { INVALID_HANDLE_VALUE, '/0' };MyUnicodeString* myUnicodeString;

void execProgram(UnicodeString& cmd,MyUnicodeString& _myUnicodeString,int& returnCode){ STARTUPINFOW si; PROCESS_INFORMATION pi; SECURITY_ATTRIBUTES sa; DWORD threadID; char msg[300]; BOOL ok; HANDLE hProcess, h, pipeThreads[2]; char cmdline[100]; myUnicodeString = &_myUnicodeString;

 hProcess = GetCurrentProcess();

 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); ZeroMemory(&si, sizeof(STARTUPINFOW)); si.cb = sizeof(STARTUPINFOW); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = INVALID_HANDLE_VALUE;

 ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE;

 /*  * Create a non-inheritible pipe.  */ CreatePipe(&Out.pipe, &h, &sa, 0);

 /*  * Dupe the write side, make it inheritible, and close the original.  */ DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE,   DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);

 /*  * Same as above, but for the error side.  */ CreatePipe(&Err.pipe, &h, &sa, 0); DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE,   DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);

 ok = CreateProcessW(NULL, /* Module name. */ cmd.w_str(), /* Command line. */ NULL, /* Process handle not inheritable. */ NULL, /* Thread handle not inher 调用一个控制台程序并取得其输出(PIPE)  www.firnow.com    时间 : 2007-10-19  作者:佚名   编辑:本站 点击:   [ 评论 ]  itable. */ TRUE, /* yes, inherit handles. */ DETACHED_PROCESS, /* No console for you. */ NULL, /* Use parent's environment block. */ NULL, /* Use parent's starting directory. */ &si, /* Pointer to STARTUPINFOW structure. */ &pi); /* Pointer to PROCESS_INFORMATION structure. */

 if (!ok) {  DWORD err = GetLastError();  int chars = _snprintf(msg, sizeof(msg) - 1,    "Tried to launch: /"%s/", but got error [%u]: ", cmdline, err);

  FormatMessage(    FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS      | FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0,    &msg[chars], (300 - chars), 0);  WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err, NULL);  returnCode = 2;  return; }

 /*  * Close our references to the write handles that have now been inherited.  */ CloseHandle(si.hStdOutput); CloseHandle(si.hStdError);

 WaitForInputIdle(pi.hProcess, 5000); CloseHandle(pi.hThread);

 /*  * Start the pipe reader threads.  */ pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID); pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID);

 /*  * Block waiting for the process to end.  */ WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess);

 /*  * Wait for our pipe to get done reading, should it be a little slow.  */ WaitForMultipleObjects(2, pipeThreads, TRUE, INFINITE); CloseHandle(pipeThreads[0]); CloseHandle(pipeThreads[1]); returnCode = 0; return;}DWORD WINAPI ReadFromPipe(LPVOID args){ pipeinfo *pi = (pipeinfo *) args; DWORD dwRead; BOOL ok; char buf[2000];

 while(true) {  ok = ReadFile(pi->pipe, buf, 2000, &dwRead, 0L);  if (!ok || dwRead == 0)  {   CloseHandle(pi->pipe);   return 0;  }  else  {   buf[dwRead] = 0;//   printf("%s", buf);         myUnicodeString->append(buf);  } }}

 

#endif

文件MyUnicodeString.h

//---------------------------------------------------------------------------

#ifndef MyUnicodeStringH#define MyUnicodeStringH//---------------------------------------------------------------------------#include #include #include using namespace std;

class MyUnicodeString{public: MyUnicodeString(); ~MyUnicodeString(); MyUnicodeString(wchar_t * wchar_ts); MyUnicodeString(UnicodeString str); MyUnicodeString(const wchar_t * wchar_ts); MyUnicodeString(int number); int indexOf(wchar_t ch); int indexOf(wchar_t * wchar_ts); int lastIndexOf(wchar_t ch); int lastIndexOf(wchar_t * wchar_ts); int getOccurCount(wchar_t* wchar_ts); int getOccurCount(wchar_t ch); int getLength(); int getLength(wchar_t * str); int getLength(const wchar_t * str); wchar_t* getReversed(); void reverse(); wchar_t* getReversed(wchar_t * str); int copy(const wchar_t* src,wchar_t* dest); int copy(wchar_t* src,wchar_t* dest); void copy(wchar_t* src,wchar_t* dest,int count); void copy(const wchar_t* src,wchar_t* dest,int count); list* split(wchar_t* splitter); list* split(UnicodeString splitter); wchar_t * getSubString(int startIndex,int count); wchar_t * getSubString(int startIndex); void rtrim(); void ltrim(); void trim(); bool startsWith(wchar_t *wchar_ts); bool endsWith(wchar_t *wchar_ts); void replace(wchar_t* oldwchar_ts,wchar_t* newwchar_ts);    void replace(UnicodeString oldStr,UnicodeString newStr); void toUpperCase(); void toLowerCase(); MyUnicodeString* operator + (wchar_t* wchar_ts); wchar_t* towchar_ts(); UnicodeString getString(); wchar_t* getCopy(); bool isInt();//Contains only numbers int toInt(); bool isLargerThan(wchar_t* wchar_ts); bool equals(wchar_t* dest); void append(const wchar_t * str); void append(UnicodeString str); void append(wchar_t * str); void append(wchar_t ch); void appendNum(int number); void append(wchar_t appendwchar_t,int toLength); void insertBefore(wchar_t insertwchar_t,int toLength); void release(); wchar_t* getSpace(int count);private: int indexOf(wchar_t ch,bool isReverse); int indexOf(wchar_t * wchar_ts,bool isReverse); int indexOf(wchar_t* src,wchar_t ch,bool isReverse); int indexOf(wchar_t* src,wchar_t * wchar_ts,bool isReverse); wchar_t * getLtrim(wchar_t* wchar_ts); wchar_t * getSubString(wchar_t* src,int startIndex,int count); wchar_t * getSubString(wchar_t* src,int startIndex); bool startsWith(wchar_t* src,wchar_t *wchar_ts); void copy(wchar_t* src); void copy(const wchar_t* src); bool isBufferNull; wchar_t * intTowchar_ts(int number); void setEndChar(wchar_t* chars,int index);protected: void replaceBuffer(wchar_t* src); wchar_t* charBuffer;};

#endif

文件MyUnicodeString.cpp

//---------------------------------------------------------------------------

#pragma hdrstop

#include "MyUnicodeString.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)const wchar_t END_CHAR('/0');

MyUnicodeString::MyUnicodeString(){ isBufferNull = true; charBuffer = NULL;}MyUnicodeString::MyUnicodeString(UnicodeString str){ isBufferNull = true; charBuffer = NULL; if(str.Length()!=0) {  copy(str.w_str()); }}MyUnicodeString::MyUnicodeString(wchar_t * wchar_ts){ isBufferNull = true; charBuffer = NULL; copy(wchar_ts);}MyUnicodeString::MyUnicodeString(const wchar_t * wchar_ts){ isBufferNull = true; copy(wchar_ts);}MyUnicodeString::MyUnicodeString(int number){ isBufferNull = true; wchar_t* wchar_ts = intTowchar_ts(number); copy(wchar_ts); free(wchar_ts);}wchar_t * MyUnicodeString::intTowchar_ts(int number){ const int maxLenForNumber = 10; wchar_t * str = getSpace(maxLenForNumber); wchar_t temp; int i = 0; if(number==0) {  str[i]='0';  i++; } else {  while(number!=0)  {   int remains = number%10;   number /= 10;   str[i] = remains+'0';   i++;  } } setEndChar(str,i); i--; for(int j=0;j

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0429s