不用点击任务栏USB装置图标,如何用Win API编写移除USB图标列表中某一USB设备?

解决方案 »

  1.   

    很easy,用windows setup api可以实现,具体代码可以看Waltoney的书的代码
      

  2.   

    #include <stdio.h> 
    #include <tchar.h> // Make all functions UNICODE safe.
    #include <windows.h>  
    #include <setupapi.h> // for SetupDiXxx functions.
    int DisplayError(TCHAR * ErrorName)
    /*++
    Routine Description:    This Routine will display the LastError in human readable 
        form when possible.    If the return value is a 32-bit number, and falls in the range:
            ERROR_NO_ASSOCIATED_CLASS   0xE0000200 
        To
            ERROR_CANT_REMOVE_DEVINST   0xE0000232 
        The values defined in setupapi.h can help to determine the error.
        Start by searching for the text string ERROR_NO_ASSOCIATED_CLASS.
      
    Arguments:
        
        ErrorName: Human readable description of the last Function called.Return Value:
        
        Allways returns FALSE.
          
    --*/
    {
        DWORD Err = GetLastError();
        LPVOID lpMessageBuffer = NULL;
        
        if (FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
            NULL, 
            Err,  
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMessageBuffer,  
            0,  
            NULL ))
            _tprintf(TEXT("%s FAILURE: %s\n"),ErrorName,(TCHAR *)lpMessageBuffer);
        else 
            _tprintf(TEXT("%s FAILURE: (0x%08x)\n"),ErrorName,Err);
        
        if (lpMessageBuffer) LocalFree( lpMessageBuffer ); // Free system buffer 
        
        SetLastError(Err);    
        return FALSE;
    }int main(int argc, char **argv)
    /*++
    Routine Discription:    Entry point to Remove.exe.
        Parse the command line, call subroutines.Arguments:
        
        Standard console 'c' application arguments.    argv[1] - PnP HardwareID of devices to remove.Return Value:
        
        Standard Console ERRORLEVEL values:    0 - Remove Successfull
        2 - Remove Failure.
        
    --*/
    {
        HDEVINFO DeviceInfoSet;
        SP_DEVINFO_DATA DeviceInfoData;
        DWORD i,err;    //
        // Verify the Arguments.
        //
        if (argc < 2)
        {
            _tprintf(TEXT("usage: remove <Hardware_ID>\n"));
            return 1; // Remove Failure
        }    //
        // Create a Device Information Set with all present devices.
        //
        DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
            0,
            0, 
            DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system
        if (DeviceInfoSet == INVALID_HANDLE_VALUE)
        {
            DisplayError(TEXT("GetClassDevs(All Present Devices)"));        
            return 1;
        }
        
        //
        //  Enumerate through all 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.
            //
    _tprintf("next\n");
            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.
                    //
                    DisplayError(TEXT("GetDeviceRegistryProperty"));
                    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))
            {
                _tprintf(TEXT("Compare device ID: [%s]\n"),p);

                if (!_tcscmp(argv[1],p))
                {
                    _tprintf(TEXT("Found! [%s]\n"),p);                //
                    // Worker function to remove device.
                    //
                    if (!SetupDiCallClassInstaller(DIF_REMOVE,
                        DeviceInfoSet,
                        &DeviceInfoData))
                    {
                        DisplayError(TEXT("CallClassInstaller(REMOVE)"));
                    }
                    break;
                }
            }        if (buffer) LocalFree(buffer);
        }    if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS))
        {
            DisplayError(TEXT("EnumDeviceInfo"));
        }
        
        //
        //  Cleanup.
        //    
    cleanup_DeviceInfo:
        err = GetLastError();
        SetupDiDestroyDeviceInfoList(DeviceInfoSet);
        
        return !(err == NO_ERROR); 
    }