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