What is wrong with Microsoft turning off USB to save power?

Is there a way to programmatically disable this (USB ports turning off automatically to save power) from JUCE?

I imagine there isn’t. Just hoping that this message somehow reaches Microsoft. Their USB device management is frustrating.

There’s nothing in JUCE for anything close to this.

Whatever your reasons are, you’re probably better off scanning StackOverflow. eg: c++ - Enable and disable USB port - Stack Overflow

Fair enough. Thanks.

I’ve found a way to detect disconnections of MIDI devices that accept input with a little modification in the JUCE code.

I’m not sure if this is something that can be implemented in Mac or Linux, but changing

void sendMessageNow (const MidiMessage& message) override
bool sendMessageNow (const MidiMessage& message) override

Would allow us to check if there has been an error sending the MIDI message whenever that is possible.

Here’s the Windows code to return false when there is an error:
(see juce\modules\juce_audio_devices\native\juce_win32_Midi.cpp)

bool sendMessageNow (const MidiMessage& message) override
{
    if (message.getRawDataSize() > 3 || message.isSysEx())
    {
        MIDIHDR h = {};

        h.lpData = (char*) message.getRawData();
        h.dwBytesRecorded = h.dwBufferLength  = (DWORD) message.getRawDataSize();

        if (midiOutPrepareHeader (han->handle, &h, sizeof (MIDIHDR)) == MMSYSERR_NOERROR)
        {
            auto res = midiOutLongMsg (han->handle, &h, sizeof (MIDIHDR));

            if (res == MMSYSERR_NOERROR)
            {
                while ((h.dwFlags & MHDR_DONE) == 0)
                    Sleep (1);

                int count = 500; // 1 sec timeout

                while (--count >= 0)
                {
                    res = midiOutUnprepareHeader (han->handle, &h, sizeof (MIDIHDR));

                    if (res == MIDIERR_STILLPLAYING)
                    {
                        Sleep (2);
                    }
                    else 
                    {
                      return true;
                    }
                }
            }
        }
    }
    else
    {
        for (int i = 0; i < 50; ++i)
        {
          auto res = midiOutShortMsg (han->handle, *(unsigned int*) message.getRawData());
          if ( res == MMSYSERR_NOERROR )
            return true;
          else if (res == MIDIERR_NOTREADY) {
            Sleep(1);
          }
          else {
            return false;
          }
        }
    }

    return false;
}

This allows me to send a bogus message every 1s to query the device. Whenever I detect an error, I close the device and re-open it. It works like a charm as a workaround for the disconnections that may happen in Windows machines (power saving policy, bad contact, etc).

Quick review: actually the constant querying of the device is preventing USB “power saving” disconnections, so I think I have found an effective workaround for this absurd Windows policy while also making the software robust to accidental disconnections of its MIDI devices.