1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* PROJECT:     ReactOS Device Manager
* LICENSE:     GPL - See COPYING in the top level directory
* FILE:        dll/win32/devmgr/devmgmt/ClassNode.cpp
* PURPOSE:     Class object for 
* COPYRIGHT:   Copyright 2015 Ged Murphy <gedmurphy@reactos.org>
*
*/

#include "precomp.h"
#include "devmgmt.h"
#include "ClassNode.h"


CClassNode::CClassNode(
    _In_ LPGUID ClassGuid,
    _In_ PSP_CLASSIMAGELIST_DATA ImageListData
    ) :
    CNode(ClassNode, ImageListData)
{
    CopyMemory(&m_ClassGuid, ClassGuid, sizeof(GUID));
}


CClassNode::~CClassNode()
{
}


bool
CClassNode::SetupNode()
{
    DWORD RequiredSize, Type, Size;
    DWORD Success;
    HKEY hKey;

    // Open the registry key for this class
    hKey = SetupDiOpenClassRegKeyExW(&m_ClassGuid,
                                     MAXIMUM_ALLOWED,
                                     DIOCR_INSTALLER,
                                     NULL,
                                     0);
    if (hKey != INVALID_HANDLE_VALUE)
    {
        // Lookup the class description (win7+)
        Size = sizeof(m_DisplayName);
        Success = RegQueryValueExW(hKey,
                                   L"ClassDesc",
                                   NULL,
                                   &Type,
                                   (LPBYTE)m_DisplayName,
                                   &Size);
        if (Success == ERROR_SUCCESS)
        {
            if (Type != REG_SZ)
            {
                Success = ERROR_INVALID_DATA;
            }
            // Check if the string starts with an @
            else if (m_DisplayName[0] == L'@')
            {
                // The description is located in a module resource
                Success = ConvertResourceDescriptorToString(m_DisplayName, sizeof(m_DisplayName));
            }
        }
        else if (Success == ERROR_FILE_NOT_FOUND)
        {
            // WinXP stores the description in the default value
            Size = sizeof(m_DisplayName);
            Success = RegQueryValueExW(hKey,
                                       NULL,
                                       NULL,
                                       &Type,
                                       (LPBYTE)m_DisplayName,
                                       &Size);
            if (Success == ERROR_SUCCESS && Type != REG_SZ)
            {
                Success = ERROR_INVALID_DATA;
            }
        }

        // Close the registry key
        RegCloseKey(hKey);
    }
    else
    {
        Success = GetLastError();
    }

    // Check if we failed to get the class description
    if (Success != ERROR_SUCCESS)
    {
        // Use the class name as the description
        RequiredSize = _countof(m_DisplayName);
        (VOID)SetupDiClassNameFromGuidW(&m_ClassGuid,
                                        m_DisplayName,
                                        RequiredSize,
                                        &RequiredSize);
    }

    // Get the image index for this class
    (VOID)SetupDiGetClassImageIndex(m_ImageListData,
                                    &m_ClassGuid,
                                    &m_ClassImage);

    return true;
}


DWORD
CClassNode::ConvertResourceDescriptorToString(
    _Inout_z_ LPWSTR ResourceDescriptor,
    _In_ DWORD ResourceDescriptorSize
    )
{
    WCHAR ModulePath[MAX_PATH];
    WCHAR ResString[256];
    INT ResourceId;
    HMODULE hModule;
    LPWSTR ptr;
    DWORD Size;
    DWORD dwError;

    // First check for a semi colon */
    ptr = wcschr(ResourceDescriptor, L';');
    if (ptr)
    {
        // This must be an inf based descriptor, the desc is after the semi colon
        StringCbCopyW(ResourceDescriptor, ResourceDescriptorSize, ++ptr);
        dwError = ERROR_SUCCESS;
    }
    else
    {
        // This must be a dll resource based descriptor. Find the comma
        ptr = wcschr(ResourceDescriptor, L',');
        if (ptr == NULL)
            return ERROR_INVALID_DATA;

        // Terminate the string where the comma was
        *ptr = UNICODE_NULL;<--- *ptr is assigned

        // Expand any environment strings
        Size = ExpandEnvironmentStringsW(&ResourceDescriptor[1], ModulePath, MAX_PATH);
        if (Size > MAX_PATH)
            return ERROR_BUFFER_OVERFLOW;
        if (Size == 0)
            return GetLastError();

        // Put the comma back and move past it
        *ptr = L',';<--- *ptr is overwritten
        ptr++;

        // Load the dll
        hModule = LoadLibraryExW(ModulePath, NULL, LOAD_LIBRARY_AS_DATAFILE);
        if (hModule == NULL)
            return GetLastError();

        // Convert the resource id to a number
        ResourceId = _wtoi(ptr);

        // If the number is negative, make it positive
        if (ResourceId < 0)
            ResourceId = -ResourceId;

        // Load the string from the dll
        if (LoadStringW(hModule, ResourceId, ResString, 256))
        {
            StringCbCopyW(ResourceDescriptor, ResourceDescriptorSize, ResString);
            dwError = ERROR_SUCCESS;
        }
        else
        {
            dwError = GetLastError();
        }

        // Free the library
        FreeLibrary(hModule);
    }

    return dwError;
}