Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Update > Problem understanding/using COM WAU API from C#[error:bad variabletype]

Reply
Thread Tools Display Modes

Problem understanding/using COM WAU API from C#[error:bad variabletype]

 
 
mabra
Guest
Posts: n/a

 
      10-29-2006
Hi All !

Sorry for my crossposting, I am not completely sure about which group is
really of any help. I started using the WUA [windows update agent] COM
API from C#. I created an interop assembly and my code compiles fine and
the most parts are running as expected.

I first create a UpdateSession object and look for updates, which are
downloaded. Then I create an UpdateInstaller, but this fails:

UpdateInstaller installer = (UpdateInstaller)
this.session.CreateUpdateInstaller(); //CRASH

I might have a wrong variable type here really. If I use VS's object
browser, I feel, I do not understand, why there are so many different
interfaces, like IUpdateInstaller, IUpdateInstaller2 etc.


I tried also:

IUpdateInstaller2 installer = (IUpdateInstaller2)
this.session.CreateUpdateInstaller(); //CRASH

May be, someone can help?

Thanks so far and
best regards,
Manfred

I append a repro here [hopefully, this is readable]

/*

Name: DemoComError.cs
Compile: csc /nologo /debug:full /t:exe /outemoComError.exe
/r:Interop.WUAApiLib.dll /r:log4net.dll DemoComError.cs
TLBIMP: TLBIMP /out:Interop.WUAApiLib.dll /namespace:Interop.WUALib
C:\WINDOWS\system32\wuapi.dll
TlbImp warning: At least one of the arguments for
'UpdateInstaller.get_ParentHwnd' can not be marshaled by the runtime
marshaler. Such arguments will therefore be passed as a pointer and
may require unsafe code to manipulate.
Error:
Unhandled Exception: System.Runtime.InteropServices.COMException
(0x80020008): Bad variable type.
at Interop.WUALib.IUpdateSession2.CreateUpdateInstall er()
at AdminTools.WindowsUpdateInfo.InstallUpdates() in
d:\Develop\Dev\Experimente\WSUS\Net\WUAWrapper\Dem oComError.cs:line 87
*/


using System;
using System.Runtime.InteropServices;
using Interop.WUALib;


namespace AdminTools
{

public class WUAApp
{
public static void Main(string[] args)
{
if(args.Length != 0)
{
WindowsUpdateInfo wui = new WindowsUpdateInfo(args[0]);
int count = wui.DetectUpdates(); //condition...
if(count != 0) wui.InstallUpdates();
}
else
{
Console.WriteLine("Args!!! [p1=computer]");
}
}
}//class

public class WindowsUpdateInfo
{
private string computerName;
private UpdateSession session;
private UpdateCollection updates;

public WindowsUpdateInfo(string computerName)
{
this.computerName = computerName;

try
{
Type type = Type.GetTypeFromProgID("Microsoft.Update.Session",
computerName, true);
this.session = (UpdateSession) Activator.CreateInstance(type);
}
catch(COMException ce)
{
Console.WriteLine("COMException;Exception:{0},Code :{1}", ce.Message,
ce.ErrorCode);
}
}

public int DetectUpdates()
{
IUpdate u;

IUpdateSearcher us = session.CreateUpdateSearcher();
ISearchResult sr = us.Search("IsInstalled=0 and Type='Software'");
this.updates = sr.Updates;
for(int i = 0; i < this.updates.Count; i++)
{
u = this.updates[i]; //do something with update;show.
}
Console.WriteLine("Updates:{0}", this.updates.Count);

return this.updates.Count;
}

public void InstallUpdates()
{
UpdateCollection updates = new UpdateCollection();
for(int i = 0; i < this.updates.Count; i++)
{
updates.Add(this.updates[i]);
}

Console.WriteLine("Added {0} updates to install.", updates.Count);

UpdateInstaller installer = (UpdateInstaller)
this.session.CreateUpdateInstaller(); //(87)CRASH
installer.Updates = updates;
IInstallationResult installationResult = installer.Install();

for(int i = 0; i < updates.Count; i++)
{
Console.WriteLine("Installed:{0},Result:{1}.", updates[i].Title,
installationResult.GetUpdateResult(i).ResultCode);
}

}

}//class

}//namespace
 
Reply With Quote
 
 
 
 
nop
Guest
Posts: n/a

 
      08-29-2007
Worked part of my code for using WUA (C#), may be help you:

UpdateSessionClass updateSession = new UpdateSessionClass();
UpdateServiceManagerClass updateServiceManager = new
UpdateServiceManagerClass();
IUpdateServiceManager updateServiceManager =
updateSession.CreateUpdateServiceManager();
IUpdateService updateService =
updateServiceManager.AddScanPackageService("Offlin e Sync Service",
"WSUSSCAN.cab", 0);
IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher();
updateSearcher.ServerSelection = ServerSelection.ssOthers
updateSearcher.ServiceID = updateService.ServiceID;
ISearchResult searchResult = updateSearcher.Search("Type='Software'");
....
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UAC problem - Need a little help and a lot of understanding Sid Windows Vista Security 4 10-25-2007 08:12 PM
Understanding NdisMAllocateMapRegisters suu-n-soup Windows Vista Drivers 1 02-01-2006 07:08 PM
Understanding the MSplotter leojose Windows Vista Drivers 0 05-24-2005 08:32 AM
help me with my understanding of WSPRecv/From Harry Potter Windows Vista Drivers 4 09-10-2004 07:05 PM
Re: Understanding the protocol Farooque Khan Windows Vista Drivers 0 09-10-2003 06:42 AM



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