コード例 #1
0
int 
PreInstallFromInf(
    IN HWND hWnd,
    IN LPTSTR Inf,
    IN LPTSTR InstallSection,
    IN LPTSTR ServiceSection
    )
{
    DWORD Err = NO_ERROR;
    HINF hInf = INVALID_HANDLE_VALUE;
    UINT ErrorLine;
    PVOID Context = NULL;

    hInf = SetupOpenInfFile(Inf,
                            NULL,
                            INF_STYLE_WIN4,
                            &ErrorLine);
    if (hInf == INVALID_HANDLE_VALUE) {
        Err = GetLastError();
        goto clean;
    }

    //
    // Install the service section
    //
    if (!SetupInstallServicesFromInfSection(hInf,
                                            ServiceSection,
                                            0)) {
        Err = GetLastError();
        goto clean;
    }

    Context = SetupInitDefaultQueueCallback(hWnd);

    if (!SetupInstallFromInfSection(hWnd,
                                    hInf,
                                    InstallSection,
                                    SPINST_REGISTRY | SPINST_FILES,
                                    NULL,
                                    NULL,
                                    0,
                                    SetupDefaultQueueCallback,
                                    Context,
                                    NULL,
                                    NULL)) {
        Err = GetLastError();
        goto clean;
    }

clean:
    if (Context != NULL) {
        SetupTermDefaultQueueCallback(Context);
    }
    if (hInf != INVALID_HANDLE_VALUE) {
        SetupCloseInfFile(hInf);
    }
    if (Err == NO_ERROR) {
        return 0;
    } else {
        return 1;
    }
}
コード例 #2
0
ファイル: VBoxDrvInst.cpp プロジェクト: ElvisTulip/vbox
/**
 * Executes a sepcified .INF section to install/uninstall drivers and/or services.
 *
 * @return  Exit code (EXIT_OK, EXIT_FAIL)
 * @param   pszSection          Section to execute; usually it's "DefaultInstall".
 * @param   iMode               Execution mode to use (see MSDN).
 * @param   pszInf              Full qualified path of the .INF file to use.
 */
int ExecuteInfFile(const _TCHAR *pszSection, int iMode, const _TCHAR *pszInf)
{
    _tprintf(_T("Installing from INF-File: %ws (Section: %ws) ...\n"),
             pszInf, pszSection);

    UINT uErrorLine = 0;
    HINF hINF = SetupOpenInfFile(pszInf, NULL, INF_STYLE_WIN4, &uErrorLine);
    if (hINF != INVALID_HANDLE_VALUE)
    {
        PVOID pvQueue = SetupInitDefaultQueueCallback(NULL);

        BOOL fSuccess = SetupInstallFromInfSection(NULL,
                                                    hINF,
                                                    pszSection,
                                                    SPINST_ALL,
                                                    HKEY_LOCAL_MACHINE,
                                                    NULL,
                                                    SP_COPY_NEWER_OR_SAME | SP_COPY_NOSKIP,
                                                    vboxDrvInstExecuteInfFileCallback,
                                                    pvQueue,
                                                    NULL,
                                                    NULL
                                                    );
        if (fSuccess)
        {
            _tprintf (_T( "File installation stage successful\n"));

            fSuccess = SetupInstallServicesFromInfSection(hINF,
                                                          L"DefaultInstall.Services",
                                                          0 /* Flags */);
            if (fSuccess)
            {
                _tprintf (_T( "Service installation stage successful. Installation completed\n"));
            }
            else
            {
                DWORD dwErr = GetLastError();
                switch (dwErr)
                {
                    case ERROR_SUCCESS_REBOOT_REQUIRED:
                        _tprintf (_T( "A reboot is required to complete the installation\n"));
                        break;

                    case ERROR_SECTION_NOT_FOUND:
                        break;

                    default:
                        _tprintf (_T( "Error %ld while installing service\n"), dwErr);
                        break;
                }
            }
        }
        else
            _tprintf (_T( "Error %ld while installing files\n"), GetLastError());

        if (pvQueue)
            SetupTermDefaultQueueCallback(pvQueue);

        SetupCloseInfFile(hINF);
    }
    else
        _tprintf (_T( "Unable to open %ws: %ld (error line %u)\n"),
                  pszInf, GetLastError(), uErrorLine);

    return EXIT_OK;
}
コード例 #3
0
/**
 * Install the VBox video driver.
 *
 * @returns TRUE on success.
 * @returns FALSE on failure.
 * @param   szDriverDir     The base directory where we find the INF.
 */
