⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddlg.cpp

📁 枚举所有系统中存在的设备 很简单的一个程序
💻 CPP
字号:
// dDlg.cpp : implementation file
//

#include "stdafx.h"
#include "d.h"
#include "dDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#pragma comment(lib,"Setupapi.lib")




CDDlg::CDDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

}

void CDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDDlg)
	DDX_Control(pDX, IDC_LIST1, m_strList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDDlg, CDialog)
//{{AFX_MSG_MAP(CDDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDDlg message handlers







// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.




// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.












void CDDlg::OnButton1() 
{
    m_strList.ResetContent();
	
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i;
	
    // Create a HDEVINFO with all present devices.
    hDevInfo = SetupDiGetClassDevs(	NULL,  
									0, // Enumerator
									0,
									DIGCF_PRESENT | DIGCF_ALLCLASSES );
    
    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
        // Insert error handling here.
        return ;
    }
    
    // Enumerate through all devices in Set.
    
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,  &DeviceInfoData);i++)
    {
        DWORD DataT;
        LPTSTR buffer = NULL;
        DWORD buffersize = 0;
        
        
        // Call function with null to begin with, 
        // then use the returned buffer size 
        // to Alloc the buffer. Keep calling until
        // success or an unknown failure.
        
        while (!SetupDiGetDeviceRegistryProperty(   hDevInfo,
													&DeviceInfoData,
													SPDRP_DEVICEDESC,
													&DataT,
													(PBYTE)buffer,
													buffersize,
													&buffersize))
        {
            if (GetLastError() ==  ERROR_INSUFFICIENT_BUFFER)
            {
                //改变缓冲区大小
                if (buffer) 
					LocalFree(buffer);
                buffer = (char*)LocalAlloc(LPTR,buffersize);
            }
            else
            {
                //在这里放错误处理代码
                break;
            }
        }
		
		m_strList.AddString(buffer);
		
        if (buffer) 
			LocalFree(buffer);
    }
    
    
    if ( GetLastError()!=NO_ERROR &&  GetLastError()!=ERROR_NO_MORE_ITEMS )
    {
		//在这里放错误处理代码
        return ;
    }
    
    // 清理
    SetupDiDestroyDeviceInfoList(hDevInfo); 
	
	
	
	
}

⌨️ 快捷键说明

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