[SETUPAPI]
[reactos.git] / reactos / dll / win32 / setupapi / interface.c
index 37fa07c..1d20709 100644 (file)
@@ -255,6 +255,7 @@ SETUP_CreateInterfaceList(
             rc = RegOpenKeyExW(hReferenceKey, Control, 0, KEY_QUERY_VALUE, &hControlKey);
             if (rc != ERROR_SUCCESS)
             {
+#if 0
                 if (OnlyPresentInterfaces)
                 {
                     DestroyDeviceInterface(interfaceInfo);
@@ -262,6 +263,7 @@ SETUP_CreateInterfaceList(
                 }
                 else
                     interfaceInfo->Flags |= SPINT_REMOVED;
+#endif
             }
             else
             {
@@ -290,22 +292,114 @@ cleanup:
     return rc;
 }
 
+static LPWSTR
+CreateSymbolicLink(
+    IN LPGUID InterfaceGuid,
+    IN LPCWSTR ReferenceString,
+    IN struct DeviceInfo *devInfo)
+{
+    DWORD Length, Index, Offset;
+    LPWSTR Key;
+
+    Length = wcslen(devInfo->instanceId) + 4 /* prepend ##?# */ + 41 /* #{GUID} + */ + 1 /* zero byte */;
+
+    Key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Length * sizeof(WCHAR));
+    if (!Key)
+        return NULL;
+
+    wcscpy(Key, L"##?#");
+    wcscat(Key, devInfo->instanceId);
+
+    for(Index = 4; Index < Length; Index++)
+    {
+        if (Key[Index] == L'\\')
+        {
+            Key[Index] = L'#';
+        }
+    }
+
+    wcscat(Key, L"#");
+
+    Offset = wcslen(Key);
+    pSetupStringFromGuid(InterfaceGuid, Key + Offset, Length - Offset);
+
+    return Key;
+}
+
+
 static BOOL
 InstallOneInterface(
     IN LPGUID InterfaceGuid,
     IN LPCWSTR ReferenceString,
     IN LPCWSTR InterfaceSection,
-    IN UINT InterfaceFlags)
+    IN UINT InterfaceFlags,
+    IN HINF hInf,
+    IN HDEVINFO DeviceInfoSet,
+    IN struct DeviceInfo *devInfo)
 {
+    HKEY hKey, hRefKey;
+    LPWSTR Path;
+    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
+    struct DeviceInterface *DevItf = NULL;
+
     if (InterfaceFlags != 0)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
-    FIXME("Need to InstallOneInterface(%s %s %s %u)\n", debugstr_guid(InterfaceGuid),
-        debugstr_w(ReferenceString), debugstr_w(InterfaceSection), InterfaceFlags);
-    return TRUE;
+    TRACE("Need to InstallOneInterface(%s %s %s %u) hInf %p DeviceInfoSet %p devInfo %p instanceId %s\n", debugstr_guid(InterfaceGuid),
+        debugstr_w(ReferenceString), debugstr_w(InterfaceSection), InterfaceFlags, hInf, DeviceInfoSet, devInfo, debugstr_w(devInfo->instanceId));
+
+
+    Path = CreateSymbolicLink(InterfaceGuid, ReferenceString, devInfo);
+    if (!Path)
+        return FALSE;
+
+    CreateDeviceInterface(devInfo, Path, InterfaceGuid, &DevItf);
+    HeapFree(GetProcessHeap(), 0, Path);
+    if (!DevItf)
+    {
+        return FALSE;
+    }
+
+    memcpy(&DeviceInterfaceData.InterfaceClassGuid, &DevItf->InterfaceClassGuid, sizeof(GUID));
+    DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
+    DeviceInterfaceData.Flags = DevItf->Flags;
+    DeviceInterfaceData.Reserved = (ULONG_PTR)DevItf;
+
+    hKey = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet, &DeviceInterfaceData, 0, KEY_ALL_ACCESS, NULL, 0);
+    HeapFree(GetProcessHeap(), 0, DevItf);
+    if (hKey == INVALID_HANDLE_VALUE)
+    {
+        return FALSE;
+    }
+
+    if (ReferenceString)
+    {
+        Path = HeapAlloc(GetProcessHeap(), 0, (wcslen(ReferenceString) + 2) * sizeof(WCHAR));
+        if (!Path)
+        {
+            RegCloseKey(hKey);
+            return FALSE;
+        }
+
+        wcscpy(Path, L"#");
+        wcscat(Path, ReferenceString);
+
+        if (RegCreateKeyExW(hKey, Path, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hRefKey, NULL) != ERROR_SUCCESS)
+        {
+            ERR("failed to create key %s %lx\n", debugstr_w(Path), GetLastError());
+            HeapFree(GetProcessHeap(), 0, Path);
+            return FALSE;
+        }
+
+        RegCloseKey(hKey);
+        hKey = hRefKey;
+        HeapFree(GetProcessHeap(), 0, Path);
+    }
+
+    return SetupInstallFromInfSectionW(NULL, /* FIXME */ hInf, InterfaceSection, SPINST_REGISTRY, hKey, NULL, 0, NULL, NULL, NULL, NULL);
 }
 
 /***********************************************************************
@@ -329,11 +423,14 @@ SetupDiInstallDeviceInterfaces(
         SetLastError(ERROR_INVALID_HANDLE);
     else if (!DeviceInfoData)
         SetLastError(ERROR_INVALID_PARAMETER);
+    else if (DeviceInfoData && DeviceInfoData->Reserved == 0)
+        SetLastError(ERROR_INVALID_USER_BUFFER);
     else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
         SetLastError(ERROR_INVALID_USER_BUFFER);
     else
     {
-        struct DriverInfoElement *SelectedDriver;
+        struct DeviceInfo *devInfo;
+        struct DriverInfoElement *SelectedDriver = NULL;
         SP_DEVINSTALL_PARAMS_W InstallParams;
         WCHAR SectionName[MAX_PATH];
         DWORD SectionNameLength = 0;
@@ -345,6 +442,8 @@ SetupDiInstallDeviceInterfaces(
         GUID InterfaceGuid;
         BOOL Result;
 
+        devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
+
         InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
         Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
         if (!Result)
@@ -400,11 +499,15 @@ SetupDiInstallDeviceInterfaces(
 
             ret = GetStringField(&ContextInterface, 3, &InterfaceSection);
             if (!ret)
-                goto cleanup;
+            {
+                /* ReferenceString is optional */
+                InterfaceSection = ReferenceString;
+                ReferenceString = NULL;
+            }
 
             ret = SetupGetIntField(
                 &ContextInterface,
-                4, /* Field index */
+                (ReferenceString ? 4 : 3), /* Field index */
                 &InterfaceFlags);
             if (!ret)
             {
@@ -419,11 +522,12 @@ SetupDiInstallDeviceInterfaces(
             }
 
             /* Install Interface */
-            ret = InstallOneInterface(&InterfaceGuid, ReferenceString, InterfaceSection, InterfaceFlags);
+            ret = InstallOneInterface(&InterfaceGuid, ReferenceString, InterfaceSection, InterfaceFlags, SelectedDriver->InfFileDetails->hInf, DeviceInfoSet, devInfo);
 
 cleanup:
             MyFree(InterfaceGuidString);
-            MyFree(ReferenceString);
+            if (ReferenceString)
+                MyFree(ReferenceString);
             MyFree(InterfaceSection);
             InterfaceGuidString = ReferenceString = InterfaceSection = NULL;
             Result = SetupFindNextMatchLineW(&ContextInterface, AddInterface, &ContextInterface);
@@ -433,3 +537,87 @@ cleanup:
     TRACE("Returning %d\n", ret);
     return ret;
 }
