zoukankan      html  css  js  c++  java
  • VC++获取计算机Hardware Information (CPU ID, MainBoard Info, Hard Disk Serial, System Information)

    转载:http://blog.csdn.net/yapingxin/article/details/50107799

    转载:http://zhidao.baidu.com/link?url=A5K6NgF4pXAX2hp_NYCd97OEdHTBFlATxWb40HLv2XEbVjv2-LNNnBN1pheP51C_Rs0XYLAWSjySEfRWePKPW_

    参考文章:http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I

    1.计算机名称

     1 //计算机名称
     2 void DisplayComputerNameEx()
     3 {
     4     TCHAR scComputerName[MAX_COMPUTERNAME_LENGTH*2 + 1];
     5     DWORD lnNameLength = MAX_COMPUTERNAME_LENGTH*2;
     6 
     7     GetComputerNameEx(ComputerNameNetBIOS, scComputerName, &lnNameLength);
     8 
     9     _tprintf( _T("Computer name: %s
    "), scComputerName);
    10 }

    2.获取当前用户名(当前登录用户名)

     1 CString GetCurrentUserName()
     2 {
     3     CString strUserName;
     4 
     5     LPTSTR szBuffer=new wchar_t[300];  
     6     DWORD dwSize=300;  
     7     GetUserName(szBuffer,&dwSize);  
     8     strUserName=szBuffer;  
     9 
    10     delete szBuffer; 
    11 
    12     return strUserName;
    13 }

    3.处理器个数

    1 //处理器个数
    2 void DisplayProcessorCount()
    3 {
    4     SYSTEM_INFO sysInfo;
    5     GetSystemInfo(&sysInfo);
    6 
    7     _tprintf( _T("Number of processors: %d 
    "), sysInfo.dwNumberOfProcessors);
    8 }

    4.CPU  ID

      1 //处理器ID
      2 CString GetCPUID()
      3 {
      4 
      5     CString CPUID;
      6 
      7     unsigned long s1,s2;
      8 
      9     unsigned char vendor_id[]="------------";
     10 
     11     char sel;
     12 
     13     sel='1';
     14 
     15     CString VernderID;
     16 
     17     CString MyCpuID,CPUID1,CPUID2;
     18 
     19     switch(sel)
     20 
     21     {
     22 
     23     case '1':
     24 
     25         __asm{
     26 
     27             xor eax,eax//eax=0:取Vendor信息
     28 
     29                 cpuid//取cpu id指令,可在Ring3级使用
     30 
     31                 mov dword ptr vendor_id,ebx
     32 
     33                 mov dword ptr vendor_id[+4],edx
     34 
     35                 mov dword ptr vendor_id[+8],ecx
     36 
     37         }
     38 
     39         VernderID.Format(_T("%s-"),vendor_id);
     40 
     41         __asm{
     42 
     43             mov eax,01h//eax=1:取CPU序列号
     44 
     45                 xor edx,edx
     46 
     47                 cpuid
     48 
     49                 mov s1,edx
     50 
     51                 mov s2,eax
     52 
     53         }
     54 
     55         CPUID1.Format(_T("%08X%08X"),s1,s2);
     56 
     57         __asm{
     58 
     59             mov eax,03h
     60 
     61                 xor ecx,ecx
     62 
     63                 xor edx,edx
     64 
     65                 cpuid
     66 
     67                 mov s1,edx
     68 
     69                 mov s2,ecx
     70 
     71         }
     72 
     73         CPUID2.Format(_T("%08X%08X"),s1,s2);
     74 
     75         break;
     76 
     77     case '2':
     78 
     79         {
     80 
     81             __asm{
     82 
     83                 mov ecx,119h
     84 
     85                     rdmsr
     86 
     87                     or eax,00200000h
     88 
     89                     wrmsr
     90 
     91             }
     92 
     93         }
     94 
     95 
     96         MessageBox(NULL,_T("CPU id is disabled."),_T("help"),MB_OK);
     97 
     98         break;
     99 
    100     }
    101 
    102     MyCpuID = CPUID1+CPUID2;
    103 
    104     CPUID = MyCpuID;
    105 
    106     return CPUID;
    107 
    108 }

    5.硬盘ID

      1 #define _WIN32_DCOM
      2 #include <iostream>
      3 using namespace std;
      4 #include <comdef.h>
      5 #include <Wbemidl.h>
      6 
      7 #pragma comment(lib, "wbemuuid.lib")
      8 
      9 int main(int argc, char **argv)
     10 {
     11     HRESULT hres;
     12 
     13     // Step 1: --------------------------------------------------
     14     // Initialize COM. ------------------------------------------
     15 
     16     hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
     17     if (FAILED(hres))
     18     {
     19         cout << "Failed to initialize COM library. Error code = 0x" 
     20             << hex << hres << endl;
     21         return 1;                  // Program has failed.
     22     }
     23 
     24     // Step 2: --------------------------------------------------
     25     // Set general COM security levels --------------------------
     26     // Note: If you are using Windows 2000, you need to specify -
     27     // the default authentication credentials for a user by using
     28     // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
     29     // parameter of CoInitializeSecurity ------------------------
     30 
     31     hres =  CoInitializeSecurity(
     32         NULL, 
     33         -1,                          // COM authentication
     34         NULL,                        // Authentication services
     35         NULL,                        // Reserved
     36         RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
     37         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
     38         NULL,                        // Authentication info
     39         EOAC_NONE,                   // Additional capabilities 
     40         NULL                         // Reserved
     41         );
     42 
     43     if (FAILED(hres))
     44     {
     45         cout << "Failed to initialize security. Error code = 0x" 
     46             << hex << hres << endl;
     47         CoUninitialize();
     48         return 1;                    // Program has failed.
     49     }
     50 
     51     // Step 3: ---------------------------------------------------
     52     // Obtain the initial locator to WMI -------------------------
     53 
     54     IWbemLocator *pLoc = NULL;
     55 
     56     hres = CoCreateInstance(
     57         CLSID_WbemLocator,             
     58         0, 
     59         CLSCTX_INPROC_SERVER, 
     60         IID_IWbemLocator, (LPVOID *) &pLoc);
     61 
     62     if (FAILED(hres))
     63     {
     64         cout << "Failed to create IWbemLocator object."
     65             << " Err code = 0x"
     66             << hex << hres << endl;
     67         CoUninitialize();
     68         return 1;                 // Program has failed.
     69     }
     70 
     71     // Step 4: -----------------------------------------------------
     72     // Connect to WMI through the IWbemLocator::ConnectServer method
     73 
     74     IWbemServices *pSvc = NULL;
     75 
     76     // Connect to the rootcimv2 namespace with
     77     // the current user and obtain pointer pSvc
     78     // to make IWbemServices calls.
     79     hres = pLoc->ConnectServer(
     80         _bstr_t(L"ROOT\CIMV2"), // Object path of WMI namespace
     81         NULL,                    // User name. NULL = current user
     82         NULL,                    // User password. NULL = current
     83         0,                       // Locale. NULL indicates current
     84         NULL,                    // Security flags.
     85         0,                       // Authority (e.g. Kerberos)
     86         0,                       // Context object 
     87         &pSvc                    // pointer to IWbemServices proxy
     88         );
     89 
     90     if (FAILED(hres))
     91     {
     92         cout << "Could not connect. Error code = 0x" 
     93             << hex << hres << endl;
     94         pLoc->Release();     
     95         CoUninitialize();
     96         return 1;                // Program has failed.
     97     }
     98 
     99     cout << "Connected to ROOT\CIMV2 WMI namespace" << endl;
    100 
    101     // Step 5: --------------------------------------------------
    102     // Set security levels on the proxy -------------------------
    103 
    104     hres = CoSetProxyBlanket(
    105         pSvc,                        // Indicates the proxy to set
    106         RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
    107         RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
    108         NULL,                        // Server principal name 
    109         RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
    110         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
    111         NULL,                        // client identity
    112         EOAC_NONE                    // proxy capabilities 
    113         );
    114 
    115     if (FAILED(hres))
    116     {
    117         cout << "Could not set proxy blanket. Error code = 0x" 
    118             << hex << hres << endl;
    119         pSvc->Release();
    120         pLoc->Release();     
    121         CoUninitialize();
    122         return 1;               // Program has failed.
    123     }
    124 
    125     // Step 6: --------------------------------------------------
    126     // Use the IWbemServices pointer to make requests of WMI ----
    127 
    128     // For example, get the name of the operating system
    129     IEnumWbemClassObject* pEnumerator = NULL;
    130     hres = pSvc->ExecQuery(
    131         bstr_t("WQL"), 
    132         bstr_t("SELECT * FROM Win32_PhysicalMedia"),
    133         WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
    134         NULL,
    135         &pEnumerator);
    136 
    137     if (FAILED(hres))
    138     {
    139         cout << "Query for physical media failed."
    140             << " Error code = 0x" 
    141             << hex << hres << endl;
    142         pSvc->Release();
    143         pLoc->Release();
    144         CoUninitialize();
    145         return 1;               // Program has failed.
    146     }
    147 
    148     // Step 7: -------------------------------------------------
    149     // Get the data from the query in step 6 -------------------
    150 
    151     IWbemClassObject *pclsObj = NULL;
    152     ULONG uReturn = 0;
    153 
    154     while (pEnumerator)
    155     {
    156         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
    157             &pclsObj, &uReturn);
    158 
    159         if(0 == uReturn)
    160         {
    161             break;
    162         }
    163 
    164         VARIANT vtProp;
    165 
    166         // Get the value of the Name property
    167         hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
    168 
    169         wcout << "Serial Number : " << vtProp.bstrVal << endl;
    170         VariantClear(&vtProp);
    171     }
    172 
    173     // Cleanup
    174     // ========
    175 
    176     pSvc->Release();
    177     pLoc->Release();
    178     pEnumerator->Release();
    179     pclsObj->Release();
    180     CoUninitialize();
    181 
    182     return 0;   // Program successfully completed.
    183 }

    6.主板ID

      1 //获取主板ID
      2 BOOL GetMainBoardInfoByCmd(char* & lpszBaseBoard)
      3 {
      4     const long COMMAND_SIZE = 1020; // Command line output buffer
      5     const DWORD WAIT_TIME = 500; // INFINITE
      6 
      7     // The command to get mainboard serial number
      8     TCHAR szFetCmd[] = _T("wmic BaseBoard get SerialNumber");
      9 
     10     // Pre- information of mainboard serial number
     11     const std::string strEnSearch = "SerialNumber";
     12 
     13     BOOL fReturnCode = FALSE;
     14     HANDLE hReadPipe = NULL;  // Pipeline for READ
     15     HANDLE hWritePipe = NULL; // Pipeline for WRITE
     16     PROCESS_INFORMATION pi;   // Process information
     17     STARTUPINFO            si;   // Control-command window info
     18     SECURITY_ATTRIBUTES sa;   // Security attributes
     19 
     20     char szBuffer[COMMAND_SIZE + 1] = { 0 }; // Command line output buffer
     21     std::string strBuffer;
     22     DWORD count = 0;
     23     size_t pos = 0;
     24     size_t i = 0;
     25     size_t j = 0;
     26 
     27     lpszBaseBoard = (char*)malloc((COMMAND_SIZE + 1)*sizeof(char));
     28     memset(lpszBaseBoard, 0x00, (COMMAND_SIZE + 1)*sizeof(char));
     29 
     30     memset(&pi, 0, sizeof(pi));
     31     memset(&si, 0, sizeof(si));
     32     memset(&sa, 0, sizeof(sa));
     33 
     34     pi.hProcess = NULL;
     35     pi.hThread = NULL;
     36     si.cb = sizeof(STARTUPINFO);
     37     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
     38     sa.lpSecurityDescriptor = NULL;
     39     sa.bInheritHandle = TRUE;
     40 
     41     // Step 1: Create pipeline
     42     fReturnCode = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
     43     if (!fReturnCode)
     44     {
     45         goto EXIT;
     46     }
     47 
     48     // Step 2: Set command line window to be specific READ / WRITE pipeline
     49     GetStartupInfo(&si);
     50     si.hStdError = hWritePipe;
     51     si.hStdOutput = hWritePipe;
     52     si.wShowWindow = SW_HIDE; // hide command line window
     53     si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
     54 
     55     // Step 3: Create process to get command line handle
     56     fReturnCode = CreateProcess(NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
     57     if (!fReturnCode)
     58     {
     59         goto EXIT;
     60     }
     61 
     62     // Step 4: Get return back data
     63     WaitForSingleObject(pi.hProcess, WAIT_TIME);
     64     fReturnCode = ReadFile(hReadPipe, szBuffer, COMMAND_SIZE, &count, 0);
     65     if (!fReturnCode)
     66     {
     67         goto EXIT;
     68     }
     69 
     70     // Step 5: Search for mainboard serial number
     71     fReturnCode = FALSE;
     72     strBuffer = szBuffer;
     73     pos = strBuffer.find(strEnSearch);
     74 
     75     if (pos < 0) // NOT FOUND
     76     {
     77         goto EXIT;
     78     }
     79     else
     80     {
     81         strBuffer = strBuffer.substr(pos + strEnSearch.length());
     82     }
     83 
     84     memset(szBuffer, 0x00, sizeof(szBuffer));
     85     strcpy_s(szBuffer, strBuffer.c_str());
     86 
     87     // Get ride of <space>, 
    , 
    
     88     j = 0;
     89     for (i = 0; i < strlen(szBuffer); i++)
     90     {
     91         if (szBuffer[i] != ' ' && szBuffer[i] != '
    ' && szBuffer[i] != '
    ')
     92         {
     93             lpszBaseBoard[j] = szBuffer[i];
     94             j++;
     95         }
     96     }
     97 
     98     fReturnCode = TRUE;
     99 
    100 EXIT:
    101     CloseHandle(hWritePipe);
    102     CloseHandle(hReadPipe);
    103     CloseHandle(pi.hProcess);
    104     CloseHandle(pi.hThread);
    105 
    106     return(fReturnCode);
    107 }

    获取主板ID的用法

     1 {
     2     . . . . . .
     3    char* lpszMainBoardSN = NULL;
     4     GetMainBoardInfoByCmd(lpszMainBoardSN);
     5 
     6     if (lpszMainBoardSN)
     7     {
     8         printf("%s
    ", lpszMainBoardSN);
     9 
    10         free(lpszMainBoardSN);
    11         lpszMainBoardSN = NULL;
    12     }
    13     else
    14     {
    15         printf("N/A
    ");
    16     }
    17 
    18    . . . . . .
    19 }

    示例Demo

  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/chechen/p/5899190.html
Copyright © 2011-2022 走看看