Windows patch Uninstallation using WUA API

I tried uninstallation of patch using WUA API

IUpdateInstaller::Uninstall Method( Following link describes it )

http://msdn.microsoft.com/en-us/library/windows/desktop/aa386510%28v=vs.85%29.aspx

But i am getting this error code : 0x80240028

Which means :

WU_E_UNINSTALL_NOT_ALLOWED The update could not be uninstalled because the request did not originate from a WSUS server.

I am not using WSUS.

Scan is done with wsusscn2.cab file

And manually deployed the patch using downloaded patch file.(Offline).

Code is:

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;
}


I am passing UpdateID of the patch as Input to this function.

I didnt find solution for this problem.

I hope i can get a solution here.

Thanks in Advance.
There are a few things that you are doing that raise some questions for me. You're not setting up your char array's correctly for instance, either they are constant or they should be a set size. You seem to be trying to do both. I'm not sure where 'MIN_BUF' is declared or what the value of it is, but if I had to guess I would say that it is not large enough to hold some of the data you want to store in it.

Now this may not even be your issue, it's certainly not good and it WILL cause problems for you in the future, but the actual error you are seeing means something else. That error actually means that you cannot perform the uninstall because the original installation did not originate from WSUS. Where did you get this patch file? There might be an uninstaller for it.
Last edited on
My apologies. MIN_BUFF value is 1024. I have declared it in macro.
I hope 1024 is large enough to hold the file path.

I am doing search using Same API.
lpUpdateSearcher->Search( bstrCriteria, &lpSearchResult );

After completing search, i am storing patch informations in DB.

From that i am using DownloadURL to download the patch manually from some other machine. (Since my machine is not having internet connectivity).

After download it, Am placing it in

C:\Test\[UpdateID]\


I already mentioned that am not Using WSUS. Am using Windows Update Agent downloaded from following location

download.windowsupdate.com/WindowsUpdate/redist/standalone/7.4.7600.226/WindowsUpdateAgent30-x86.exe



Scan is happening fine. Installation is happening fine. But Uninstallation gives this error.

Is it mandatory that WUA Api can uninstall patch only installed using WSUS???
My one more confusion is,

For scanning, we need Cab file.

For installation, we need Cab file and Patch File.


Similarly for Uninstallation, Cab file and (Patch File is required or not)?

Here am struggling.

After installing a patch, whose uninstaller is located in following path

C:\Windows\$NtUninstall[kbarticleID]$\spuninst\spuninst.exe

but i need to do it using WUA Api.

I dont wanna do with Commandline.
Is it mandatory that WUA Api can uninstall patch only installed using WSUS???

I hate to say it, but I think it might be. I've personally installed thousands of Windows updates but I'll admit that I don't have a whole lot of experience removing them, it's just something that doesn't come up for me. What makes me think that you need to use the uninstaller manually is that A.) I'm certain that if that component exists in the MSI that it will work, and B.) your issue has a lot in common with this post here: http://blogs.technet.com/b/heyscriptingguy/archive/2009/03/12/how-can-i-remove-a-microsoft-update.aspx which is basically using the same mechanism that you are but through VBS instead of COM+.

Maybe the Windows Shadow Copy service would be a more workable approach for you? http://technet.microsoft.com/en-us/library/ee923636(v=ws.10).aspx
Last edited on
Thank you so much for your reply.
I have spent lot of time for this thing.
This is a good experience.
I will try some other way to uninstall patch.
Topic archived. No new replies allowed.