显卡 mac ip地址获取

#include "stdafx.h"
#include "HardTest.h"
#include <iostream>
using namespace std;
#include <comdef.h>
#define _WIN32_DCOM
#include <Wbemidl.h>
#include <Wbemcli.h>
#pragma comment(lib,"wbemuuid.lib")
#include "d3d9.h"
#include "ddraw.h"

#include   <stdio.h>   
#include   <stdlib.h>   
#include   <httpext.h>   
#include   <windef.h>   
#include   <Nb30.h>
#include   <WS2tcpip.h>
int CDialog(vector<DISP_ADAPTER_INFO>& dispInfoVec)
{
    HRESULT hres;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

    hres =  CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x"
            << hex << hres << endl;
        return FALSE;                  // Program has failed.
    }

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    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))
    {
        cout << "Failed to initialize security. Error code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return FALSE;                    // Program has failed.
    }

    // Step 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))
    {
        cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return FALSE;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;

    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    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))
    {
        cout << "Could not connect. Error code = 0x"
            << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return FALSE;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


    // Step 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))
    {
        cout << "Could not set proxy blanket. Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return FALSE;               // Program has failed.
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    CString strRet = _T("显卡:");
    CString strMemSize = _T("");

    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t("SELECT * FROM Win32_VideoController"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        cout << "Query for operating system name failed."
            << " Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        CoUninitialize();
        return FALSE;              // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;

    LONGLONG llTotal = 0;

    //    下面对具体的信息进行枚举
    DISP_ADAPTER_INFO adapterInfo;
    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
            &pclsObj, &uReturn);

        if(0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property
        hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);

        adapterInfo.csDesc =  vtProp.bstrVal;

        VariantClear(&vtProp);

        hr = pclsObj->Get(L"AdapterRAM", 0, &vtProp, 0, 0);

        strMemSize.Format("%ld", vtProp.llVal);

        llTotal += (LONGLONG)&vtProp ;
        strMemSize.Format( _T("(%I64d)") , ( llTotal / (1024*1024) ) );

        adapterInfo.iRam = atoi(strMemSize);

        VariantClear(&vtProp);

        dispInfoVec.push_back(adapterInfo);
    }

    return TRUE;
}

bool GetDispAdapterInfo_2_Info(vector<DISP_ADAPTER_INFO>& dispInfoVec)
{
    LPDIRECT3D9 pD3D=NULL;
    pD3D=Direct3DCreate9(D3D_SDK_VERSION);//创建Direct 3D对象
    if (!pD3D)
    {
        return false;
    }

    DWORD m_dwNumAdapters=pD3D-> GetAdapterCount();//获得显卡数量
    DISP_ADAPTER_INFO adapterInfo;
    for(UINT iAdapter=0;iAdapter<m_dwNumAdapters;iAdapter++)
    {
        D3DADAPTER_IDENTIFIER9 di;
        pD3D-> GetAdapterIdentifier(iAdapter,0,&di);//获得显卡信息
        char  szBuf[MAX_DEVICE_IDENTIFIER_STRING];
        sprintf(szBuf,"%s",di.Description);//  

        adapterInfo.csDesc = di.Description;
        dispInfoVec.push_back(adapterInfo);
    }

    return true;
}

bool GetDispAdapterInfo_2_Mem(vector<DISP_ADAPTER_INFO>& dispInfoVec)
{
    LPDIRECTDRAW2 lpdd;
    HRESULT ddrval;

    CoInitialize(NULL);    

    ddrval = CoCreateInstance(CLSID_DirectDraw,
        NULL, CLSCTX_ALL, IID_IDirectDraw2, (void**)&lpdd);

    if (FAILED(ddrval))
    {
        return false;
    }

    if(!FAILED(ddrval))
    {
        ddrval = IDirectDraw2_Initialize(lpdd, NULL);
    }

    DDCAPS ddcaps;

    ddcaps.dwSize = sizeof DDCAPS;
    lpdd->GetCaps(&ddcaps, NULL);

    lpdd->Release();

    DWORD dwMem = ddcaps.dwVidMemTotal;

    int iMem = dwMem/1008/1008;

    CString strMem;
    strMem.Format(_T("display memory is %d MB"),iMem);

    for (vector<DISP_ADAPTER_INFO>::iterator adapterIt=dispInfoVec.begin(); adapterIt!=dispInfoVec.end(); ++adapterIt)
    {
        adapterIt->iRam = iMem;
    }

    CoUninitialize();

    return true;
}

bool GetDispAdapterInfo_2(vector<DISP_ADAPTER_INFO>& dispInfoVec)
{
    GetDispAdapterInfo_2_Info(dispInfoVec);
    GetDispAdapterInfo_2_Mem(dispInfoVec);

    return true;
}


int getMAC(char * mac)     
{     
    NCB ncb;     
    typedef struct _ASTAT_     
    {
        ADAPTER_STATUS   adapt;  
        NAME_BUFFER   NameBuff   [30];     
    }ASTAT,   *   PASTAT;     
    ASTAT Adapter;     

    typedef struct _LANA_ENUM     
    {   //   le
        UCHAR   length;  
        UCHAR   lana[MAX_LANA];     
    }LANA_ENUM   ;     
    LANA_ENUM lana_enum;     

    UCHAR uRetCode;     
    memset(&ncb, 0, sizeof(ncb));     
    memset(&lana_enum, 0, sizeof(lana_enum));     

    ncb.ncb_command = NCBENUM;     
    ncb.ncb_buffer = (unsigned char *)&lana_enum;     
    ncb.ncb_length = sizeof(LANA_ENUM);     
    uRetCode = Netbios(&ncb);     
    if(uRetCode != NRC_GOODRET)     
        return uRetCode;     

    for(int lana=0; lana<lana_enum.length; lana++)     
    {
        ncb.ncb_command = NCBRESET;
        ncb.ncb_lana_num = lana_enum.lana[lana];
        uRetCode = Netbios(&ncb);   
        if(uRetCode == NRC_GOODRET)  
            break;
    }
    if(uRetCode != NRC_GOODRET)
        return uRetCode;     

    memset(&ncb, 0, sizeof(ncb));  
    ncb.ncb_command = NCBASTAT;  
    ncb.ncb_lana_num = lana_enum.lana[0];
    strcpy((char*)ncb.ncb_callname, "*");
    ncb.ncb_buffer = (unsigned char *)&Adapter;
    ncb.ncb_length = sizeof(Adapter);
    uRetCode = Netbios(&ncb);  
    if(uRetCode != NRC_GOODRET)   
        return uRetCode;     
    sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",    
        Adapter.adapt.adapter_address[0],     
        Adapter.adapt.adapter_address[1],     
        Adapter.adapt.adapter_address[2],     
        Adapter.adapt.adapter_address[3],     
        Adapter.adapt.adapter_address[4],     
        Adapter.adapt.adapter_address[5]  
    );
    return 0;   
}
int GetLocalAddress(char * pszIp,int nMaxSize)
{
    struct addrinfo * result;
    int error;

    char hostname[NI_MAXHOST] = "";
    gethostname(hostname, sizeof(hostname));
    error = getaddrinfo(hostname, NULL, NULL, &result);

    if (0 != error)
    {   
        fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
        return 1;
    }   
    struct sockaddr_in* pSockaddr=(sockaddr_in*)result->ai_addr;
    strcpy_s(pszIp,nMaxSize,inet_ntoa(pSockaddr->sin_addr));

    freeaddrinfo(result);
    return 0;
}

你可能感兴趣的:(IP地址)