#include "precomp.h"
BOOL CreateHiddenConsoleProcess(LPCTSTR szChildName, PROCESS_INFORMATION* ppi,
LPHANDLE phInWrite, LPHANDLE phOutRead,
LPHANDLE phErrRead) {
BOOL fCreated;
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
HANDLE hInRead = INVALID_HANDLE_VALUE;
HANDLE hOutWrite = INVALID_HANDLE_VALUE;
HANDLE hErrWrite = INVALID_HANDLE_VALUE;
sa.nLength = sizeof( sa );
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if( !CreatePipe( &hInRead, phInWrite, &sa, 0 )) {
hInRead = INVALID_HANDLE_VALUE;
goto error;
}
if( !CreatePipe( phOutRead, &hOutWrite, &sa, 0 )) {
hOutWrite = INVALID_HANDLE_VALUE;
goto error;
}
if( !CreatePipe( phErrRead, &hErrWrite, &sa, 0 )) {
hErrWrite = INVALID_HANDLE_VALUE;
goto error;
}
memset( &si, 0, sizeof( si ));
si.cb = sizeof( si );
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hInRead;
si.hStdOutput = hOutWrite;
si.hStdError = hErrWrite;
fCreated = CreateProcess( NULL,
(LPTSTR)szChildName,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
ppi );
if( !fCreated )
goto error;
CloseHandle( hInRead );
CloseHandle( hOutWrite );
CloseHandle( hErrWrite );
return TRUE;
error:
if (hInRead != INVALID_HANDLE_VALUE) CloseHandle( hInRead );
if (hOutWrite != INVALID_HANDLE_VALUE) CloseHandle( hOutWrite );
if (hErrWrite != INVALID_HANDLE_VALUE) CloseHandle( hErrWrite );
CloseHandle( ppi->hProcess );
CloseHandle( ppi->hThread );
hInRead =
hOutWrite =
hErrWrite =
ppi->hProcess =
ppi->hThread = INVALID_HANDLE_VALUE;
return FALSE;
}
BOOL SpawnProcess(char *cmd_line, PROCESS_INFORMATION *pi) {
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
return CreateProcess(cmd_line, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
CREATE_NEW_CONSOLE, NULL, NULL, &si, pi);
}
int GetWin32Version(void) {
LPOSVERSIONINFO osv;
DWORD retval;
osv = new OSVERSIONINFO;
osv->dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (osv);
retval = osv->dwPlatformId;
delete osv;
return (retval);
}
HWND TelnetGetConsoleWindow() {
DWORD pid = GetCurrentProcessId(), wpid;
char title[512], *t = title;
HWND hrv = NULL;
#ifndef __BORLANDC__
if(!GetConsoleTitle(title, sizeof(title))) t = NULL;
for(;;) {
if((hrv = FindWindowEx(NULL, hrv, "tty", t)) == NULL) break;
if(!GetWindowThreadProcessId(hrv, &wpid)) continue;
if(wpid == pid) return hrv;
}
#endif
return GetForegroundWindow();
}
bool SetIcon(HWND hConsoleWindow, HANDLE hIcon, LPARAM *pOldBIcon, LPARAM *pOldSIcon,
const char *icondir) {
if(!hConsoleWindow) return false;
#ifndef __BORLANDC__
if(!hIcon) {
char filename[MAX_PATH];
_snprintf(filename, MAX_PATH - 1, "%s%s", icondir, "telnet.ico");
filename[MAX_PATH - 1] = '\0';
hIcon = LoadImage(NULL, filename, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE +
LR_LOADFROMFILE);
}
#else
if(!hIcon) {
hIcon = LoadIcon((HANDLE)GetWindowLongPtr(hConsoleWindow,
GWLP_HINSTANCE), "TELNETICON");
}
#endif
if(hIcon) {
#ifdef ICON_BIG
*pOldBIcon = SendMessage(hConsoleWindow, WM_SETICON, ICON_BIG,
(LPARAM)hIcon);
#endif
#ifdef ICON_SMALL
*pOldSIcon = SendMessage(hConsoleWindow, WM_SETICON, ICON_SMALL,
(LPARAM)hIcon);
#endif
return true;
} else {
return false;
}
}
void ResetIcon(HWND hConsoleWindow, LPARAM oldBIcon, LPARAM oldSIcon) {
#ifdef ICON_BIG
SendMessage(hConsoleWindow, WM_SETICON, ICON_BIG, (LPARAM)oldBIcon);
#endif
#ifdef ICON_SMALL
SendMessage(hConsoleWindow, WM_SETICON, ICON_SMALL, (LPARAM)oldSIcon);
#endif
}