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

📄 usb_install_2kxpdlg.cpp

📁 USB驱动在window xp/2K下的自动安装程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                (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
            {
                // Unknown Failure.
				str.Format("Error: SetupDiGetDeviceRegistryProperty unknown Failure");
				AfxMessageBox(str, MB_OK | MB_ICONQUESTION);
                goto cleanup_DeviceInfo;
            }            
        }
        
        if (GetLastError() == ERROR_INVALID_DATA) 
		{
            continue;
		}
        
        // Compare each entry in the buffer multi-sz list with our HardwareID.        
        for (p=buffer; (*p) && (p < &buffer[buffersize]); p += lstrlen(p) + sizeof(TCHAR))
        {            
            // convert the string to lowercase 
			strlwr(p); 

            if (!_tcscmp(HardwareId,p))
            {
                bFound = TRUE;
                break;
            }
        }
        
        if (buffer)
		{
			LocalFree(buffer);
		}

        if (bFound)
		{
			break;
		}
    }
    
    if (GetLastError() != NO_ERROR)
    {        
		str.Format("Error: EnumDeviceInfo Failure");
		AfxMessageBox(str, MB_OK | MB_ICONQUESTION); 
    }	
    
    //  Cleanup.    
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    SetLastError(err);
    
    return err == NO_ERROR; 
}

BOOL
InstallRootEnumeratedDriver(LPTSTR HardwareId, LPTSTR INFFile, PBOOL  RebootRequired)
{
    HDEVINFO DeviceInfoSet = 0;
    SP_DEVINFO_DATA DeviceInfoData;
    GUID ClassGUID;
    TCHAR ClassName[MAX_LEN];
    DWORD err;
    
	CString str;

    // Use the INF File to extract the Class GUID.     
    if (!SetupDiGetINFClass(INFFile,&ClassGUID,ClassName,sizeof(ClassName),0))
    {        
		str.Format("Error : SetupDiGetINFClass ");
		AfxMessageBox(str, MB_OK | MB_ICONQUESTION);

		return FALSE;
    }
        
    // Create the container for the to-be-created Device Information Element.    
    DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);
    if(DeviceInfoSet == INVALID_HANDLE_VALUE) 
    {        
		str.Format("Error : SetupDiCreateDeviceInfoList");
		AfxMessageBox(str, MB_OK | MB_ICONQUESTION);

		return FALSE;
    }
    
    // Now create the element. 
    // Use the Class GUID and Name from the INF file.    
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    if (!SetupDiCreateDeviceInfo(DeviceInfoSet,
        ClassName,
        &ClassGUID,
        NULL,
        0,
        DICD_GENERATE_ID,
        &DeviceInfoData))
    {        
		str.Format("Error : SetupDiCreateDeviceInfo");
		AfxMessageBox(str, MB_OK | MB_ICONQUESTION);

        goto cleanup_DeviceInfo;
    }
    
    // Add the HardwareID to the Device's HardwareID property.    
    if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,
        &DeviceInfoData,
        SPDRP_HARDWAREID,
        (LPBYTE)HardwareId,
        (lstrlen(HardwareId)+1+1)*sizeof(TCHAR)))	
    {        
		str.Format("Error : SetupDiSetDeviceRegistryProperty");
		AfxMessageBox(str, MB_OK | MB_ICONQUESTION);

        goto cleanup_DeviceInfo;
    }
        
    // Transform the registry element into an actual devnode 
    // in the PnP HW tree.    
    if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE,
        DeviceInfoSet,
        &DeviceInfoData))
    {        
		str.Format("Error : SetupDiCallClassInstaller");
		AfxMessageBox(str, MB_OK | MB_ICONQUESTION);

        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,
        HardwareId,
        INFFile,
        INSTALLFLAG_FORCE,
        RebootRequired))
    {
        DWORD err = GetLastError();
                
        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);
}