BOOL installVideoDriver(TCHAR* szDriverDir)
{
  HDEVINFO hDevInfo;
  SP_DEVINSTALL_PARAMS DeviceInstallParams={0};
  SP_DRVINFO_DATA drvInfoData={0};
  SP_DRVINFO_DETAIL_DATA DriverInfoDetailData={0};

  DWORD cbReqSize;

  /* Vars used for reading the INF */
  HINF hInf;
  TCHAR szServiceSection[LINE_LEN];
  INFCONTEXT serviceContext;
  TCHAR szServiceData[LINE_LEN];
  TCHAR deviceRegStr[1000];//I'm lazy here. 1000 ought to be enough for everybody...

  SP_DEVINFO_DATA deviceInfoData;
  DWORD configFlags;

  HKEY hkey;
  DWORD disp;
  TCHAR regKeyName[LINE_LEN];

  BOOL rc;

  /* Create an empty list */
  hDevInfo = SetupDiCreateDeviceInfoList((LPGUID) &GUID_DEVCLASS_DISPLAY,
                                         NULL);

  if (hDevInfo == INVALID_HANDLE_VALUE)
    return FALSE;

  memset(&DeviceInstallParams, 0, sizeof(SP_DEVINSTALL_PARAMS));
  DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);

  rc=SetupDiGetDeviceInstallParams(hDevInfo,
                                   NULL,
                                   &DeviceInstallParams);

  if(!rc)
    return FALSE;

  DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
  DeviceInstallParams.Flags |= DI_NOFILECOPY | /* We did our own file copying */
    DI_DONOTCALLCONFIGMG |
    DI_ENUMSINGLEINF; /* .DriverPath specifies an inf file */


  /* Path to inf file */
  wsprintf(DeviceInstallParams.DriverPath,
           TEXT("%ws\\%ws"),
           szDriverDir, TEXT(VBOXGUEST_VIDEO_INF_NAME));

  rc=SetupDiSetDeviceInstallParams(hDevInfo,
                                   NULL,
                                   &DeviceInstallParams);
  if(!rc)
    return FALSE;

  /* Read the drivers from the inf file */
  if (!SetupDiBuildDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER))
    {
      SetupDiDestroyDeviceInfoList(hDevInfo);
      return FALSE;
    }

  /* Get the first found driver.
     Our Inf file only contains one so this is fine  */
  drvInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
  if(FALSE==SetupDiEnumDriverInfo(hDevInfo, NULL, SPDIT_CLASSDRIVER,
                                  0, &drvInfoData)){
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return FALSE;
  }

  /* Get necessary driver details */
  DriverInfoDetailData.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
  if (!(!SetupDiGetDriverInfoDetail(hDevInfo,
                                    NULL,
                                    &drvInfoData,
                                    &DriverInfoDetailData,
                                    DriverInfoDetailData.cbSize,
                                    &cbReqSize)
        &&GetLastError()== ERROR_INSUFFICIENT_BUFFER) )
    {
      SetupDiDestroyDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
      SetupDiDestroyDeviceInfoList(hDevInfo);
      return FALSE;
    }

  hInf = SetupOpenInfFile(DriverInfoDetailData.InfFileName,
                          NULL, INF_STYLE_WIN4, NULL);

  if (hInf == INVALID_HANDLE_VALUE)
    {
      SetupDiDestroyDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
      SetupDiDestroyDeviceInfoList(hDevInfo);
      return FALSE;
    }

  /* First install the service */
  wsprintf(szServiceSection, TEXT("%ws.Services"),
           DriverInfoDetailData.SectionName);

  if(!SetupFindFirstLine(hInf, szServiceSection, NULL, &serviceContext))
    {
      /* No service line?? Can't be... */
      closeAndDestroy(hDevInfo, hInf);
      return FALSE;
    }

  /* Get the name */
  SetupGetStringField(&serviceContext,
                      1,
                      szServiceData,
                      sizeof(szServiceData),
                      NULL);

  wsprintf(deviceRegStr, TEXT("Root\\LEGACY_%ws\\0000"), szServiceData);

  memset(&deviceInfoData, 0, sizeof(SP_DEVINFO_DATA));
  deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

  if (SetupDiOpenDeviceInfo(hDevInfo, deviceRegStr, NULL, 0, &deviceInfoData) //Check for existing
      ||(SetupDiCreateDeviceInfo(hDevInfo, deviceRegStr,                      //Create new
                                 (LPGUID) &GUID_DEVCLASS_DISPLAY,
                                 NULL, //Do we need a description here?
                                 NULL, //No user interface
                                 0,
                                 &deviceInfoData) &&
         SetupDiRegisterDeviceInfo(hDevInfo,
                                   &deviceInfoData,
                                   0,
                                   NULL,
                                   NULL,
                                   NULL)) )
    {
      /* We created a new key in the registry */

      memset(&DeviceInstallParams, 0,sizeof(SP_DEVINSTALL_PARAMS));
      DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);

      SetupDiGetDeviceInstallParams(hDevInfo,
                                    &deviceInfoData,
                                    &DeviceInstallParams);

      DeviceInstallParams.Flags |= DI_NOFILECOPY | //We already copied the files
        DI_DONOTCALLCONFIGMG |
        DI_ENUMSINGLEINF; //Use our INF file only

      /* Path to inf file */
      wsprintf(DeviceInstallParams.DriverPath,
               TEXT("%ws\\%ws"),
               szDriverDir, TEXT(VBOXGUEST_VIDEO_INF_NAME));

      SetupDiSetDeviceInstallParams(hDevInfo,
                                    &deviceInfoData,
                                    &DeviceInstallParams);


      if(!SetupDiBuildDriverInfoList(hDevInfo,
                                     &deviceInfoData,
                                     SPDIT_CLASSDRIVER))
        {
          closeAndDestroy(hDevInfo, hInf);
          return FALSE;
        }

      drvInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
      if (!SetupDiEnumDriverInfo(hDevInfo,
                                 &deviceInfoData,
                                 SPDIT_CLASSDRIVER,
                                 0,
                                 &drvInfoData))
        {
          closeAndDestroy(hDevInfo, hInf);
          return FALSE;
        }

      if(!SetupDiSetSelectedDriver(hDevInfo,
                                   &deviceInfoData,
                                   &drvInfoData))
        {
          closeAndDestroy(hDevInfo, hInf);
          return FALSE;
        }

      if(!SetupDiInstallDevice(hDevInfo,
                               &deviceInfoData))
        {
          closeAndDestroy(hDevInfo, hInf);
          return FALSE;
        }
    }

  /* Make sure the device is enabled */
  if (SetupDiGetDeviceRegistryProperty(hDevInfo,
                                       &deviceInfoData, SPDRP_CONFIGFLAGS,
                                       NULL, (LPBYTE) &configFlags,
                                       sizeof(DWORD),
                                       NULL)
      && (configFlags & CONFIGFLAG_DISABLED))
    {
      configFlags &= ~CONFIGFLAG_DISABLED;

      SetupDiSetDeviceRegistryProperty(hDevInfo,
                                       &deviceInfoData,
                                       SPDRP_CONFIGFLAGS,
                                       (LPBYTE) &configFlags,
                                       sizeof(DWORD));
    }

  wsprintf(regKeyName,
           TEXT("System\\CurrentControlSet\\Services\\%ws\\Device%d"),
           szServiceData, 0); //We only have one device

  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                     regKeyName,
                     0,
                     NULL,
                     REG_OPTION_NON_VOLATILE,
                     KEY_READ | KEY_WRITE,
                     NULL,
                     &hkey,
                     &disp) == ERROR_SUCCESS)
    {
      /* Insert description */
      RegSetValueEx(hkey,
                    TEXT("Device Description"),
                    0,
                    REG_SZ,
                    (LPBYTE) DriverInfoDetailData.DrvDescription,
                    (lstrlen(DriverInfoDetailData.DrvDescription) + 1) *
                    sizeof(TCHAR) );

      TCHAR szSoftwareSection[LINE_LEN];

      wsprintf(szSoftwareSection,
               TEXT("%ws.SoftwareSettings"),
               szServiceData);

      if (!SetupInstallFromInfSection(NULL,
                                      hInf,
                                      szSoftwareSection,
                                      SPINST_REGISTRY,
                                      hkey,
                                      NULL,
                                      0,
                                      NULL,
                                      NULL,
                                      NULL,
                                      NULL))
        {
          RegCloseKey(hkey);
          closeAndDestroy(hDevInfo, hInf);
          return FALSE;
        }

      RegCloseKey(hkey);
    }

  /* Install OpenGL stuff */
  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                     TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\OpenGLDrivers"),
                     0,
                     NULL,
                     REG_OPTION_NON_VOLATILE,
                     KEY_READ | KEY_WRITE,
                     NULL,
                     &hkey,
                     &disp) == ERROR_SUCCESS)
    {
      /* Do installation here if ever necessary. Currently there is no OpenGL stuff */

      RegCloseKey(hkey);
    }


  /* Cleanup */
  closeAndDestroy(hDevInfo, hInf);

