鲨丁鱼.net技术小栈

这里讨论.net的web和form开发,还有其它关于WEB开发和安全的全部知识点,顺带一些经典的有意思的杂文!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#/C++获取CPU和硬盘系列号的源代码

Posted on 2005-12-21 13:45  King0502  阅读(264)  评论(0编辑  收藏  举报

下面就具体的代码作一说明:
1、C#

        public string CpuID
        
{//要引用“using System.Management;”
            get
            
{
                
string strProcessorId;
                ManagementObjectSearcher opSearch;
                ManagementObjectCollection mocSystem;
                strProcessorId
="";
                
try
                
{
                    opSearch 
= new  ManagementObjectSearcher("SELECT * FROM Win32_Processor");
                    mocSystem
=opSearch.Get();//Win32_Processor:在MSDN查找
                    foreach( ManagementObject opInfo in mocSystem)
                    
{//返回所有CPU的ID,分别返回可修改下述语句
                        strProcessorId+= opInfo["ProcessorId"].ToString().Trim() ;
                        
//ProcessorId:在MSDN查找更多
                    }

                }

                
catch
                
{
                
//TO DO:添加异常处理
                }

                
return strProcessorId;
            }

        }

 

public string DiskDrive
        
{//硬盘厂商
            get
            
{
                
string strDiskDrive;
                ManagementObjectSearcher opSearch;
                ManagementObjectCollection mocSystem;                
                strDiskDrive
="";
                
try
                
{
                    opSearch 
= new  ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
                    mocSystem
=opSearch.Get();
                    
foreach( ManagementObject opInfo in mocSystem)
                    
{
                        strDiskDrive
+= opInfo["PNPDeviceID"].ToString().Trim() ;
                    }

                }

                
catch
                
{
                    
//TO DO:添加异常处理                    
                }

                
return strDiskDrive;
            }

        }
    

2、C++
 代码框架,你所要做的仅是修改WQL语句,具体可看MSDN。以下代码为简介起见,一次返回所有的CPUID或硬盘厂商系列号。

#include "StdAfx.h"
#include 
".\puiddisk.h"
#define _WIN32_DCOM
#include 
<iostream>
using namespace std;
#include 
<comdef.h>
#include 
<Wbemidl.h>

# pragma comment(lib, 
"wbemuuid.lib")
CpuIDDisk::CpuIDDisk(
void)
{
    GetInfomation();
}


CpuIDDisk::
~CpuIDDisk(void)
{
}

int CpuIDDisk::GetInfomation(void)
{
    HRESULT hres;
    
//步骤1:不是必须的,COM只须也只能初始化一次
     hres =  CoInitializeEx(0, COINIT_MULTITHREADED );
    
if (FAILED(hres))
    
{
        
return 1//初始化COM异常:注意,COM只须也只能初始化一次
    }

    
//步骤2:不是必须的,COM只须也只能设置一次
    
//Set general COM security levels
    hres =  CoInitializeSecurity(
        NULL, 
        
-1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );
                      
    
if (FAILED(hres))
    
{
        CoUninitialize();
        
return 1;                    // Program has failed.
    }

    
    
//以上不是必须的,若已有“::COMInit();”,则要跳过
    
//步骤3: Obtain the initial locator to WMI 
    IWbemLocator *pLoc = NULL;
    hres 
= CoCreateInstance(
        CLSID_WbemLocator,             
        
0
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID 
*&pLoc);
 
    
if (FAILED(hres))
    
{
        CoUninitialize();
        
return 1;//Failed to create IWbemLocator object
    }


    
//步骤4:Connect to WMI through the IWbemLocator::ConnectServer method
    IWbemServices *pSvc = NULL;
    hres 
= pLoc->ConnectServer(
         _bstr_t(L
"ROOT\\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );
    
    
if (FAILED(hres))
    
{
        pLoc
->Release();     
        CoUninitialize();
        
return 1;                // Program has failed.
    }

    
// 步骤5: Set security levels on the proxy
    hres = CoSetProxyBlanket(
       pSvc,                        
// Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

    
if (FAILED(hres))
    
{
        pSvc
->Release();
        pLoc
->Release();     
        CoUninitialize();
        
return 1
    }


    
// 步骤6:Use the IWbemServices pointer to make requests of WMI ----
    IEnumWbemClassObject* pEnumerator = NULL;
    
//计算CPUID
    hres = pSvc->ExecQuery(
        bstr_t(
"WQL"), 
        bstr_t(
"SELECT * FROM Win32_Processor"),//Win32_OperatingSystem
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        
&pEnumerator);
    
    
if (FAILED(hres))
    
{
        pSvc
->Release();
        pLoc
->Release();
        CoUninitialize();
        
return 1;
    }

    
// 步骤7:Get the data from the query 
    IWbemClassObject *pclsObj;
    ULONG uReturn 
= 0;
    
while (pEnumerator)
    
{
        HRESULT hr 
= pEnumerator->Next(WBEM_INFINITE, 1
            
&pclsObj, &uReturn);

        
if(0 == uReturn)
        
{
            
break;
        }

        VARIANT vtProp;
        VariantInit(
&vtProp);
        hr 
= pclsObj->Get(L"ProcessorId"0&vtProp, 00);
        strProcessID
=_com_util::ConvertBSTRToString(vtProp.bstrVal);//strProcessID:类级变量
    }


    
//计算硬盘系列号
    hres = pSvc->ExecQuery(
        bstr_t(
"WQL"), 
        bstr_t(
"SELECT * FROM Win32_DiskDrive"),
        WBEM_FLAG_FORWARD_ONLY 
| WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        
&pEnumerator);

    
if (FAILED(hres))
    
{
        pSvc
->Release();
        pLoc
->Release();
        CoUninitialize();
        
return 1
    }

    
while (pEnumerator)
    
{
        HRESULT hr 
= pEnumerator->Next(WBEM_INFINITE, 1
            
&pclsObj, &uReturn);

        
if(0 == uReturn)
        
{
            
break;
        }


        VARIANT vtProp;
        VariantInit(
&vtProp);
        hr 
= pclsObj->Get(L"PNPDeviceID"0&vtProp, 00);
    strDisk
=_com_util::ConvertBSTRToString(vtProp.bstrVal);
    }


    pSvc
->Release();
    pLoc
->Release();
    pEnumerator
->Release();
    pclsObj
->Release();
    CoUninitialize();

    
return 0;   
}