一、得到OS版本信息
参考文章:https://www.cnblogs.com/VOIP/archive/2011/03/22/1990927.html
常用两个函数GetVersion和GetVersionEx。
GetVersion这个函数曾经困扰了很多程序员,其本来设计的是在DWORD返回值中用低位的那个字表示MS-DOS的版本号,高位的那个字表示Windows版本号。对于每个字来说,高位字节代表主要版本号,低位字节代表次要版本号。可是因为编写此函数的程序员犯了一个错误,使得此函数返回的Windows版本号颠倒了(即把主要版本号放到了低位字节,而次要版本号放到了高位字节)。当发现此错误的时候已经有很多程序员在使用这个函数了,Microsoft只好将错就错,直接把原来的API文档给改了过来。
为了解决GetVersion带来的问题,Microsoft后来开发了一个新的函数GetVersionEx,用它能够得到更详细的Windows系统的版本信息,下面我就写写GetVersionEx的使用方法。
函数原型:
BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);
我们先来看看OSVERSIONINFOEX这个结构:
typedef struct { DWORD dwOSVersionInfoSize; //在使用GetVersionEx之前要将此初始化为结构的大小 DWORD dwMajorVersion; //系统主版本号 DWORD dwMinorVersion; //系统次版本号 DWORD dwBuildNumber; //系统构建号 DWORD dwPlatformId; //系统支持的平台(详见附1) TCHAR szCSDVersion[128]; //系统补丁包的名称 WORD wServicePackMajor; //系统补丁包的主版本 WORD wServicePackMinor; //系统补丁包的次版本 WORD wSuiteMask; //标识系统上的程序组(详见附2) BYTE wProductType; //标识系统类型(详见附3) BYTE wReserved; //保留,未使用 } OSVERSIONINFOEX, *POSVERSIONINFOEX;
这个结构在Windows 2000后出现,老版本的OSVERSIONINFO结构没有wServicePackMajor、wServicePackMinor、wSuiteMask、wProductType和wReserved这几个成员。
一个关于他的简单示例:
OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); printf("OS Version: %d ", osvi.dwMajorVersion);
但是,这个函数有局限性,只适用于Win8.1以下的版本,如果是Win10就需要使用另外的方法。
判断版本信息如下:
#include <Windows.h> #include <string> #include <stdio.h> void getSystemName() { std::string vname; //先判断是否为win8.1或win10 typedef void(__stdcall*NTPROC)(DWORD*, DWORD*, DWORD*); HINSTANCE hinst = LoadLibrary("ntdll.dll"); DWORD dwMajor, dwMinor, dwBuildNumber; NTPROC proc = (NTPROC)GetProcAddress(hinst, "RtlGetNtVersionNumbers"); proc(&dwMajor, &dwMinor, &dwBuildNumber); if (dwMajor == 6 && dwMinor == 3) //win 8.1 { vname = "Microsoft Windows 8.1"; printf_s("此电脑的版本为:%s ", vname.c_str()); return; } if (dwMajor == 10 && dwMinor == 0) //win 10 { vname = "Microsoft Windows 10"; printf_s("此电脑的版本为:%s ", vname.c_str()); return; } //下面判断不能Win Server,因为本人还未有这种系统的机子,暂时不给出 //判断win8.1以下的版本 SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器 GetSystemInfo(&info); //调用GetSystemInfo函数填充结构 OSVERSIONINFOEX os; os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); #pragma warning(disable:4996) if (GetVersionEx((OSVERSIONINFO *)&os)) { //下面根据版本信息判断操作系统名称 switch (os.dwMajorVersion) { //判断主版本号 case 4: switch (os.dwMinorVersion) { //判断次版本号 case 0: if (os.dwPlatformId == VER_PLATFORM_WIN32_NT) vname ="Microsoft Windows NT 4.0"; //1996年7月发布 else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) vname = "Microsoft Windows 95"; break; case 10: vname ="Microsoft Windows 98"; break; case 90: vname = "Microsoft Windows Me"; break; } break; case 5: switch (os.dwMinorVersion) { //再比较dwMinorVersion的值 case 0: vname = "Microsoft Windows 2000"; //1999年12月发布 break; case 1: vname = "Microsoft Windows XP"; //2001年8月发布 break; case 2: if (os.wProductType == VER_NT_WORKSTATION && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) vname = "Microsoft Windows XP Professional x64 Edition"; else if (GetSystemMetrics(SM_SERVERR2) == 0) vname = "Microsoft Windows Server 2003"; //2003年3月发布 else if (GetSystemMetrics(SM_SERVERR2) != 0) vname = "Microsoft Windows Server 2003 R2"; break; } break; case 6: switch (os.dwMinorVersion) { case 0: if (os.wProductType == VER_NT_WORKSTATION) vname = "Microsoft Windows Vista"; else vname = "Microsoft Windows Server 2008"; //服务器版本 break; case 1: if (os.wProductType == VER_NT_WORKSTATION) vname = "Microsoft Windows 7"; else vname = "Microsoft Windows Server 2008 R2"; break; case 2: if (os.wProductType == VER_NT_WORKSTATION) vname = "Microsoft Windows 8"; else vname = "Microsoft Windows Server 2012"; break; } break; default: vname = "未知操作系统"; } printf_s("此电脑的版本为:%s ", vname.c_str()); } else printf_s("版本获取失败 "); } void main() { getSystemName(); system("pause"); }
可以得到正确的结果,包括Win10。
二、获取wlan自动配置服务状态
wlan自动配置服务(Wlan Autoconfig)是微软 Windows Vista以上版本操作系统的无线网卡配置服务。替代并增强了Windows XP/Windows server 2003中的Windows零配置服务(Wireless zero configuration)。
wlan自动配置服务的servicename是Wlansvc,Windows零配置服务的servicename是WZCSVC。服务状态三步走:OpenSCManager --> OpenService --> QueryServiceStatus。
服务器状态对应的值为
后续可以根据服务的状态进行相应的操作。
随便参考一个Windows服务运行状态的例子:
#include <iostream> #include <tchar.h> #include <Windows.h> using namespace std; /* 检查Windows服务状态信息 使用API: OpenSCManager OpenService QueryServiceStatusEx */ int main(void) { TCHAR szSvcName[] = _T("HistorySvr"); SC_HANDLE schSCManager = NULL; SC_HANDLE schService = NULL; SERVICE_STATUS_PROCESS ssStatus; DWORD dwOldCheckPoint = 0; DWORD dwStartTickCount = 0; DWORD dwWaitTime = 0; DWORD dwBytesNeeded = 0; // Get a handle to the SCM database. schSCManager = OpenSCManager( NULL, // local computer NULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) { printf("OpenSCManager failed (%d) ", GetLastError()); } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS); // full access if (schService == NULL) { printf("OpenService failed (%d) ", GetLastError()); CloseServiceHandle(schSCManager); } // Check the status in case the service is not stopped. if (!QueryServiceStatusEx( schService, // handle to service SC_STATUS_PROCESS_INFO, // information level (LPBYTE) &ssStatus, // address of structure sizeof(SERVICE_STATUS_PROCESS), // size of structure &dwBytesNeeded ) ) // size needed if buffer is too small { printf("QueryServiceStatusEx failed (%d) ", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); } else { // Check if the service is already running. It would be possible // to stop the service here, but for simplicity this example just returns. printf("Service status: "); switch(ssStatus.dwCurrentState) { case SERVICE_STOPPED: case SERVICE_STOP_PENDING: printf("Stop"); break; case SERVICE_PAUSED: case SERVICE_PAUSE_PENDING: printf("Pause"); break; case SERVICE_CONTINUE_PENDING: case SERVICE_RUNNING: case SERVICE_START_PENDING: printf("Running"); break; } cout << endl; } cin.get(); return 0; }
OpenSCManager最后一个参数代表的意义:
三、SetupDi系列函数
函数介绍:
1、SetupDiClassGuidsFromName
2、SetupDiGetClassDevs
3、SetupDiEnumDeviceInfo
汇总:
SetupDi 设备接口函数:
SetupDiCreateDeviceInterface 为一个设备注册设备功能(Registers device functionality(a device interface) for a device)。 SetupDiOpenDeviceInterface 重新获得一个已经存在的设备接口的信息,并且把它添加到指定的device information set中。 SetupDiGetDeviceInterfaceAlias 返回指定设备接口的别名 SetupDiGetClassDevs 返回一个设备信息集合,这个集合包含了指定class的设备的信息。 SetupDiGetClassDevsEx 返回一个设备信息集合,这个集合包含了指定本地的或者远程计算机的class设备的信息。 SetupDiEnumDeviceInterfaces 返回device information集合一个设备接口元素的一个内容结构。每次调用都返回一个设备接口的信息。 SetupDiGetDeviceInterfaceDetail 返回一个特定设备接口的详细信息 SetupDiCreateDeviceInterfaceRegKey 创建一个注册表子键来存储一些关于一个设备接口实例的信息,并且返回这个键的句柄。 SetupDiOpenDeviceInterfaceRegKey 指定一个设备接口实例,然后返回其注册表子键的句柄。 SetupDiDeleteDeviceInterfaceRegKey 删除一个指定设备接口实例的注册表子键。 SetupDiInstallDeviceInterfaces 这是DIF_INSTALLINTERFACES请求的默认处理函数。他安装INF文件DDInstall.Interfaces域列出的那些设备接口。 SetupDiRemoveDeviceInterface 从系统中删除一个注册了的设备接口。 SetupDiDeleteDeviceInterfaceData 从一个设备信息集合里面伤处一个设备接口。 SetupDiSetDeviceInterfaceDefault 为一个device calss设置一个指定的设备接口为默认接口。 SetupDiInstallClassEx 安装一个class installer或者一个借口class。 SetupDiOpenClassRegKeyEx 打开设备安装class注册表键,设备接口class注册表键,或者一个指定的class子键。这个函数能打开本地机器或者远程机器里的指定注册表键。
SetupDi设备属性函数(windwos vista和后续版本):
SetupDIGetClassProperty 重新获得一个device setup class或者一个device interface class的设备属性。 SetupDiGetClassPropertyEx 可用于本地或远程计算机 SetupDIGetClassPropertyKeys 获得一列设备property keys,这些keys代表了一个device setup class或者一个device interface class的设备属性。 SetupDiGetClassPropertyKeysEx 可用于本地或远程计算机。 SetupDiGetDeviceInterfaceProperty 获得一个设备接口的设备属性。 SetupDiGetDeviceINterfacePropertyKeys 获得一列设备属性keys,这些keys代表一个设备接口的属性。 SetupDiGetDeviceProperty 获得一个设备实例属性 SetupDiGetDevicePropertyKeys ……………… SetupDISetClassProperty 为一个设备安装class或一个设备接口class设置class属性 SetupDiSetClassPropertyEx 本地远程计算机 SetupDiSetDeviceInterfaceProperty 为一个设备接口设置设备属性 SetupDiSetDeviceProperty 为一个设备实例设置设备属性
驱动安装函数:
DiInstallDevice 安装一个指定的驱动,这个驱动之前在driver store里已经安装过。而且一个PnP设备目前在系统里面(vista和以后版本系统) DiInstallDriver 在driver store里预安装一个驱动,和在匹配一个PnP设备的时候安装这个驱动。 DiRollbackDriver 回滚一个已经安装在一个指定设备的驱动到为这个设备设置的后备驱动 DiShowUpdateDevice 显示一个指定设备的硬件升级向导(vista和后续版本) InstallSelectedDriver 在一个选定的设备上安装一个指定的驱动 UpdateDriverForPlugAndPlayDevices 更新已经安装的功能驱动,这个功能驱动用来匹配系统中的PnP设备。
SetupDi设备信息函数:
SetupDiCreateDeviceInfoList 创建一个空的device information set。这个集合可以被用来联系一个class GUID。 SetupDiCreateDeviceInfoListEx 本地远程计算机 SetupDiCreateDeviceInfo 创建一个新的设备信息元素,并把它加入到一个指定的设备信息集合中。 SetupDiOpenDeviceInfo 获取一个设备实例的信息,并把它加入到指定的设备信息集合中。 SetupDiEnumDeviceInfo 返回一个设备信息集合里面的信息元素。 SetupDiGetDeviceInstanceId 获取设备实例ID,这个ID与一个设备信息元素联合着。 SetupDiGetDeviceInfoListClass 获取一个class GUID的设备信息集合。 SetupDiGetDeviceInfoListDetail Retrieves information associated with a device information set including the class GUID, remote machine handle, and remote machine name.获取设备信息集合,里面包括了class GUID 远程机器handle,和远程机器名字 SetupDiGetClassDevPropertySheets 获取一个指定的设备信息元素的属性单句柄,或者指定设备信息集合的设备安装class的属性单句柄 SetupDiGetClassDevs 获取一个包含有指定的class的所有设备信息集合 SetupDiGetClassDevsEx 本地或者远程机器 SetupDiSetSelectedDevice 设置一个指定的设备信息元素成为一个设备信息集合的选定成员。这个函数一般在安装向导使用 SetupDiGetSelectedDevice 获取指定设备信息集合里目前选择的设备 SetupDiRegisterDeviceInfo 与PnP管理器注册一个信息设备实例 SetupDiDeleteDeviceInfo 从一个指定的设备信息集合中删除一个元素。这个函数不删除真正的设备 SetupDiDestroyDeviceInfoList 删除一个设备信息集合并清理所有内存
SetupDI驱动信息函数:
SetupDiBuildDriverInfoList 建立一列与制定设备实例有联系的驱动,或者设备信息集合的全局class驱动列表 SetupDiEnumDriverInfo 枚举一个驱动信息列表中的成员 SetupDiGetDriverInfoDetail 获取一个是定的驱动信息元素的详细信息 SetuoDiSetSelectedDriver 设置一个指定的驱动列表成员成为当前选定的驱动。这个函数可以用来冲洗设置驱动列表以至于没有当前选定驱动 SetupDiGetSelectedDriver 获取驱动列表中已经被选中的驱动 SetupDiCancelDriverInfoSearch 取消一个驱动列表查询,这个查询目前不在同一个线程中运行 SetupDiDestroyDriverInfoList 消灭一个驱动信息列表
SetupDi驱动选择函数|:
SetupDiAskForOEMDisk 显示一个对话框,来让用户选择一个OEM安装磁盘的路径 SetupDiSelectOEMDrv 用用户提供的一个OEM路径来为一个设备选择一个驱动 SetupDiSelectDevice 为一个DIF_SELECTDEVICE请求的默认句柄 另附msdn setupdi***系列函数说明 http://msdn.microsoft.com/en-us/library/ff551120(v=vs.85).aspx 若有提示SetupDi****无法解析的外部符号则要加setupapi.h setupapi.lib 这些可以在下面的链接下载。http://download.csdn.net/detail/gaofeidongdong/4157679
函数应用举例:https://blog.csdn.net/thanklife/article/details/76049588
1394 Host Bus Controller Class = 1394 ClassGuid = {6bdd1fc1-810f-11d0-bec7-08002be2092f} This class includes system-supplied drivers of 1394 host controllers connected on a PCI bus, but not drivers of 1394 peripherals. Battery Devices Class = Battery ClassGuid = {72631e54-78a4-11d0-bcf7-00aa00b7b32a} This class includes drivers of battery devices and UPSes. CD-ROM Drives Class = CDROM ClassGuid = {4d36e965-e325-11ce-bfc1-08002be10318} This class includes drivers of CD-ROM drives, including SCSI CD-ROM drives. By default, the system's CD-ROM class installer also installs a system-supplied CD audio driver and CD-ROM changer driver as PnP filters. Disk Drives Class = DiskDrive ClassGuid = {4d36e967-e325-11ce-bfc1-08002be10318} This class includes drivers of hard disk drives. See also the HDC and SCSIAdapter classes. Display Adapters Class = Display ClassGuid = {4d36e968-e325-11ce-bfc1-08002be10318} This class includes drivers of video adapters, including display drivers and video miniports. Floppy Disk Controllers Class = FDC ClassGuid = {4d36e969-e325-11ce-bfc1-08002be10318} This class includes drivers of floppy disk drive controllers. Floppy Disk Drives Class= FloppyDisk ClassGuid= {4d36e980-e325-11ce-bfc1-08002be10318} This class includes drivers of floppy drives. Hard Disk Controllers Class = HDC ClassGuid = {4d36e96a-e325-11ce-bfc1-08002be10318} This class includes drivers of hard disk controllers, including ATA/ATAPI controllers but not SCSI and RAID disk controllers. Human Input Devices (HID) Class = HIDClass ClassGuid = {745a17a0-74d3-11d0-b6fe-00a0c90f57da} This class includes devices that export interfaces of the HID class, including HID keyboard and mouse devices, which the installed HID device drivers enumerate as their respective "child" devices. (See also the Keyboard or Mouse classes later in this list.) Imaging Device Class = Image ClassGuid = {6bdd1fc6-810f-11d0-bec7-08002be2092f} This class includes drivers of still-image capture devices, digital cameras, and scanners. IrDA Devices Class = Infrared ClassGuid = {6bdd1fc5-810f-11d0-bec7-08002be2092f} This class includes Serial-IR and Fast-IR NDIS miniports, but see also the Network Adapter class for other NDIS NIC miniports. Keyboard Class = Keyboard ClassGuid = {4d36e96b-e325-11ce-bfc1-08002be10318} This class includes all keyboards. That is, it also must be specified in the (secondary) INF for an enumerated "child" HID keyboard device. Medium Changers Class= MediumChanger ClassGuid= {ce5939ae-ebde-11d0-b181-0000f8753ec4} This class includes drivers of SCSI media changer devices. Memory Technology Driver Class = MTD ClassGUID = {4d36e970-e325-11ce-bfc1-08002be10318} This class includes drivers for memory devices, such as flash memory cards. Multimedia Class = Media ClassGuid = {4d36e96c-e325-11ce-bfc1-08002be10318} This class includes Audio and DVD multimedia devices, joystick ports, and full-motion video-capture devices. Modem Class = Modem ClassGuid = {4d36e96d-e325-11ce-bfc1-08002be10318} This class installs modems. An INF for a device of this class installs no device driver(s), but rather specifies the features and configuration information of a particular modem and stores this information in the registry. See also the Multifunction class. Monitor Class = Monitor ClassGuid = {4d36e96e-e325-11ce-bfc1-08002be10318} This class includes display monitors. An INF for a device of this class installs no device driver(s), but rather specifies the features of a particular monitor to be stored in the registry for use by drivers of video adapters. (Monitors are enumerated as the child devices of display adapters.) Mouse Class = Mouse ClassGuid = {4d36e96f-e325-11ce-bfc1-08002be10318} This class includes all mice and other kinds of pointing devices, such as trackballs. That is, it also must be specified in the (secondary) INF for an enumerated "child" HID mouse device. Multifunction Devices Class = Multifunction ClassGuid = {4d36e971-e325-11ce-bfc1-08002be10318} This class includes combo cards, such as a PCMCIA modem and netcard adapter. The driver for such a PnP multifunction device is installed under this class and enumerates the modem and netcard separately as its "child" devices. Multi-port Serial Adapters Class = MultiportSerial ClassGuid = {50906cb8-ba12-11d1-bf5d-0000f805f530} This class includes intelligent multiport serial cards, but not peripheral devices that connect to its ports. It does not include unintelligent (16550-type) mutiport serial controllers or single-port serial controllers (see the Ports class). Network Adapter Class = Net ClassGuid = {4d36e972-e325-11ce-bfc1-08002be10318} This class includes NDIS NIC miniports excluding Fast-IR miniports, NDIS intermediate drivers (of "virtual adapters"), and CoNDIS MCM miniports. Network Client Class = NetClient ClassGuid = {4d36e973-e325-11ce-bfc1-08002be10318} This class includes network and/or print providers. Network Service Class = NetService ClassGuid = {4d36e974-e325-11ce-bfc1-08002be10318} This class includes network services, such as redirectors and servers. Network Transport Class = NetTrans ClassGuid = {4d36e975-e325-11ce-bfc1-08002be10318} This class includes NDIS protocols, CoNDIS stand-alone call managers, and CoNDIS clients, as well as higher level drivers in transport stacks. PCMCIA Adapters Class = PCMCIA ClassGuid = {4d36e977-e325-11ce-bfc1-08002be10318} This class includes system-supplied drivers of PCMCIA and CardBus host controllers, but not drivers of PCMCIA or CardBus peripherals. Ports (COM & LPT serial ports) Class = Ports ClassGuid = {4d36e978-e325-11ce-bfc1-08002be10318} This class includes drivers of serial or parallel port devices, but see also the MultiportSerial class. Printer Class = Printer ClassGuid = {4d36e979-e325-11ce-bfc1-08002be10318} This class includes printers. SCSI and RAID Controllers Class = SCSIAdapter ClassGuid = {4d36e97b-e325-11ce-bfc1-08002be10318} This class includes SCSI HBA miniports and disk-array controller drivers. Smart Card Readers Class = SmartCardReader ClassGuid = {50dd5230-ba8a-11d1-bf5d-0000f805f530} This class includes drivers for smart card readers. Storage Volumes Class = Volume ClassGuid = {71a27cdd-812a-11d0-bec7-08002be2092f} This class includes storage volumes as defined by the system-supplied logical volume manager and class drivers that create device objects to represent storage volumes, such as the system disk class driver. System Devices Class = System ClassGuid = {4d36e97d-e325-11ce-bfc1-08002be10318} This class includes the Windows® 2000 HALs, system bus drivers, the system ACPI driver, and the system volume-manager driver. It also includes battery drivers and UPS drivers. Tape Drives Class = TapeDrive ClassGuid = {6d807884-7d21-11cf-801c-08002be10318} This class includes drivers of tape drives, including all tape miniclass drivers. USB Class = USB ClassGuid = {36fc9e60-c465-11cf-8056-444553540000} This class includes system-supplied (bus) drivers of USB host controllers and drivers of USB hubs, but not drivers of USB peripherals. The following classes and GUIDs should not be used to install devices (or drivers) on Windows 2000 platforms: Adapter Class = Adapter ClassGUID = {4d36e964-e325-11ce-bfc1-08002be10318} This class is obsolete. APM Class = APMSupport ClassGUID = {d45b1c18-c8fa-11d1-9f77-0000f805f530} This class is reserved for system use. Computer Class = Computer ClassGUID = {4d36e966-e325-11ce-bfc1-08002be10318} This class is reserved for system use. Decoders Class = Decoder ClassGUID = {6bdd1fc2-810f-11d0-bec7-08002be2092f} This class is reserved for future use. Global Positioning System Class = GPS ClassGUID = {6bdd1fc3-810f-11d0-bec7-08002be2092f} This class is reserved for future use. No driver Class = NoDriver ClassGUID = {4d36e976-e325-11ce-bfc1-08002be10318} This class is obsolete. Non-Plug and Play Drivers Class = LegacyDriver ClassGUID = {8ecc055d-047f-11d1-a537-0000f8753ed1} This class is reserved for system use. Other Devices Class = Unknown ClassGUID = {4d36e97e-e325-11ce-bfc1-08002be10318} This class is reserved for system use. Enumerated devices for which the system cannot determine the type are installed under this class. Do not use this class if you're unsure in which class your device belongs; either determine the correct device setup class or create a new class. Printer Upgrade Class = Printer Upgrade ClassGUID = {4d36e97a-e325-11ce-bfc1-08002be10318} This class is reserved for system use. Sound Class = Sound ClassGUID = {4d36e97c-e325-11ce-bfc1-08002be10318} This class is obsolete. USB Mass Storage Device ClassGUID = a5dcbf10-6530-11d2-901f-00c04fb951ed