[USB-BRINGUP-TRUNK]
[reactos.git] / base / applications / network / telnet / src / tnmisc.cpp
1 #include "precomp.h"
2
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,
7 LPHANDLE phErrRead) {
8 BOOL fCreated;
9 STARTUPINFO si;
10 SECURITY_ATTRIBUTES sa;
11 HANDLE hInRead;
12 HANDLE hOutWrite;
13 HANDLE hErrWrite;
14
15 // Create pipes
16 // initialize security attributes for handle inheritance (for WinNT)
17 sa.nLength = sizeof( sa );
18 sa.bInheritHandle = TRUE;
19 sa.lpSecurityDescriptor = NULL;
20
21 // create STDIN pipe
22 if( !CreatePipe( &hInRead, phInWrite, &sa, 0 ))
23 goto error;
24
25 // create STDOUT pipe
26 if( !CreatePipe( phOutRead, &hOutWrite, &sa, 0 ))
27 goto error;
28
29 // create STDERR pipe
30 if( !CreatePipe( phErrRead, &hErrWrite, &sa, 0 ))
31 goto error;
32
33 // process startup information
34 memset( &si, 0, sizeof( si ));
35 si.cb = sizeof( si );
36 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
37 // child process' console must be hidden for Win95 compatibility
38 si.wShowWindow = SW_HIDE;
39 // assign "other" sides of pipes
40 si.hStdInput = hInRead;
41 si.hStdOutput = hOutWrite;
42 si.hStdError = hErrWrite;
43
44 // Create a child process (suspended)
45 fCreated = CreateProcess( NULL,
46 (LPTSTR)szChildName,
47 NULL,
48 NULL,
49 TRUE,
50 0,
51 NULL,
52 NULL,
53 &si,
54 ppi );
55
56 if( !fCreated )
57 goto error;
58
59 CloseHandle( hInRead );
60 CloseHandle( hOutWrite );
61 CloseHandle( hErrWrite );
62
63 return TRUE;
64
65 error:
66 CloseHandle( hInRead );
67 CloseHandle( hOutWrite );
68 CloseHandle( hErrWrite );
69 CloseHandle( ppi->hProcess );
70 CloseHandle( ppi->hThread );
71
72 hInRead =
73 hOutWrite =
74 hErrWrite =
75 ppi->hProcess =
76 ppi->hThread = INVALID_HANDLE_VALUE;
77
78 return FALSE;
79 }
80
81 BOOL SpawnProcess(char *cmd_line, PROCESS_INFORMATION *pi) {
82 STARTUPINFO si;
83
84 memset(&si, 0, sizeof(si));
85 si.cb = sizeof(si);
86
87 return CreateProcess(cmd_line, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
88 CREATE_NEW_CONSOLE, NULL, NULL, &si, pi);
89 }
90
91 // crn@ozemail.com.au
92 int GetWin32Version(void) {
93 // return win32 version; 0 = Win32s, 1 = Win95, 2 = WinNT, 3 = Unknown -crn@ozemail.com.au
94 LPOSVERSIONINFO osv;
95 DWORD retval;
96
97 osv = new OSVERSIONINFO;
98
99 osv->dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
100 GetVersionEx (osv);
101 retval = osv->dwPlatformId;
102 delete osv;
103 return (retval);
104 }
105
106 // Paul Brannan 8/7/98
107 // This code is from Michael 'Hacker' Krelin (author of KINSole)
108 // (slightly modified)
109 HWND TelnetGetConsoleWindow() {
110 DWORD pid = GetCurrentProcessId(), wpid;
111 char title[512], *t = title;
112 HWND hrv = NULL;
113
114 #ifndef __BORLANDC__ // Ioannou Dec. 8, 1998
115 if(!GetConsoleTitle(title, sizeof(title))) t = NULL;
116
117 for(;;) {
118 if((hrv = FindWindowEx(NULL, hrv, "tty", t)) == NULL) break;
119 if(!GetWindowThreadProcessId(hrv, &wpid)) continue;
120 if(wpid == pid) return hrv;
121 }
122 #endif
123
124 return GetForegroundWindow();
125 }
126
127 // Sets the icon of the console window to hIcon
128 // If hIcon is 0, then use a default icon
129 // hConsoleWindow must be set before calling SetIcon
130 bool SetIcon(HWND hConsoleWindow, HANDLE hIcon, LPARAM *pOldBIcon, LPARAM *pOldSIcon,
131 const char *icondir) {
132 if(!hConsoleWindow) return false;
133
134 // FIX ME!!! The LoadIcon code should work with any compiler!
135 // (Paul Brannan 12/17/98)
136 #ifndef __BORLANDC__ // Ioannou Dec. 8, 1998
137 if(!hIcon) {
138 char filename[MAX_PATH]; // load from telnet.ico
139 _snprintf(filename, MAX_PATH - 1, "%s%s", icondir, "telnet.ico");
140 filename[MAX_PATH - 1] = '\0';
141
142 // Note: loading the icon from a file doesn't work on NT
143 // There is no LoadImage in Borland headers - only LoadIcon
144 hIcon = LoadImage(NULL, filename, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE +
145 LR_LOADFROMFILE);
146 }
147 #else
148 // load the icon from the resource file -crn@ozemail.com.au 16/12/98
149 if(!hIcon) {
150 hIcon = LoadIcon ((HANDLE)GetWindowLongPtr(hConsoleWindow,
151 GWL_HINSTANCE), "TELNETICON");
152 }
153 #endif
154
155 if(hIcon) {
156 #ifdef ICON_BIG
157 *pOldBIcon = SendMessage(hConsoleWindow, WM_SETICON, ICON_BIG,
158 (LPARAM)hIcon);
159 #endif
160 #ifdef ICON_SMALL
161 *pOldSIcon = SendMessage(hConsoleWindow, WM_SETICON, ICON_SMALL,
162 (LPARAM)hIcon);
163 #endif
164 return true;
165 } else {
166 // Otherwise we get a random icon at exit! (Paul Brannan 9/13/98)
167 return false;
168 }
169 }
170
171 // Allows SetIcon to be called again by resetting the current icon
172 // Added 12/17/98 by Paul Brannan
173 void ResetIcon(HWND hConsoleWindow, LPARAM oldBIcon, LPARAM oldSIcon) {
174 #ifdef ICON_BIG
175 SendMessage(hConsoleWindow, WM_SETICON, ICON_BIG, (LPARAM)oldBIcon);
176 #endif
177 #ifdef ICON_SMALL
178 SendMessage(hConsoleWindow, WM_SETICON, ICON_SMALL, (LPARAM)oldSIcon);
179 #endif
180 }