getvar.c

来自「C#一个实例源代码」· C语言 代码 · 共 80 行

C
80
字号
/*-----------------------------------------------------------
GetVar.c
-----------------------------------------------------------*/
#include"windows.h"
#include"stdio.h"

#define RTN_UNKNOWN 0
#define RTN_SERVER 1
#define RTN_WORKSTATION 2
#define RTN_NTAS 3
#define RTN_ERROR 13

DWORD GetWindowsVariant(void)
{
    #define MY_BUFSIZE 32    // Arbitrary initial value. 
                             // Dynamic allocation will be used.
    HKEY hKey;
    TCHAR szProductType[MY_BUFSIZE];
    DWORD dwBufLen = MY_BUFSIZE;
    LONG lRet;

    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
                    0,
                    KEY_QUERY_VALUE,
                    &hKey) != ERROR_SUCCESS) return RTN_ERROR;

    lRet = RegQueryValueEx(hKey,
                    TEXT("ProductType"),
                    NULL,
                    NULL,
                    (LPBYTE)szProductType,
                    &dwBufLen);

    RegCloseKey(hKey);

    if(lRet != ERROR_SUCCESS) return RTN_ERROR;

    // check product options, in order of likelihood
    if(lstrcmpi(TEXT("WINNT"), szProductType) == 0) 
         return RTN_WORKSTATION;
    if(lstrcmpi(TEXT("SERVERNT"), szProductType) == 0) 
         return RTN_SERVER;
    if(lstrcmpi(TEXT("LANMANNT"), szProductType) == 0) 
         return RTN_NTAS;
    // else return "unknown variant" code
    else return RTN_UNKNOWN;
} 
int main()
{
	DWORD WinVar;

	WinVar=GetWindowsVariant();

	switch(WinVar)
	{
	case 0:
    printf("%s\n","Unknowm");
	break;

	case 1:
    printf("%s\n","Server");
	break;

	case 2:
    printf("%s\n","Workstation");
	break;

	case 3:
    printf("%s\n","Ntas");
	break;

	case 13:
    printf("%s\n","Error");
	break;
	}
	return 0;
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?