Results 1 to 14 of 14

Thread: disable or enable a hardware

Hybrid View

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    19

    Re: disable or enable a hardware

    Reopening this thread as no response has solved the issue.

    Well, I've been working with a serial (RS232) connection and facing denied access to the communication port while debugging the code in the IDE. This issue is caused by a problem in the code that fails to close the COM port before stopping. As a temporary solution, I've had to close the IDE in order to release the COM port.

    I was considering that once the error code is captured, I could disable/remove the serial port and then enable/reinstall it again. By doing so, the need to close the IDE would no longer be necessary.

    So, how to perform this task through code ?

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,658

    Re: disable or enable a hardware

    If it can be disabled via device manager it can indeed be disabled by code... however some brief research on this topic suggests it is not possible on a 64bit machine through the WOW64 layer (i.e. from a 32bit app). You *could* get around this by calling the 64bit DLLs, but it's very, very difficult. Another option would be twinBASIC, which wasn't around in 2006 when this question was asked.

    Here's an example of how to do it in C#: https://stackoverflow.com/questions/...disable-device

    And here's the relevant APIs you'd need in 64bit VBA/twinBASIC:

    Code:
    Public Enum Setup_DIGCF
        DIGCF_DEFAULT  = &H00000001  ' only valid with DIGCF_DEVICEINTERFACE
        DIGCF_PRESENT  = &H00000002
        DIGCF_ALLCLASSES  = &H00000004
        DIGCF_PROFILE  = &H00000008
        DIGCF_DEVICEINTERFACE  = &H00000010
    End Enum
    
    Public Type SP_DEVINFO_DATA
        cbSize As Long
        ClassGuid As UUID
        DevInst As Long
        Reserved As LongPtr
    End Type
    
    Public Enum Setup_DICSF
        DICS_FLAG_GLOBAL  = &H00000001  ' make change in all hardware profiles
        DICS_FLAG_CONFIGSPECIFIC  = &H00000002  ' make change in specified profile only
        DICS_FLAG_CONFIGGENERAL  = &H00000004  ' 1 or more hardware profile-specific
    '  changes to follow.
    End Enum
    
    
    Public Enum DI_FUNCTION
        DIF_SELECTDEVICE  = &H00000001
        DIF_INSTALLDEVICE  = &H00000002
        DIF_ASSIGNRESOURCES  = &H00000003
        DIF_PROPERTIES  = &H00000004
        DIF_REMOVE  = &H00000005
        DIF_FIRSTTIMESETUP  = &H00000006
        DIF_FOUNDDEVICE  = &H00000007
        DIF_SELECTCLASSDRIVERS  = &H00000008
        DIF_VALIDATECLASSDRIVERS  = &H00000009
        DIF_INSTALLCLASSDRIVERS  = &H0000000A
        DIF_CALCDISKSPACE = &H0000000B
        DIF_DESTROYPRIVATEDATA  = &H0000000C
        DIF_VALIDATEDRIVER  = &H0000000D
        DIF_MOVEDEVICE = &H0000000E
        DIF_DETECT  = &H0000000F
        DIF_INSTALLWIZARD  = &H00000010
        DIF_DESTROYWIZARDDATA  = &H00000011
        DIF_PROPERTYCHANGE  = &H00000012
        DIF_ENABLECLASS  = &H00000013
        DIF_DETECTVERIFY  = &H00000014
        DIF_INSTALLDEVICEFILES  = &H00000015
        DIF_UNREMOVE  = &H00000016
        DIF_SELECTBESTCOMPATDRV  = &H00000017
        DIF_ALLOW_INSTALL  = &H00000018
        DIF_REGISTERDEVICE  = &H00000019
        DIF_NEWDEVICEWIZARD_PRESELECT  = &H0000001A
        DIF_NEWDEVICEWIZARD_SELECT  = &H0000001B
        DIF_NEWDEVICEWIZARD_PREANALYZE  = &H0000001C
        DIF_NEWDEVICEWIZARD_POSTANALYZE  = &H0000001D
        DIF_NEWDEVICEWIZARD_FINISHINSTALL  = &H0000001E
        DIF_UNUSED1  = &H0000001F
        DIF_INSTALLINTERFACES  = &H00000020
        DIF_DETECTCANCEL  = &H00000021
        DIF_REGISTER_COINSTALLERS  = &H00000022
        DIF_ADDPROPERTYPAGE_ADVANCED  = &H00000023
        DIF_ADDPROPERTYPAGE_BASIC  = &H00000024
        DIF_RESERVED1  = &H00000025
        DIF_TROUBLESHOOTER  = &H00000026
        DIF_POWERMESSAGEWAKE  = &H00000027
        DIF_ADDREMOTEPROPERTYPAGE_ADVANCED  = &H00000028
        DIF_UPDATEDRIVER_UI  = &H00000029
        DIF_FINISHINSTALL_ACTION  = &H0000002A
        DIF_RESERVED2  = &H00000030
    End Enum
    
    Public Type SP_CLASSINSTALL_HEADER
        cbSize As Long
        InstallFunction As DI_FUNCTION
    End Type
    
    Public Enum SetupDeviceStateChange
        DICS_ENABLE  = &H00000001
        DICS_DISABLE  = &H00000002
        DICS_PROPCHANGE  = &H00000003
        DICS_START  = &H00000004
        DICS_STOP  = &H00000005
    End Enum
    
    Public Type SP_PROPCHANGE_PARAMS
        ClassInstallHeader As SP_CLASSINSTALL_HEADER
        StateChange As SetupDeviceStateChange
        Scope As Setup_DICSF
        HwProfile As Long
    End Type
    
    
    Public DeclareWide PtrSafe Function SetupDiGetClassDevsW Lib "setupapi" (ClassGuid As UUID, ByVal Enumerator As String, ByVal hWndParent As LongPtr, ByVal Flags As Setup_DIGCF) As LongPtr
    Public DeclareWide PtrSafe Function SetupDiEnumDeviceInfo Lib "setupapi" (ByVal DeviceInfoSet As LongPtr, ByVal MemberIndex As Long, ByRef DeviceInfoData As SP_DEVINFO_DATA) As BOOL
    Public DeclareWide PtrSafe Function SetupDiDestroyDeviceInfoList Lib "setupapi" (ByVal DeviceInfoSet As LongPtr) As BOOL
    Public DeclareWide PtrSafe Function SetupDiGetDeviceInstanceIdW Lib "setupapi" (ByVal DeviceInfoSet As LongPtr, DeviceInfoData As SP_DEVINFO_DATA, ByVal DeviceInstanceId As LongPtr, ByVal DeviceInstanceSize As Long, RequiredSize As Long) As BOOL
    Public DeclareWide PtrSafe Function SetupDiSetClassInstallParamsW Lib "setupapi" (ByVal DeviceInfoSet As LongPtr, DeviceInfoData As SP_DEVINFO_DATA, ClassInstallParams As Any, ByVal ClassInstallParamsSize As Long) As BOOL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width