mercredi 2 septembre 2015

Designing a Win32api app

I'm pretty new to C++ and the more I read the more questions I have.

I want to convert an old C code of mine, which lists the devices on a machine, to a well designed C++ code that does the same.

As I have read, one of the most basic concept of C++ is isolation of Containers, Iterators and Algorithms. That said, I came into conclusion that I should create 3 classes:

  1. DeviceInterface - Holds a single interface info such as SP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA and SP_DEVINFO_DATA

  2. DevicesInterfaceContainer - Container that somehow holds all the devices information in form of DeviceInterface classes.

  3. DeviceInterfaceIterator - Used by the algorithm to iterate over the items in the DevicesInterfaceContainer.

I didn't manage to implement it because I wasn't sure how.

  1. Should the iterator be an output iterator that initializes the DevicesInterfaceContainer? Or whether the DevicesInterfaceContainer should initialize and acquire all the devices by its own?

  2. What happens when an Win32 api fails in the constructor? Should I throw an exception? if so, what kind of exception? std::exception? std::runtime_error? or should I define a Win32Exception of my own that holds information about the LastError? (All I need is the last error in order to format the message.)

Something like:

class DeviceInterfacesList
{
public:
    DeviceInterfacesList(GUID& rtInterfaceGuid)
    {
        m_hDevInfo = SetupDiGetClassDevs(
            &rtInterfaceGuid,
            NULL,
            NULL,
            DIGCF_DEVICEINTERFACE
        );
        if (INVALID_HANDLE_VALUE == m_hDevInfo)
        {
            throw Win32Exception();
        }
    }

    ...
    ...
    ...

    HDEVINFO m_hDevInfo;
};

I will be glad to get some guidance / good updated examples for using STL + win32api or anything that will clarify my questions.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire