Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
3
Добавлен:
28.06.2014
Размер:
9.75 Кб
Скачать
/*
 #########################################################################
 ## Installation program for Windows 95 / NT driver                     ##
 ##                    Main module ( linked with DIALOG.C )             ##
 ##                                                 by Gonzo 1997-98    ##
 #########################################################################
*/
#include <windows.h>
#include <stdio.h>

#include "instdrv.h"
#include "instdrvs.h"
#include "instdrvf.h"
#include "inststr.h"


 static char szText[0x100];

 TDriverState DrvState;    // Current state of driver

 BOOL bHide;               // Quiet mode: Nothing to screen!
 BOOL bAlwaysAskForReboot; // Always ask. No matter /Q or not.
 BOOL bSkipReboot;         // No reboot at all;
 BOOL bInfo;               // Help mode: display info!
 BOOL bPrintLog;           // Print each action to file INSTDRV.LOG
 BOOL bWinNT;              // System type: NT or 95
 int  bRus;                // TRUE - russian. This value is 0 or 1000
 HINSTANCE hMainInstance;  // Instance of application for resource loading
 int  CurDrvVer;           // Version of driver with this utility.
                           // Appears on dialog in left lower corner

 char*               RS                    ( int  );
 int                 MakeOperation         ( int  );
 void                TryToExitWindows      ( void );
 extern BOOL _export CALLBACK MsgDlgProc   ( HWND, UINT, WPARAM, LPARAM);
 extern int          CheckDriver           ( void );

// This function is nessesary then INSTAPI.DLL isn't used...
// Please don't try to define INSTALL_WITHOUT_DLL
#ifdef INSTALL_WITHOUT_DLL
 extern int          InitLibrary           ( HINSTANCE );
#endif


/*-----------------------------------------------------------------*/
// Returns string from resource in current language
char * RS(int ID)
{
 static char szResourceBuffer[255];

 if (!LoadString( hMainInstance,ID + bRus,szResourceBuffer,255))
 {
    sprintf (szText,"Cannot load string resource Number %d (Error %ld)",ID,GetLastError());
    MessageBox(NULL,szText,"Fatal error",MB_);
    strcpy(szResourceBuffer,"---------\0");
 }
 return szResourceBuffer;
}

//-------------------------------------------------------------------------
// This routine makes install/unistall operations and show status message
//-------------------------------------------------------------------------
int  MakeOperation( int  nCommand )
{
 int nRet;
 HCURSOR hCur;

 hCur = SetCursor(LoadCursor(NULL,IDC_WAIT));

 // if Uninstall ==> GD_RemoveDriver()
 // In all another cases GD_ReInstall()
 if( nCommand & COMMAND_UNINST )  // Uninstall driver
    nRet = GD_RemoveDriver( TRUE );
 else                             // Install or ReInstall driver 
    nRet =  GD_ReInstallDriver();

 // RebootNeeded isn't error.
 if ( nRet == ge_REBOOT_NEEDED )
 {
     DrvState.RebootNeeded = 1;
     nRet = ge_NO_ERROR;
 }

 // Here we build and show status message
 if ( nRet == ge_NO_ERROR )
 {   if ( bWinNT )
         sprintf (szText,RS(S_OKOK),"NT");
     else
         sprintf (szText,RS(S_OKOK),"95");
     if ( nCommand & COMMAND_UNINST )
     {
       if ( bRus )  strcat (szText,"удален");
       else         strcat (szText,"uninstalled");
     }
     else
     {
       if ( bRus )  strcat  (szText,"установлен");
       else         strcat  (szText,"installed");
     }
 }
 else
 {
    if ( nCommand & COMMAND_UNINST)
    {
        if ( bRus )  sprintf  (szText,RS(S_FAIL),"удаления");
        else         sprintf  (szText,RS(S_FAIL),"uninstallation");
    }
    else
    {
      if ( bRus ) sprintf  (szText,RS(S_FAIL),"установки");
      else        sprintf  (szText,RS(S_FAIL),"installation");
    }
  }
 if  (!bHide)
    MessageBox( NULL, szText, RS(S_TITLE), MB_ | MB_ICONQUESTION );

 SetCursor(hCur);

 if ( DrvState.RebootNeeded )
     TryToExitWindows();
 return nRet;

}
//*********************************************************************************
//  This routine asks user for reboot and makes it if [YES] pressed.
//
void TryToExitWindows()
{
 if ( bSkipReboot ) 
    return;
 sprintf(szText,RS(S_REBOOT));

 if (((!bHide)||(bAlwaysAskForReboot))&&
     (MessageBox (NULL,szText,RS(S_TITLE),MB_YESNO+MB_TASKMODAL)==IDNO))
     {
        GD_PrintLog ("User cancel reboot ....");
        return;
     }
 GD_PrintLog ("-- Restarting Windows....");

 ExitWindowsEx (EWX_REBOOT, 0);
 exit(4);

}
/*
*********************************************************************************
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
   ##   ##   ####  ######  ##   ##   | | | | | | | | | | | | | | | | | | | | | |
|  ### ###  ##  ##   ##    ###  ##  | | | | | | | | | | | | | | | | | | | | | |
   #######  ######   ##    #### ##   | | | | | | | | | | | | | | | | | | | | | |
|  ## # ##  ##  ##   ##    ## ####  | | | | | | | | | | | | | | | | | | | | | |
   ##   ##  ##  ## ######| ##   ##   | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
*********************************************************************************
*/

