1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
class VirtualConsole{
HANDLE near_end, //Our end of the pipe, where we'll write.
far_end, //The other end.
process; //The client.
public:
bool good;
VirtualConsole(const std::string &name,ulong color);
~VirtualConsole();
void put(const char *str,size_t size=0);
void put(const std::string &str){
this->put(str.c_str(),str.size());
}
};
VirtualConsole::VirtualConsole(const std::string &name,ulong color){
this->good=0;
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=1;
sa.lpSecurityDescriptor=0;
if (!CreatePipe(&this->far_end,&this->near_end,&sa,0)){
assert(this->near_end==INVALID_HANDLE_VALUE);
return;
}
SetHandleInformation(this->near_end,HANDLE_FLAG_INHERIT,0);
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&pi,sizeof(pi));
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(STARTUPINFO);
si.hStdInput=this->far_end;
si.dwFlags|=STARTF_USESTDHANDLES;
TCHAR program[]=TEXT("console.exe");
TCHAR arguments[100];
#ifndef UNICODE
sprintf(arguments,"%d",color);
#else
swprintf(arguments,L"0 %d",color);
#endif
if (!CreateProcess(program,arguments,0,0,1,CREATE_NEW_CONSOLE|CREATE_UNICODE_ENVIRONMENT,0,0,&si,&pi))
return;
this->process=pi.hProcess;
CloseHandle(pi.hThread);
this->good=1;
this->put(name);
this->put("\n",1);
}
VirtualConsole::~VirtualConsole(){
if (this->near_end!=INVALID_HANDLE_VALUE){
if (this->process!=INVALID_HANDLE_VALUE){
TerminateProcess(this->process,0);
CloseHandle(this->process);
}
CloseHandle(this->near_end);
CloseHandle(this->far_end);
}
}
void VirtualConsole::put(const char *str,size_t size){
if (!this->good)
return;
if (!size)
size=strlen(str);
DWORD l;
WriteFile(this->near_end,str,size,&l,0);
}
|