Search code examples
c#comwindows-update

Type.GetTypeFromProgID() returns null, but New-Object works in Powershell


I'm trying to create an instance of the COM object Microsoft.Update.AutoUpdate in C#. This works in PowerShell:

$AutoUpdates = New-Object -ComObject "Microsoft.Update.AutoUpdate"

However, this doesn't work in C#:

var autoUpdateClass = Type.GetTypeFromProgID("Microsoft.Update.Auto­Update");
// var autoup = Activator.CreateInstance(autoUpdateClass) as IAutomaticUpdates;

In this case, autoUpdateClass is null. Is there any way to activate an instance of this class? Maybe I have to manually specify the DLL, or ensure the correct bitness of my process?

A working alternative would be to add a reference to WUApiLib.dll, and setting "Embed Interop Types" to false. Then I can just do new AutoUpdateClass(). The drawback is that I then have an additional Interop.WUApiLib.dll, and also people recommend to "never do this". Any way, I'd like to know why GetTypeFromProgID fails here.


Solution

  • Because you have a special unicode character between "Auto" and "Update" that you probably don't have in your powershell script, if you paste your code into notepad it will display this:

    enter image description here

    PS: more and more people have these kind of hidden char issues these days, not sure why...