#if 0
  /* If this key is inserted into the registry, windows will show the desktop
     applet on next boot. We decide in the installer if we want that so the code
     is disabled here. */
  /* Set registry keys so windows picks up the changes */
  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                     TEXT("SYSTEM\\CurrentControlSet\\Control\\GraphicsDrivers\\NewDisplay"),
                     0,
                     NULL,
                     REG_OPTION_NON_VOLATILE,
                     KEY_READ | KEY_WRITE,
                     NULL,
                     &hkey,
                     &disp) == ERROR_SUCCESS)
    {
      RegCloseKey(hkey);
    }
#endif

  /* We must reboot at some point */
  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                     TEXT("SYSTEM\\CurrentControlSet\\Control\\GraphicsDrivers\\RebootNecessary"),
                     0,
                     NULL,
                     REG_OPTION_NON_VOLATILE,
                     KEY_READ | KEY_WRITE,
                     NULL,
                     &hkey,
                     &disp) == ERROR_SUCCESS)
    {
      RegCloseKey(hkey);
    }

  return TRUE;
}
コード例 #4
0
ファイル: Preinst.cpp プロジェクト: csowter/winuac2
int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
    if (!InitializeOptions(lpCmdLine))
    {
        if (!OptionSilent)
        {
            MessageBox(NULL, "Please specify the correct parameters:\r\n" \
                       "/path <Full path to the INF>\r\n" \
                       "/path:relative <Relative path to the INF from the current directory>\r\n" \
                       "/ddinstall <DDInstall section name to be processed>\r\n" \
                       "/os <Expected OS version in the form of x.y where x is MajorVersion, y is Minor version, ie 5.0 for Windows 2000>\r\n" \
                       "/buildnumber <Expected OS build number, ie 2195 for Windows 2000>\r\n" \
                       "/sp <Expected OS service pack number>\r\n",
                       "Pre-Installer",
                       MB_OK);

        }

        return ERR_FAIL;
    }

    if ((OptionOsVersionCheck) || (OptionOsBuildNumberCheck) || (OptionOsServicePackCheck))
    {
        OSVERSIONINFO OsVersionInfo;
        OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

        if (GetVersionEx(&OsVersionInfo))
        {
            if (OptionOsVersionCheck)
            {
                CHAR Version[32];

                sprintf(Version, "%d.%d", OsVersionInfo.dwMajorVersion, OsVersionInfo.dwMinorVersion);

                if (_stricmp(Version, ExpectedOsVersion))
                {
                    if (!OptionSilent)
                    {
                        CHAR ErrorMsg[256];
                        sprintf(ErrorMsg, "OS version %s doesn't match the expected %s", Version, ExpectedOsVersion);

                        MessageBox(NULL, ErrorMsg, "Pre-Installer", MB_OK);
                    }

                    return ERR_FAIL;
                }
            }

            if (OptionOsBuildNumberCheck)
            {
                if (OsVersionInfo.dwBuildNumber != ExpectedOsBuildNumber)
                {
                    if (!OptionSilent)
                    {
                        CHAR ErrorMsg[256];
                        sprintf(ErrorMsg, "OS build number %d doesn't match the expected %d", OsVersionInfo.dwBuildNumber, ExpectedOsBuildNumber);

                        MessageBox(NULL, ErrorMsg, "Pre-Installer", MB_OK);
                    }

                    return ERR_FAIL;
                }
            }

            if (OptionOsServicePackCheck)
            {
                CHAR ServicePack[32];

                sprintf(ServicePack, "Service Pack %d", ExpectedOsServicePack);

                if (_stricmp(OsVersionInfo.szCSDVersion, ServicePack))
                {
                    if (!OptionSilent)
                    {
                        CHAR ErrorMsg[256];
                        sprintf(ErrorMsg, "OS %s doesn't match the expected %s", OsVersionInfo.szCSDVersion, ServicePack);

                        MessageBox(NULL, ErrorMsg, "Pre-Installer", MB_OK);
                    }

                    return ERR_FAIL;
                }
            }
        }
        else
        {
            return ERR_FAIL;
        }
    }

    HINF InfHandle = SetupOpenInfFile(InfPath, NULL, INF_STYLE_WIN4 , NULL);	// Get INF Handle

    if (InfHandle != INVALID_HANDLE_VALUE)
    {
        HSPFILEQ QueueHandle = SetupOpenFileQueue();

        if (QueueHandle != INVALID_HANDLE_VALUE)
        {
            SetupInstallFilesFromInfSection(InfHandle, NULL, QueueHandle, DDInstallSection, NULL, SP_COPY_FORCE_NEWER);
            //Copies the files

            PVOID Context = SetupInitDefaultQueueCallback(NULL);

            if (Context)
            {
                SetupCommitFileQueue(NULL, QueueHandle, FileCallback, Context);

                SetupTermDefaultQueueCallback(Context);
            }

            SetupCloseFileQueue(QueueHandle);
        }

        // Do not do the following as we do not want to install the drivers yet. When the device is turned on,
        // Windows PnP will take care of the rest of the installation process.
#if 0
        CHAR DDInstallServicesSection[MAX_PATH];

        strcpy(DDInstallServicesSection, DDInstallSection);
        strcat(DDInstallServicesSection, ".Services");

        SetupInstallServicesFromInfSection(InfHandle, DDInstallServicesSection, SPSVCINST_TAGTOFRONT);

        HKEY RegKey = SetupDiOpenClassRegKey((LPGUID)&CLSID_MEDIA, KEY_ALL_ACCESS);

        if (RegKey != INVALID_HANDLE_VALUE)
        {
            SetupInstallFromInfSection(NULL, InfHandle, DDInstallSection, SPINST_REGISTRY, RegKey, NULL, NULL,
                                       NULL, NULL, NULL, NULL);		// Does the AddReg, Del Reg Stuff

            RegCloseKey(RegKey);
        }
#endif // 0

        SetupCloseInfFile(InfHandle);
    }

    if (SetupCopyOEMInf(InfPath, NULL, SPOST_PATH, NULL, NULL ,0, NULL, NULL))
    {
        return SUCCESS;
    }
    else
    {
        DWORD err = GetLastError();

        return ERR_FAIL;
    }
}