Invalid Handle error when trying to NtQueryObject from midi device

75 views Asked by At

I'm trying to make a utility library to grab information for certain devices, and one of the things I want it to do is backtrack from an open midi device to find its information from the device tree. The docs seem to indicate that I can cast a midi handle into a standard handle as indicated in MidiInGetID which i was exploring as an alternative avenue:

The midiInGetID function gets the device identifier for the given MIDI input device.

This function is supported for backward compatibility. New applications can cast a handle of the device rather than retrieving the device identifier.

but when I try to ntquery the cast handle, I get back the error 0xc0000008 which ntstatus.h tells me is an "Invalid Handle" error. What am I missing here? and is there an alternate method that gets the result I actually want such as through MidiInGetID? I'm not sure how to use the puDeviceId that I would get back. if I can get the device's PWSTR device id, then I can use CfgMgr32 to query any of its information that I need, but the problem is making the jump from a HMIDIIN/OUT to the device's PWSTR name

int main()
{
    //LPHMIDIIN phmi;
    UINT uDeviceID = midiInGetNumDevs();
    PUBLIC_OBJECT_BASIC_INFORMATION info;
    PULONG rlen = 0;
    HANDLE hnd;
    UINT did = 0;
    NTSTATUS ec;

    HMIDIIN midihandle = 0;
    MMRESULT mmstat = 0;
    std::cout << "mididevs: " << uDeviceID;
    for (int i = 0; i < uDeviceID; i++) {
        //(DWORD_PTR)&midiproc
        mmstat = midiInOpen(&midihandle, i, (DWORD_PTR)(void*)midiproc, 0, CALLBACK_FUNCTION);
        if (mmstat != MMSYSERR_NOERROR) {
            std::cout << "\nerror: " << mmstat;
            continue;
        }

        std::cout << "\n" << midihandle;
        ec = NtQueryObject((HANDLE)midihandle, ObjectBasicInformation, &info, sizeof info, rlen);
        if (ec != STATUS_SUCCESS) {
            std::cout << "\nstatus: " << std::hex<<(ULONG)ec;
            continue;
        }
        //QueryDosDevice()
    }
    //GetDevicePropertiesCfgmgr32();
    return 0;
}

any ideas what I'm missing?

(HANDLE)midihandle should theoretically cast properly, but I get invalid handle in the NtQueryObject call

0

There are 0 answers