//Install USB Driver with INF file, but must get the Hardware ID first  
BOOL InstallDriver()
{    
    BOOL RebootRequired = 0;
	
	CString str;
    
	// Get the HardwareID from INF file, must be first do it.	
	if (!GetHardwareIDFromINF())
	{		
		return FALSE;
	}

	CString strInfFile(GetFilePath() + "\\iPOS_T700.inf");
    char *inf_file = (LPSTR)(LPCTSTR)strInfFile;

    // Look to see if this device already exists.	
	if (FindExistingDevice(HardwareID))
	{
		// No Need to Create a Device Node, just call our API.		
		if (!UpdateDriverForPlugAndPlayDevices(0,   // No Window Handle
			HardwareID,     // Hardware ID
			inf_file,       // INF FileName
			INSTALLFLAG_FORCE,
			&RebootRequired))
		{
			str.Format("Error : UpdateDriverForPlugAndPlayDevices");
			AfxMessageBox(str, MB_OK | MB_ICONQUESTION);
			return FALSE; 
		}
	}	
	else
	{
		if (GetLastError()!= ERROR_NO_MORE_ITEMS)
		{
			// An unknown failure from FindExistingDevice()
			str.Format("Error : An unknown failure from FindExistingDevice");
			AfxMessageBox(str, MB_OK | MB_ICONQUESTION);
			return FALSE; 
		}
		
		// Driver Does not exist, Create and call the API.
		// HardwareID must be a multi-sz string, which HardwareID is.		
		if (!InstallRootEnumeratedDriver(HardwareID, // HardwareID
			inf_file,   // INF FileName
			&RebootRequired))
		{	
			str.Format("Error : InstallRootEnumeratedDriver");
			AfxMessageBox(str, MB_OK | MB_ICONQUESTION);
			return FALSE; 
		}
	}
    
	return TRUE;
}

//Uninstall driver with Hardware ID
BOOL UnInstallDriver()
{
	HDEVINFO DeviceInfoSet;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i,err;

	CString str;
	
    // Create a Device Information Set with Special devices.    
    DeviceInfoSet = SetupDiGetClassDevs(NULL, // Special devices
        0,
        0, 
        DIGCF_ALLCLASSES | DIGCF_PRESENT ); // Special devices present on system
    if (DeviceInfoSet == INVALID_HANDLE_VALUE)
    {
        str.Format("GetClassDevs(Special Present Devices) Error");
        AfxMessageBox(str, MB_OK | MB_ICONQUESTION);
        return FALSE;
    }
    
    //  Enumerate through Devices.    
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
    {
        DWORD DataT;
        LPTSTR p,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
            {
                // Unknown Failure.                                
                goto cleanup_DeviceInfo;
            }            
        }

        if (GetLastError() == ERROR_INVALID_DATA) 
            continue;
                
        // Compare each entry in the buffer multi-sz list with our HardwareID.        
        for ( p=buffer; *p&&(p<&buffer[buffersize]); p+=lstrlen(p)+sizeof(TCHAR))
        {			
           	// convert the string to lowercase
            strlwr(p);
			
			if (!strcmp(HardwareID, p))
            {
           
                // Worker function to remove device.           
                if (!SetupDiCallClassInstaller(DIF_REMOVE,
                    DeviceInfoSet,
                    &DeviceInfoData))
                {
                    //_tprintf(TEXT("CallClassInstaller(REMOVE) error\n"));
					
                }				
				
                break;
            }
        }

        if (buffer)
		{
			LocalFree(buffer);
		}
    }

    if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS))
    {
        //_tprintf(TEXT("EnumDeviceInfo error"));
    }
    
    
//  Cleanup.        
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);    		
	SetLastError(err);

    return err == NO_ERROR; 
}

void CUsb_Install_2KXPDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if (m_pDlgProgress)
	{			
		delete m_pDlgProgress;
		m_pDlgProgress = NULL;
	}
	
}

void CUsb_Install_2KXPDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	g_iPos += 10;

	m_pDlgProgress->m_ctrl_Progress.SetPos(g_iPos);

	if ((m_bFinishThread) && (g_iPos >= 80)) 
	{
    	m_pDlgProgress->m_ctrl_Progress.SetPos(100);
		
		int ret = 0;
		m_pDlgProgress->EndDialog(ret);		
		
		KillTimer(m_nTimer);				
	}
	
	CDialog::OnTimer(nIDEvent);
}


void CUsb_Install_2KXPDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (m_pDlgProgress)
	{			
		delete m_pDlgProgress;
		m_pDlgProgress = NULL;
	}

	CDialog::OnClose();
}

⌨️ 快捷键说明

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