tnmisc.cpp
上传用户:tigerk9
上传日期:2020-03-10
资源大小:237k
文件大小:5k
源码类别:

Telnet客户端

开发平台:

Visual C++

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