1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
int PatchUninstallFunc( char * chUpdateID )
{
try
{
IUpdateSession *lpUpdateSession = NULL;
IUpdateServiceManager *lpUpdateServiceManager = NULL;
IUpdateService *lpUpdateService = NULL;
IUpdateSearcher *lpUpdateSearcher = NULL;
ISearchResult *lpSearchResult = NULL;
IUpdateCollection *lpUpdateCollection = NULL;
IUpdate *lpUpdate = NULL;
IUpdateIdentity *lpIdentity = NULL;
IUpdateCollection *lpUninstCollection = NULL;
IUpdateInstaller *lpUpdateInstaller = NULL;
IInstallationResult *lpResult = NULL;
HRESULT hResult = NULL;
//1. Initialize COM.
CoInitialize( NULL );
//2. CreateInstance of IUpdateSession.
if( CoCreateInstance( CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID *)&lpUpdateSession ) != S_OK )
{
printf("Error\a" );
return -1;
}
lpUpdateSession->put_ClientApplicationID( bstr_t("TestID") );
//3. Create Instance of IUpdateServiceManager.
if( CoCreateInstance( CLSID_UpdateServiceManager, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateServiceManager,
(LPVOID *)&lpUpdateServiceManager ) != S_OK )
{
printf("Error\a" );
return -1;
}
char chCABPath[MIN_BUFF] = "c:\\Test\\wsusscn2.cab";
_bstr_t bstrCabLocation( chCABPath );
//4. Register cab file with IUpdateServiceManager - Offline Sync Service - Returns pointer to IUpdateService Interface.
printf( "AddScanPackageService...\n" );
if( lpUpdateServiceManager->AddScanPackageService( CComBSTR( "Offline Sync Service" ), bstrCabLocation, 0, &lpUpdateService ) != S_OK )
{
printf("Error\a" );
return -1;
}
SysFreeString( bstrCabLocation );
//5. Create UpdateSearcher.
if( lpUpdateSession->CreateUpdateSearcher( &lpUpdateSearcher ) != S_OK )
{
printf("Error\a" );
return -1;
}
//6. Fill details required for UpdateSearcher.
BSTR temp;
lpUpdateService->get_ServiceID( &temp );
lpUpdateSearcher->put_ServiceID( temp );
lpUpdateSearcher->put_ClientApplicationID( bstr_t("TestID" ) );
lpUpdateSearcher->put_ServerSelection( ssOthers );
//7. Define Search Criteria.
char chCriteria[MIN_BUFF] = "";
_snprintf( chCriteria, sizeof(chCriteria) - 1, " Type = 'Software' and UpdateID = '%s'", chUpdateID );
_bstr_t bstrCriteria ( chCriteria );
//8. Start Search.
printf( "Search...\n" );
if ( lpUpdateSearcher->Search( bstrCriteria, &lpSearchResult ) != S_OK )
{
printf("Error\a" );
return -1;
}
SysFreeString( bstrCriteria );
VARIANT_BOOL varBool = VARIANT_FALSE;
char chTempUID[MIN_BUFF] = "";
long lCount = 0;
BSTR bstrUpdateID;
//9. Get the Search Result.
lpSearchResult->get_Updates( &lpUpdateCollection );
lpUpdateCollection->get_Count( &lCount);
if( lCount <= 0 )
{
printf("No update installed with this UpdateID\a" );
return -1;
}
lpUpdateCollection->get_Item( 0, &lpUpdate );
lpUpdate->get_IsInstalled( &varBool );
if( varBool == VARIANT_TRUE )
{
printf( "Installed" );
lpUpdate->get_IsUninstallable( &varBool );
if( varBool == VARIANT_TRUE )
{
printf("Uninstallable\n" );
}
else
{
printf("Not Uninstallable\n" );
}
lpUpdate->get_Identity( &lpIdentity );
lpIdentity->get_UpdateID( &bstrUpdateID );
if( bstrUpdateID != NULL )
{
strcpy( chTempUID, (const char *)bstr_t(bstrUpdateID) );
}
if( stricmp( chUpdateID, chTempUID ) != 0 )
{
printf("Error\a" );
return -1;
}
//10. CreateInstance for UpdateCollection.
if( CoCreateInstance( CLSID_UpdateCollection, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateCollection, (LPVOID *)&lpUninstCollection ) != S_OK )
{
printf("Error\a" );
return -1;
}
long lReturn = 0;
lpUninstCollection->Add( lpUpdate, &lReturn );
//11. Create UpdateInstaller.
if( lpUpdateSession->CreateUpdateInstaller( &lpUpdateInstaller ) != S_OK )
{
printf("Error\a" );
return -1;
}
lpUpdateInstaller->put_ClientApplicationID( bstr_t( "TestID" ) );
lpUpdateInstaller->put_IsForced( VARIANT_TRUE );
lpUpdateInstaller->put_AllowSourcePrompts( VARIANT_FALSE );
if( lpUpdate->AcceptEula() != S_OK )
{
printf("Error\a" );
return -1;
}
if( lpUpdateInstaller->put_Updates( lpUninstCollection ) != S_OK )
{
printf("Error\a" );
return -1;
}
//12. Proceed with uninstall.
hResult = lpUpdateInstaller->Uninstall( &lpResult );
/***************************************
*Above function return error 0x80240028
***************************************/
if( hResult != S_OK )
{
printf("Error\a" );
return -1;
}
}
else
{
printf( "Patch not yet installed.\a" );
return -1;
}
}
catch( ... )
{
printf("Exception.\a");
return -1;
}
return 0;
}
|