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