How to programmatically unplug & replug an arbitrary USB device?

后端 未结 13 1741
南笙
南笙 2020-12-07 19:22

I\'m trying to fix a non-responsive USB device that\'s masquerading as a virtual COM port. Manual replugging works, but there may be up to 12 of these units. Is there an API

13条回答
  •  无人及你
    2020-12-07 19:49

    You can use the C# Hardware Helper Lib and add the ResetDevice function.

    public bool ResetDevice( IntPtr hDevInfo, IntPtr devInfoData )  
    // Need to add  
    // public const int DICS_PROPCHANGE = ((0x00000003));   
    // at the public class Native under //PARMS  
    int szOfPcp;  
    IntPtr ptrToPcp;  
    int szDevInfoData;  
    IntPtr ptrToDevInfoData;  
    
    Native.SP_PROPCHANGE_PARAMS pcp = new Native.SP_PROPCHANGE_PARAMS();  
    pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Native.SP_CLASSINSTALL_HEADER));  
    pcp.ClassInstallHeader.InstallFunction = Native.DIF_PROPERTYCHANGE;  
    pcp.StateChange = Native.DICS_PROPCHANGE; // for reset  
    pcp.Scope = Native.DICS_FLAG_CONFIGSPECIFIC;  
    pcp.HwProfile = 0;  
    
    szOfPcp = Marshal.SizeOf(pcp);  
    ptrToPcp = Marshal.AllocHGlobal(szOfPcp);  
    Marshal.StructureToPtr(pcp, ptrToPcp, true);  
    szDevInfoData = Marshal.SizeOf(devInfoData);  
    ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);  
    Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);  
    
    bool rslt1 = Native.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp,   Marshal.SizeOf(typeof(Native.SP_PROPCHANGE_PARAMS)));  
    bool rstl2 = Native.SetupDiCallClassInstaller(Native.DIF_PROPERTYCHANGE, hDevInfo,   ptrToDevInfoData);  
    
    if (rslt1 && rstl2)  
    {  
        return true;  
    }  
    return false;  
    }  
    

提交回复
热议问题