3 // from the PVAX (http://www.ccas.ru/~posp/popov/spawn.htm)
4 // Create a process with pipes to stdin/out/err
5 BOOL
CreateHiddenConsoleProcess(LPCTSTR szChildName
, PROCESS_INFORMATION
* ppi
,
6 LPHANDLE phInWrite
, LPHANDLE phOutRead
,
10 SECURITY_ATTRIBUTES sa
;
11 HANDLE hInRead
= INVALID_HANDLE_VALUE
;
12 HANDLE hOutWrite
= INVALID_HANDLE_VALUE
;
13 HANDLE hErrWrite
= INVALID_HANDLE_VALUE
;
16 // initialize security attributes for handle inheritance (for WinNT)
17 sa
.nLength
= sizeof( sa
);
18 sa
.bInheritHandle
= TRUE
;
19 sa
.lpSecurityDescriptor
= NULL
;
22 if( !CreatePipe( &hInRead
, phInWrite
, &sa
, 0 )) {
23 hInRead
= INVALID_HANDLE_VALUE
;
28 if( !CreatePipe( phOutRead
, &hOutWrite
, &sa
, 0 )) {
29 hOutWrite
= INVALID_HANDLE_VALUE
;
34 if( !CreatePipe( phErrRead
, &hErrWrite
, &sa
, 0 )) {
35 hErrWrite
= INVALID_HANDLE_VALUE
;
39 // process startup information
40 memset( &si
, 0, sizeof( si
));
42 si
.dwFlags
= STARTF_USESHOWWINDOW
| STARTF_USESTDHANDLES
;
43 // child process' console must be hidden for Win95 compatibility
44 si
.wShowWindow
= SW_HIDE
;
45 // assign "other" sides of pipes
46 si
.hStdInput
= hInRead
;
47 si
.hStdOutput
= hOutWrite
;
48 si
.hStdError
= hErrWrite
;
50 // Create a child process (suspended)
51 fCreated
= CreateProcess( NULL
,
65 CloseHandle( hInRead
);
66 CloseHandle( hOutWrite
);
67 CloseHandle( hErrWrite
);
72 if (hInRead
!= INVALID_HANDLE_VALUE
) CloseHandle( hInRead
);
73 if (hOutWrite
!= INVALID_HANDLE_VALUE
) CloseHandle( hOutWrite
);
74 if (hErrWrite
!= INVALID_HANDLE_VALUE
) CloseHandle( hErrWrite
);
75 CloseHandle( ppi
->hProcess
);
76 CloseHandle( ppi
->hThread
);
82 ppi
->hThread
= INVALID_HANDLE_VALUE
;
87 BOOL
SpawnProcess(char *cmd_line
, PROCESS_INFORMATION
*pi
) {
90 memset(&si
, 0, sizeof(si
));
93 return CreateProcess(cmd_line
, NULL
, NULL
, FALSE
, NORMAL_PRIORITY_CLASS
,
94 CREATE_NEW_CONSOLE
, NULL
, NULL
, &si
, pi
);
98 int GetWin32Version(void) {
99 // return win32 version; 0 = Win32s, 1 = Win95, 2 = WinNT, 3 = Unknown -crn@ozemail.com.au
103 osv
= new OSVERSIONINFO
;
105 osv
->dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
107 retval
= osv
->dwPlatformId
;
112 // Paul Brannan 8/7/98
113 // This code is from Michael 'Hacker' Krelin (author of KINSole)
114 // (slightly modified)
115 HWND
TelnetGetConsoleWindow() {
116 DWORD pid
= GetCurrentProcessId(), wpid
;
117 char title
[512], *t
= title
;
120 #ifndef __BORLANDC__ // Ioannou Dec. 8, 1998
121 if(!GetConsoleTitle(title
, sizeof(title
))) t
= NULL
;
124 if((hrv
= FindWindowEx(NULL
, hrv
, "tty", t
)) == NULL
) break;
125 if(!GetWindowThreadProcessId(hrv
, &wpid
)) continue;
126 if(wpid
== pid
) return hrv
;
130 return GetForegroundWindow();
133 // Sets the icon of the console window to hIcon
134 // If hIcon is 0, then use a default icon
135 // hConsoleWindow must be set before calling SetIcon
136 bool SetIcon(HWND hConsoleWindow
, HANDLE hIcon
, LPARAM
*pOldBIcon
, LPARAM
*pOldSIcon
,
137 const char *icondir
) {
138 if(!hConsoleWindow
) return false;
140 // FIX ME!!! The LoadIcon code should work with any compiler!
141 // (Paul Brannan 12/17/98)
142 #ifndef __BORLANDC__ // Ioannou Dec. 8, 1998
144 char filename
[MAX_PATH
]; // load from telnet.ico
145 _snprintf(filename
, MAX_PATH
- 1, "%s%s", icondir
, "telnet.ico");
146 filename
[MAX_PATH
- 1] = '\0';
148 // Note: loading the icon from a file doesn't work on NT
149 // There is no LoadImage in Borland headers - only LoadIcon
150 hIcon
= LoadImage(NULL
, filename
, IMAGE_ICON
, 0, 0, LR_DEFAULTSIZE
+
154 // load the icon from the resource file -crn@ozemail.com.au 16/12/98
156 hIcon
= LoadIcon((HANDLE
)GetWindowLongPtr(hConsoleWindow
,
157 GWLP_HINSTANCE
), "TELNETICON");
163 *pOldBIcon
= SendMessage(hConsoleWindow
, WM_SETICON
, ICON_BIG
,
167 *pOldSIcon
= SendMessage(hConsoleWindow
, WM_SETICON
, ICON_SMALL
,
172 // Otherwise we get a random icon at exit! (Paul Brannan 9/13/98)
177 // Allows SetIcon to be called again by resetting the current icon
178 // Added 12/17/98 by Paul Brannan
179 void ResetIcon(HWND hConsoleWindow
, LPARAM oldBIcon
, LPARAM oldSIcon
) {
181 SendMessage(hConsoleWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)oldBIcon
);
184 SendMessage(hConsoleWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)oldSIcon
);