home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / startk95.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  4KB  |  115 lines

  1. /* This example code shows how to start K95 from within another Win32 */
  2. /* application. */
  3.  
  4. #include <windows.h>
  5.  
  6. HANDLE
  7. StartK95WithScript( char * script ) {
  8.     PROCESS_INFORMATION K95ProcessInfo ;
  9.     OSVERSIONINFO osverinfo ;
  10.     STARTUPINFO si ;
  11.     HANDLE hOut, hIn ;
  12.     CHAR cmdstr[256];
  13.  
  14.     /* Initialize the process Startup Information structure */
  15.     memset( &si, 0, sizeof(STARTUPINFO) ) ;
  16.     si.cb = sizeof(STARTUPINFO);
  17.  
  18.     /* Find out what platform we are running on */
  19.     osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
  20.     GetVersionEx( &osverinfo ) ;
  21.     
  22.     /* Are we running on Windows 95? */
  23.     if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) {
  24.     /*
  25.     This really bad hack of code is here only because Windows 95 doesn't do
  26.     what it is supposed to be doing, namely properly constructing the 
  27.     console window for its child process.  Therefore, we must do it 
  28.     manually.  This is only necessary when starting K95 from a GUI
  29.     application.
  30.     */
  31.     AllocConsole() ;
  32.  
  33.     /* Get a handle to Standout Out */
  34.     hOut = CreateFile( "CONOUT$", GENERIC_READ | GENERIC_WRITE, 
  35.                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
  36.                                   NULL,
  37.                                   OPEN_EXISTING,
  38.                                   0,
  39.                                   0) ;
  40.  
  41.     /* Get a handle to Standout In */
  42.     hIn = CreateFile( "CONIN$", GENERIC_READ | GENERIC_WRITE, 
  43.                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
  44.                                   NULL,
  45.                                   OPEN_EXISTING,
  46.                                   0,
  47.                                   0) ;
  48.  
  49.     /* Setup the StartupInfo structure to use our new handles */
  50.     si.dwFlags = (DWORD) STARTF_USESTDHANDLES ;
  51.     DuplicateHandle( GetCurrentProcess(), hOut, GetCurrentProcess(), 
  52.              &si.hStdOutput,
  53.                        DUPLICATE_SAME_ACCESS, TRUE, 
  54.              DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS ) ;
  55.     si.hStdError = si.hStdOutput ;
  56.     DuplicateHandle( GetCurrentProcess(), hIn, GetCurrentProcess(), 
  57.              &si.hStdInput,
  58.                        DUPLICATE_SAME_ACCESS, TRUE, 
  59.              DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS ) ;
  60.     }
  61.  
  62.     si.dwFlags |= (DWORD) STARTF_USECOUNTCHARS | 
  63.     STARTF_USEFILLATTRIBUTE |
  64.     STARTF_USESHOWWINDOW;
  65.     si.dwXCountChars = (DWORD) 80;    /* width is 80 cols   */
  66.     si.dwYCountChars = (DWORD) 25;    /* height is 25 lines */
  67.     si.dwFillAttribute = (DWORD) 0x07;    /* white on black */
  68.     si.wShowWindow = SW_SHOWMAXIMIZED ;
  69.  
  70.     /* wShowWindow may be set to any of these values
  71.        SW_SHOWNORMAL     
  72.        SW_SHOWMINIMIZED  
  73.        SW_SHOWMAXIMIZED  
  74.        SW_SHOWNOACTIVATE 
  75.        SW_SHOW           
  76.        SW_SHOWMINNOACTIVE
  77.        SW_SHOWNA         
  78.        SW_SHOWDEFAULT    
  79.     */
  80.  
  81.     /* build executable string */
  82.     sprintf( cmdstr, "k95.exe %s", script ) ;
  83.  
  84.     /* Start K95 */
  85.     if (!CreateProcess((LPSTR)NULL,                    /* start K-95  */
  86.             (LPSTR)cmdstr,                 /* give it the file */
  87.             (LPSECURITY_ATTRIBUTES)NULL,   /* fix if necessary */
  88.             (LPSECURITY_ATTRIBUTES)NULL,   /* fix if necessary */
  89.             FALSE,                          /* fix if necessary */
  90.             (DWORD) CREATE_NEW_PROCESS_GROUP,    
  91.             (LPVOID)NULL,                  /* fix if necessary */
  92.             (LPSTR)NULL,                   /* Current directory */
  93.             &si,                           /* Startup info, fix */
  94.             &K95ProcessInfo        /* Process info */
  95.             )) 
  96.     {
  97.     /* Error has occurred, call GetLastError() to find out why */
  98.  
  99.     /* If Windows 95, cleanup the console we created */
  100.     if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
  101.     {
  102.         FreeConsole();
  103.     }
  104.     return(NULL);
  105.     } else
  106.     {
  107.     /* If Windows 95, cleanup the console we created */
  108.     if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
  109.     {
  110.         FreeConsole();
  111.     }
  112.     return ((HANDLE) K95ProcessInfo.hProcess);
  113.     }
  114. }
  115.