+
+HKEY WINAPI
+SetupDiOpenDeviceInterfaceRegKey(
+    IN HDEVINFO  DeviceInfoSet, IN PSP_DEVICE_INTERFACE_DATA  DeviceInterfaceData, IN DWORD  Reserved, IN REGSAM  samDesired)
+{
+    HKEY hKey = INVALID_HANDLE_VALUE, hDevKey;
+    struct DeviceInfoSet * list;
+
+    TRACE("%p %p %p 0x%08x 0x%08x)\n", DeviceInfoSet, DeviceInterfaceData, Reserved, samDesired);
+
+    if (!DeviceInfoSet)
+        SetLastError(ERROR_INVALID_PARAMETER);
+    else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
+        SetLastError(ERROR_INVALID_HANDLE);
+    else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEVICE_INFO_SET_MAGIC)
+        SetLastError(ERROR_INVALID_HANDLE);
+    else if (!DeviceInterfaceData)
+        SetLastError(ERROR_INVALID_PARAMETER);
+    else if (DeviceInterfaceData && DeviceInterfaceData->Reserved == 0)
+        SetLastError(ERROR_INVALID_USER_BUFFER);
+    else if (DeviceInterfaceData && DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
+        SetLastError(ERROR_INVALID_USER_BUFFER);
+    else
+    {
+        struct DeviceInterface *DevItf;
+        LPWSTR Path, Guid, Slash;
+        DWORD Length;
+        DevItf = (struct DeviceInterface *)DeviceInterfaceData->Reserved;
+
+        Length = wcslen(DevItf->SymbolicLink);
+
+        Path = HeapAlloc(GetProcessHeap(), 0, (Length+2) * sizeof(WCHAR));
+        if (!Path)
+        {
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return INVALID_HANDLE_VALUE;
+        }
+
+        wcscpy(Path, DevItf->SymbolicLink);
+
+        Guid = wcsrchr(Path, '}');
+        Slash = wcsrchr(Path, '\\');
+        if (!Guid || !Slash)
+        {
+            SetLastError(ERROR_INVALID_PARAMETER);
+            return INVALID_HANDLE_VALUE;
+        }
+
+        if ((ULONG_PTR)Slash > (ULONG_PTR)Guid)
+        {
+            /* Create an extra slash */
+            memmove(Slash+1, Slash, (wcslen(Slash) + 1) * sizeof(WCHAR));
+            Slash[1] = L'#';
+        }
+
+        Guid = Path;
+        while((ULONG_PTR)Guid < (ULONG_PTR)Slash)
+        {
+            if (*Guid == L'\\')
+                *Guid = L'#';
+
+            Guid++;
+        }
+
+        hKey = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid, samDesired, DIOCR_INTERFACE, NULL, NULL);
+        if (hKey != INVALID_HANDLE_VALUE)
+        {
+            if (RegOpenKeyExW(hKey, Path, 0, samDesired, &hDevKey) == ERROR_SUCCESS)
+            {
+                RegCloseKey(hKey);
+                hKey = hDevKey;
+            }
+            else
+            {
+                RegCloseKey(hKey);
+                hKey = INVALID_HANDLE_VALUE;
+            }
+        }
+
+        HeapFree(GetProcessHeap(), 0, Path);
+    }
+
+    return hKey;
+}