int checkDiscovery() {
    if (freespace_private_discoveryStatusChanged()) {
        int rc;
        int totalChanges = 0;

        // Wait for system to stabilize before scanning.
        if (CMP_WaitNoPendingInstallEvents(0) == WAIT_TIMEOUT) {
            DEBUG_PRINTF("Pending install events.  Wait for resolution.\n");
            freespace_private_requestDeviceRescan();
            return FREESPACE_ERROR_BUSY;
        }

        DEBUG_WPRINTF(L"Scanning devices\n");

        // Mark and sweep the device list.
        freespace_private_filterDevices(NULL, 0, NULL, filterInitialize);

        // Mark everything that still exists and add new devices
        rc = freespace_private_scanAndAddDevices();
        if (rc != FREESPACE_SUCCESS) {
            // Unexpected error.  Schedule a rescan.
            DEBUG_WPRINTF(L"Error %d while scanning devices.  Request a rescan.\n", rc);
            freespace_private_requestDeviceRescan();
            return rc;
        }

        // Handle all changes.
        totalChanges += checkDiscoveryRemoveDevices();
        totalChanges += checkDiscoveryPartiallyRemovedDevices();
        totalChanges += checkDiscoveryAddedDevices();

        /*
         * Continue to schedule a rescan until no changes are detected.
         * Although this should not be necessary, rescanning until a
         * stable state is reached should increase robustness.
         */
        if (totalChanges != 0) {
            DEBUG_WPRINTF(L"Detected %d changes.  Schedule rescan\n", totalChanges);
            freespace_private_requestDeviceRescan();
        }
    }

    return freespace_private_discoveryGetThreadStatus();
}
int __cdecl
main(ULONG argc, PCHAR argv[])
{
  DWORD ret;
  DWORD timeout = INFINITE;

  if (argc == 2)
  {
    timeout = atoi(argv[1]);
  }

  //printf("timeout = %d\n", timeout);

  ret = CMP_WaitNoPendingInstallEvents(timeout);

  //printf("ret = %d\n", ret);

  return ret;
}
Exemplo n.º 3
0
int __cdecl	
_tmain( 
    int argc, 
    TCHAR* argv[] 
    )
{
    TCHAR DestPath[MAX_PATH];
    ULONG Size = MAX_PATH;
    TCHAR * InfPath = NULL;
	TCHAR DriverPackageInfPath[MAX_PATH];
    DWORD Ret;
    TCHAR Option;
    BOOL NeedRebootDev = FALSE;
    BOOL NeedRebootDrvPkg = FALSE;
    DWORD Flags = 0;  
    LPTSTR lpFilePart;
    int i;
    DWORD dw;
    Size = MAX_PATH;
    
    _tprintf( TEXT("Driver Installer Sample Program\n") ); 
    if( argc < 3 )
    {
        _tprintf( TEXT("USAGE: drvinst /i /c /r /g <inf-filename> [Flag(s)]\n") );
        _tprintf( TEXT("\nOptions:\n"));
        _tprintf( TEXT("/i : re-install driver package and devices if presented.\n") );
        _tprintf( TEXT("/u : uninstall devices and driver package.\n") );
        _tprintf( TEXT("/g : get installed driver package path.\n") );

        _tprintf( TEXT("\n<inf-filename> : path to the inf file.\n") );
        _tprintf( TEXT("\nFlags:\n"));
        _tprintf( TEXT("DRIVER_PACKAGE_REPAIR:                 0x00000001 ( 1)\n"));
        _tprintf( TEXT("DRIVER_PACKAGE_SILENT:                 0x00000002 ( 2)\n"));
        _tprintf( TEXT("DRIVER_PACKAGE_FORCE:                  0x00000004 ( 4)\n"));
        _tprintf( TEXT("DRIVER_PACKAGE_ONLY_IF_DEVICE_PRESENT: 0x00000008 ( 8)\n"));
        _tprintf( TEXT("DRIVER_PACKAGE_LEGACY:                 0x00000010 (16)\n"));
        _tprintf( TEXT("DRIVER_PACKAGE_DELETE_FILES:           0x00000020 (32)\n"));
        
        return 0;
    }

    //
    // Get command line choices
    //

    //
    // The code suppresses the warning 28193 for the call to _totlower.
    // This suppression is done because that function has a check return
    // annotation on it.  However, it doesn't return an error code
    // and the check return annotation is really being used to indicate
    // that the return value of the function should be looked at and/or 
    // assigned to a variable. The check return annotation means the return
    // value should always be checked in all code paths.  The return value is
    // assigned to a variable, but the return in the for loop could cause
    // this function to exit before Option is ever examined.  For that case,
    // the code doesn't need to check the value of Option so the warning
    // is being suppressed
    //
#pragma warning( suppress: 28193)
    Option = (TCHAR)_totlower(argv[1][1]);
	InfPath = argv[2];

    for ( i = 3; i<argc; i++ ){
        dw = _ttol( argv[i] );
        //
        // Either an error occurred during conversion of a flags parameter 
        // into a long or a flag of 0 was specified.  Either way,
        // this is invalid input.
        //
        if (0 == dw)
        {
            _tprintf( TEXT("An invalid flag was specified.") );
            
            return 0;
        }

        Flags |= dw;
    }


    GetFullPathName( InfPath, MAX_PATH, DriverPackageInfPath, &lpFilePart);  
    
    _tprintf( TEXT("INFO: Option \"/%c\"\n"), Option );    
    _tprintf( TEXT("INFO: Input file \"%s\"\n"), DriverPackageInfPath );
    _tprintf( TEXT("INFO: Flags 0x%X (%u)\n\n"), Flags, Flags );

    
    DIFXAPISetLogCallback( LogCallback, (PVOID)NULL );
    
    Ret = ERROR_SUCCESS;

    if ( TEXT('u') == Option )
    {
        _tprintf( TEXT("INFO: uninstalling devices.\n"));

        if ( TRUE == RemoveDevices( &NeedRebootDev ) ) {
            _tprintf( TEXT("SUCCESS: uninstalled devices.\n") );

        }
        else {
            _tprintf( TEXT("ERROR: failed.\n") );
            Ret = !ERROR_SUCCESS;
        }

        if ( ERROR_SUCCESS == Ret ) {
            _tprintf( TEXT("INFO: uninstalling driver package.\n"));    

            Ret = DriverPackageUninstall(  
                        DriverPackageInfPath,
                        Flags,
                        NULL,
                        &NeedRebootDrvPkg);

            if( ERROR_SUCCESS == Ret ){
                _tprintf( TEXT("SUCCESS: uninstalled driver package %s.\n"), DriverPackageInfPath );
            } else {
                _tprintf( TEXT("ERROR: failed with error code 0x%X\n"), Ret );
            }
        
            if ( NeedRebootDev || NeedRebootDrvPkg ) {
                _tprintf( TEXT("INFO: Machine will have to be rebooted to complete uninstall.") );
            }
        }
    }
    else if ( TEXT('i') == Option )
    {
        _tprintf( TEXT("DRVINST: installing drivers.\n"));
        
        /* http://msdn.microsoft.com/en-us/library/windows/hardware/ff552293(v=vs.85).aspx
           
        */
        
        // step1 - Checking for In-Progress Installations
        if ( WAIT_OBJECT_0 != CMP_WaitNoPendingInstallEvents ( 0 ) ) {
        	_tprintf( TEXT("ERROR: There are pending installation activities or some error happened when checking CMP_WaitNoPendingInstallEvents\n") );
					goto final_main;
        }
        
        /*
           step2 - Determine whether a device is plugged in.
           step3 - Preinstall driver packages
        	 If we use DIFx API, will save our time, otherwise we must follow the MSDN regarding step2 and step3
        	 
        */
        Ret = DriverPackageInstall( 
                    DriverPackageInfPath, 
                    Flags,
                    NULL,
                    &NeedRebootDrvPkg);

        if( ERROR_SUCCESS == Ret ){
            _tprintf( TEXT("SUCCESS: installed package %s.\n"), DriverPackageInfPath );
        } else if (ERROR_NO_MORE_ITEMS == Ret){
            _tprintf( TEXT("INFO: All devices found already have a better driver than what is contained in the specified inf file. To force an install, use the ForceIfNotBetter flag.\n"));
        } else if (ERROR_NO_SUCH_DEVINST == Ret){
            _tprintf( TEXT("INFO: There aren't any live devnodes with the DeviceId contained in the INF.\n"));
        }
        else{
            _tprintf( TEXT("ERROR: failed with error code 0x%X\n"), Ret );
        }

        if (NeedRebootDrvPkg){
            _tprintf( TEXT("INFO: Machine will have to be rebooted to complete install.") );
        }
    }
    else if ( TEXT('g') == Option )
    {  
        _tprintf( TEXT("TEST: getting driver package path.\n")); 
        Ret = DriverPackageGetPath( DriverPackageInfPath, DestPath, &Size );
        if ( ERROR_SUCCESS==Ret ){
            _tprintf( TEXT("INFO: INF path is %s\n"), DestPath );    
        } else {
            _tprintf( TEXT("ERROR: failed with error code 0x%X\n"), Ret );
        }
    } else {
        _tprintf( TEXT("ERROR: invalid command line option /%c.\n"), Option );
    }

final_main:
    DIFXAPISetLogCallback( NULL, (PVOID)NULL );
    
    return 1;
}
Exemplo n.º 4
0
static BOOL
CommonInstall(VOID)
{
    HWND hWnd = NULL;

    hSysSetupInf = SetupOpenInfFileW(
        L"syssetup.inf",
        NULL,
        INF_STYLE_WIN4,
        NULL);
    if (hSysSetupInf == INVALID_HANDLE_VALUE)
    {
        FatalError("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
        return FALSE;
    }

    if (!InstallSysSetupInfDevices())
    {
        FatalError("InstallSysSetupInfDevices() failed!\n");
        goto error;
    }

    if(!InstallSysSetupInfComponents())
    {
        FatalError("InstallSysSetupInfComponents() failed!\n");
        goto error;
    }

    if (!IsConsoleBoot())
    {
        HANDLE hThread;

        hThread = CreateThread(
            NULL,
            0,
            ShowStatusMessageThread,
            (LPVOID)&hWnd,
            0,
            NULL);

        if (hThread)
            CloseHandle(hThread);
    }

    if (!EnableUserModePnpManager())
    {
        FatalError("EnableUserModePnpManager() failed!\n");
        goto error;
    }

    if (CMP_WaitNoPendingInstallEvents(INFINITE) != WAIT_OBJECT_0)
    {
        FatalError("CMP_WaitNoPendingInstallEvents() failed!\n");
        goto error;
    }

    EndDialog(hWnd, 0);
    return TRUE;

error:
    if (hWnd)
        EndDialog(hWnd, 0);
    SetupCloseInfFile(hSysSetupInf);
    return FALSE;
}