Contents

今天的The Old New Thing: How can I tell if Windows Update is waiting for the system to reboot?介绍了如何检查机器是否因为装了Windows更新而需要重新启动。下面是稍作修改的CPP和C#的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <atlstr.h>
#include <wuapi.h>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
{
CComPtr<ISystemInformation> info;
info.CoCreateInstance(CLSID_SystemInformation);

VARIANT_BOOL rebootRequired;
info->get_RebootRequired(&rebootRequired);

std::cout << rebootRequired;
}
CoUninitialize();
return 0;
}
1
2
3
4
Type t = Type.GetTypeFromProgID("Microsoft.Update.SystemInfo");
object s = Activator.CreateInstance(t);
var needReboot = t.InvokeMember("RebootRequired", BindingFlags.GetProperty, null, s, null);
Console.WriteLine(needReboot);
Contents