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

📄 setupdriverdlg.cpp

📁 多个设备驱动选择的实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	

// 	SetupDiClassNameFromGuid(&ClassGUID, ClassName, sizeof(ClassName), NULL);
    //
    // Create the container for the to-be-created Device Information Element.


    //
    DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);
    if(DeviceInfoSet == INVALID_HANDLE_VALUE)
    {
//         return DisplayError(TEXT("CreateDeviceInfoList"));
    }

    //
    // Now create the element.
    // Use the Class GUID and Name from the INF file.
    //

	/*CString strTemp,strResult;
	strTemp.Format(m_chHardwareID);
	
	int iFindTemp = 0;
	
	//while (iFindTemp = strTemp.Find("&", iFindTemp+1))
	{
		strResult = strTemp.Left(22);
		strResult += strTemp.Right(5);
	}
	m_chHardwareID = (char*)(LPCTSTR)strResult;*/

    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    if (!SetupDiCreateDeviceInfo(DeviceInfoSet,
        ClassName,
        &ClassGUID,
        NULL,
        NULL,
        DICD_GENERATE_ID,
        &DeviceInfoData))
    {
       /* DisplayError(TEXT("CreateDeviceInfo"));*/
        goto cleanup_DeviceInfo;
    }

    //
    // Add the HardwareID to the Device's HardwareID property.
    //

    if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,
        &DeviceInfoData,
        SPDRP_HARDWAREID,
        (LPBYTE)m_chHardwareID,
		sizeof(m_chHardwareID)))
    {
		int iError = GetLastError();
        goto cleanup_DeviceInfo;
    }

    //
    // Transform the registry element into an actual devnode
    // in the PnP HW tree.
    //
    if (!SetupDiCallClassInstaller(DIF_INSTALLDEVICE,
        DeviceInfoSet,
        &DeviceInfoData))
    {
        goto cleanup_DeviceInfo;
    }

    //
    // The element is now registered. We must explicitly remove the
    // device using DIF_REMOVE, if we encounter any failure from now on.
    //

    //
    // Install the Driver.
    //
    if (!UpdateDriverForPlugAndPlayDevices(0,
        m_chHardwareID,
        m_chINFPath,
        INSTALLFLAG_FORCE,
        RebootRequired))
    {
        DWORD err = GetLastError();
     //    DisplayError(TEXT("UpdateDriverForPlugAndPlayDevices"));

        if (!SetupDiCallClassInstaller(
            DIF_REMOVE,
            DeviceInfoSet,
            &DeviceInfoData))
        {
         //    DisplayError(TEXT("CallClassInstaller(REMOVE)"));
        }
        SetLastError(err);
    }

    //
    //  Cleanup.
    //
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);

    return err == NO_ERROR;
}

void CSetupDriverDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

BOOL CSetupDriverDlg::getDeviceEnum(void)
{
	HRESULT hr;
	ULONG cFetched;
	IMoniker *pM = NULL;
    IPropertyBag *pBag=0;
	IEnumMoniker *pEm=NULL;
	ICreateDevEnum *pCreateDevEnum=NULL;

	CoInitialize(NULL);

	hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
                          IID_ICreateDevEnum, (void**)&pCreateDevEnum);
	if(hr != NOERROR)
	{
		printf("No devices found in the device list.");
		return FALSE;
	}

	hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEm, 0);
	if(hr != NOERROR)       
	{
		printf("There are no VideoInputDevice devices available");
		return FALSE;
	}	
		
	SAFE_RELEASE(pCreateDevEnum);

	pEm->Reset();

	while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK)
	{
        hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
			
		if(SUCCEEDED(hr))
        {
            VARIANT var;
            var.vt = VT_BSTR;
			
			hr = pBag->Read(L"DevicePath", &var, 0);
			if (SUCCEEDED(hr))
			{
				if(var.vt == VT_BSTR)
				{
					CString str((LPCWSTR)var.bstrVal);
					CString vidFind, pidFind;
					
					vidFind = str.Right(str.GetLength() - str.Find("vid_")-4);
					vidFind = vidFind.Left(4);
						
					if (!vidFind.Compare("0c45"))
					{
// 						hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&pBaseFilter);
						GetInstanceID(str);
						return TRUE;
					}
					else
					{
					// 	pBaseFilter = NULL;
						hr = S_FALSE;
					}
				//	ShowMessage(str);
				}
			}
			VariantClear(&var);
		}
	}

	SAFE_RELEASE(pBag);
	SAFE_RELEASE(pM);					
	SAFE_RELEASE(pEm);
	
	return FALSE;
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSetupDriverDlg::OnQueryDragIcon()
{
	return (HCURSOR)m_hIcon;
}

BOOL CSetupDriverDlg::FindExistingDevice()
{
    HDEVINFO DeviceInfoSet;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i,err;
    BOOL Found;
	
    //
    // Create a Device Information Set with all present devices.
    //
    DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
        NULL,
        0,
        DIGCF_ALLCLASSES | DIGCF_PRESENT/*DIGCF_DEVICEINTERFACE*/ /*DIGCF_ALLCLASSES*/ /*| DIGCF_PRESENT*/ ); // All devices present on system

    if (DeviceInfoSet == INVALID_HANDLE_VALUE)
    {
     //    return DisplayError(TEXT("GetClassDevs(All Present Devices)"));
			
    }

    //_tprintf(TEXT("Search for Device ID: [%s]\n"),HardwareId);

    Found = FALSE;
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
    {
        DWORD DataT;
        LPSTR buffer = NULL;
        DWORD buffersize = 0;
		
        //
        // We won't know the size of the HardwareID buffer until we call
        // this function. So call it with a null to begin with, and then
        // use the required buffer size to Alloc the nessicary space.
        // Keep calling we have success or an unknown failure.
        //
        while (!SetupDiGetDeviceRegistryProperty(
            DeviceInfoSet,
            &DeviceInfoData,
            SPDRP_HARDWAREID,
            &DataT,
            (PBYTE)buffer,
            buffersize,
            &buffersize))
        {
            if (GetLastError() == ERROR_INVALID_DATA)
            {
                //
                // May be a Legacy Device with no HardwareID. Continue.
                //
                break;
            }
            else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                //
                // We need to change the buffer size.
                //
                if (buffer)
                    LocalFree(buffer);
                buffer = (char *)LocalAlloc(LPTR,buffersize);
            }
            else
            {
                goto cleanup_DeviceInfo;
            }
        }

        if (GetLastError() == ERROR_INVALID_DATA)
            continue;
		
		CString strFindVid;
		strFindVid.Format("%s", buffer);
		int iFind = strFindVid.Find("USB\\Vid_0c45");

		if (iFind != -1)
		{
			if(!SetupDiGetDeviceInstanceId(DeviceInfoSet,   &DeviceInfoData,   buffer,   buffersize,   NULL))  
			  return   FALSE; 
			
			m_chInstanceID = new char[buffersize];
			StringCchCopy(m_chInstanceID, buffersize, buffer);
			
			m_chHardwareID = new char[buffersize];
			StringCchCopy(m_chHardwareID, buffersize, strFindVid);
		}

       if (buffer) LocalFree(buffer);
        if (Found) break;
    }

    if (GetLastError() != NO_ERROR)
    {
      //   DisplayError(TEXT("EnumDeviceInfo"));
    }

    //
    //  Cleanup.
    //
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);
	
	if (m_chInstanceID)
	{
		return TRUE;
	}

    return FALSE; //???
}

⌨️ 快捷键说明

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