2
\$\begingroup\$

Now I have this funny program. I bought an ASUS ZenBook which includes NumberPad. The downside of it is that it turns on with a slightest touch on its upper right corner so that I start write numeric gibberish in my code.

This one, disables the NumberPad (must be run as the admin):

#pragma comment (lib, "Setupapi.lib")

#include <windows.h>
#include <setupapi.h>
#include <iostream>

int main() {
    GUID guid;
    HRESULT hResult =
        CLSIDFromString(
            L"{4d36e96b-e325-11ce-bfc1-08002be10318}",
            (LPCLSID) &guid);

    if (hResult == CO_E_CLASSSTRING) {
        std::cerr << "ERROR: Bad GUID string.\n";
        return EXIT_FAILURE;
    }

    HDEVINFO hDeviceInfo =
        SetupDiGetClassDevsExA(
            &guid,
            NULL,
            NULL,
            DIGCF_PRESENT,
            NULL,
            NULL,
            NULL);

    if (hDeviceInfo == INVALID_HANDLE_VALUE) {
        std::cerr << "ERROR: Could not obtain HDEVINFO.\n";
        return EXIT_FAILURE;
    }
    
    SP_DEVINFO_DATA deviceInfoData;
    deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    deviceInfoData.ClassGuid = guid;
    
    BOOL deviceEnumerated = 
        SetupDiEnumDeviceInfo(
            hDeviceInfo, 
            0,
            &deviceInfoData);

    if (!deviceEnumerated) {
        std::cerr << "ERROR: Could not enumerate the SP_DEVINFO_DATA.\n";
        return EXIT_FAILURE;
    }

    BOOL removeStatus =
        SetupDiCallClassInstaller(
            DIF_REMOVE,
            hDeviceInfo, 
            &deviceInfoData);

    if (!removeStatus) {
        std::cerr << "ERROR: Could not remove the device.\n";
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Critique request

So, how could I improve it?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Low hanging fruits:

  • L"{4d36e96b-e325-11ce-bfc1-08002be10318}" looks like a magic number to me. If I understand correctly, it is a GUID of NumberPad; if so, make it a properly named constant.

    Aside, it looks like this program has a capability to disable other features (just supply a different GUID). Consider passing a GUID via a command line.

  • It would be nice to have an option to re-enable a disabled feature.

  • Error reporting is sloppy. Consider using GetLastError.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.