int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpszCmdLine, int nCmdShow )
{

 int   nCommand = 0;
 int   nRet;
 DWORD dwConfigFlags;

 hMainInstance = hInstance;
 strupr( lpszCmdLine );

 // This call is nessesary then INSTAPI.DLL isn't used...
 // Please don't try to define INSTALL_WITHOUT_DLL
#ifdef INSTALL_WITHOUT_DLL
 if (InitLibrary(hInstance)!=ge_NO_ERROR)
    return 6;
#endif

//------------------------------------------------------------------------

 GD_PrintLog ("-- Starting INSTDRV....");

 // Get versions of driver and system...
 GD_GetVersions    (&bWinNT, &(BOOL)bRus, &CurDrvVer );
 // We change bRus to use it in resource strings loading.
 bRus *= 1000;

 {
  //------------------------------------------------------------------------
  // This block checks if INSTDRV already started and activates started copy
  // to prevent two runnig copies.
    HWND hW;
    HWND hW_cfg;

    //Find other running copy of the same Instdrv
    if ((hW = FindWindow( NULL, RS(S_TITLE)))!=NULL)
    {
        if ((hW_cfg = FindWindow( NULL, RS(S_TITLE_CFG)))!=NULL)
            SetForegroundWindow(hW_cfg);
        else
            SetForegroundWindow(hW);
        return 0;
    }
    //Now find the old version of the Instdrv (with 'tm' on window hrader)
    if ((hW = FindWindow( NULL, RS(S_TITLE2)))!=NULL)
    {
        if ((hW_cfg = FindWindow( NULL, RS(S_TITLE_CFG2)))!=NULL)
            SetForegroundWindow(hW_cfg);
        else
            SetForegroundWindow(hW);
        return 0;
    }
 }

 //-------------------  Checking the command line options -----------------
 // Check: install or uninstall mode (/U)
 nCommand |= strstr( lpszCmdLine, "/U" ) ? COMMAND_UNINST : 0;

 // Check: no messages (/Q)
 bHide  = strstr( lpszCmdLine, "/Q"  ) ? TRUE : FALSE;

 // Check: always asking for reboot
 bAlwaysAskForReboot = strstr( lpszCmdLine, "/RB"  ) ? TRUE : FALSE;

 // Check: skip reboot
 bSkipReboot = strstr( lpszCmdLine, "/NORB"  ) ? TRUE : FALSE;

 // Check: information (/I)
 bInfo  = ((! bHide) && (strstr( lpszCmdLine, "/I" )) ? TRUE : FALSE);

 // Check: print log (/LOG)
 bPrintLog = (strstr( lpszCmdLine, "/LOG" )) ? TRUE : FALSE;


 if( strstr( lpszCmdLine, "/LR" ))       bRus=1000;
 if( strstr( lpszCmdLine, "/LE" ))       bRus=0;

 // Check /? || /H - display help
 if( strstr( lpszCmdLine, "/?" ) || strstr( lpszCmdLine, "/H" ) )
 {
        strcpy( szText,RS(S_INFO1));
        strcat( szText,RS(S_INFO2));
        MessageBox( NULL, szText, RS(S_TITLE), MB_ | MB_ICONINFORMATION  );
        return -1;
 }

 // Fill DrvState with zeroes....
 memset (&DrvState,sizeof(DrvState),0);

 // Set mode of messages showing in dll

 dwConfigFlags  = (!bHide)?gic_SHOWMESSAGES:0;
 dwConfigFlags |= ( bInfo)?gic_FULLINFO:0;
 dwConfigFlags |= ( bPrintLog)?gic_PRINTLOG:0;
 GD_SetInstdrvConfig ( dwConfigFlags );

 // Check is driver already present or not... Filling DI structure
 // (see DIALOG.C for implementation of this routine)
 nRet = CheckDriver();
 if ((nRet == ge_SOURCE_FILE_NOT_FOUND) ||
     (nRet == ge_DRIVER_BUSY))
 {
    GD_GetLastErrorDescription(szText,0x100);
    if ((!bHide)&&(!bInfo))
       MessageBox(NULL,szText,"Error ",MB_);
    if (nRet == ge_SOURCE_FILE_NOT_FOUND) 
       exit(3);
    if (nRet == ge_DRIVER_BUSY)
       exit(5);
 }

 // *********** Here main fork: Simple operation in 'quiet' mode  ***********
 // ***********  or dialog in another case  *********************************
 if ( bHide )
 {
       if (!MakeOperation(nCommand))
           return 0;
       else
           return 3; //Can't execute operation
 }
 else
 {
       // This cycle like: DIALOG -> OPERATION -> DIALOG -> ...

       while (1)
       {
          // This dialog returns if user press Install/ReInstall/Remove/Exit buttons
          //--------------------------------------------------
          nCommand = DialogBox(hMainInstance,MAKEINTRESOURCE(IDD_MSG),GetDesktopWindow(),(DLGPROC)MsgDlgProc);
          //--------------------------------------------------
          if (nCommand & COMMAND_EXIT) break; //Exit code...
          MakeOperation(nCommand);
          CheckDriver();	
       }
 }
  
 if ( DrvState.RebootNeeded )
    TryToExitWindows();

 GD_PrintLog ("-- INSTDRV successful finished.");

 return 0;      // All ok

}


Соседние файлы в папке INST.DLL