2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Configuration of network devices
4 * FILE: dll/win32/netcfgx/installer.c
5 * PURPOSE: Network devices installer
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
13 /* Append a REG_SZ to an existing REG_MULTI_SZ string in the registry.
14 * If the value doesn't exist, create it.
15 * Returns ERROR_SUCCESS if success. Otherwise, returns an error code
19 AppendStringToMultiSZ(
22 IN PCWSTR ValueToAppend
)
26 DWORD dwRequired
, dwLength
;
30 rc
= RegQueryValueExW(hKey
,
36 if (rc
!= ERROR_FILE_NOT_FOUND
)
38 if (rc
!= ERROR_SUCCESS
)
40 if (dwRegType
!= REG_MULTI_SZ
)
42 rc
= ERROR_GEN_FAILURE
;
46 dwTmp
= dwLength
= dwRequired
+ wcslen(ValueToAppend
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
47 Buffer
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
50 rc
= ERROR_NOT_ENOUGH_MEMORY
;
54 rc
= RegQueryValueExW(hKey
,
60 if (rc
!= ERROR_SUCCESS
)
65 dwRequired
= sizeof(WCHAR
);
66 dwLength
= wcslen(ValueToAppend
) * sizeof(WCHAR
) + 2 * sizeof(UNICODE_NULL
);
67 Buffer
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
70 rc
= ERROR_NOT_ENOUGH_MEMORY
;
75 /* Append the value */
76 wcscpy(&Buffer
[dwRequired
/ sizeof(WCHAR
) - 1], ValueToAppend
);
77 /* Terminate the REG_MULTI_SZ string */
78 Buffer
[dwLength
/ sizeof(WCHAR
) - 1] = UNICODE_NULL
;
80 rc
= RegSetValueExW(hKey
,
88 HeapFree(GetProcessHeap(), 0, Buffer
);
96 IN HDEVINFO DeviceInfoSet
,
97 IN PSP_DEVINFO_DATA DeviceInfoData
,
99 DWORD Characteristics
,
102 LPWSTR InstanceId
= NULL
;
103 LPWSTR DeviceName
= NULL
;
104 LPWSTR ExportName
= NULL
;
107 HKEY hNetworkKey
= NULL
;
108 HKEY hLinkageKey
= NULL
;
109 HKEY hConnectionKey
= NULL
;
110 DWORD dwShowIcon
, dwLength
, dwValue
;
113 /* Install the adapter */
114 if (!SetupDiInstallDevice(DeviceInfoSet
, DeviceInfoData
))
117 ERR("SetupDiInstallDevice() failed (Error %lu)\n", rc
);
121 /* Get Instance ID */
122 if (SetupDiGetDeviceInstanceIdW(DeviceInfoSet
, DeviceInfoData
, NULL
, 0, &dwLength
))
124 ERR("SetupDiGetDeviceInstanceIdW() returned TRUE. FALSE expected\n");
125 rc
= ERROR_GEN_FAILURE
;
129 InstanceId
= HeapAlloc(GetProcessHeap(), 0, dwLength
* sizeof(WCHAR
));
132 ERR("HeapAlloc() failed\n");
133 rc
= ERROR_NOT_ENOUGH_MEMORY
;
137 if (!SetupDiGetDeviceInstanceIdW(DeviceInfoSet
, DeviceInfoData
, InstanceId
, dwLength
, NULL
))
140 ERR("SetupDiGetDeviceInstanceIdW() failed with error 0x%lx\n", rc
);
144 /* Create device name */
145 DeviceName
= HeapAlloc(GetProcessHeap(), 0, (wcslen(L
"\\Device\\") + wcslen(UuidString
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
148 ERR("HeapAlloc() failed\n");
149 rc
= ERROR_NOT_ENOUGH_MEMORY
;
152 wcscpy(DeviceName
, L
"\\Device\\");
153 wcscat(DeviceName
, UuidString
);
155 /* Create export name */
156 ExportName
= HeapAlloc(GetProcessHeap(), 0, (wcslen(L
"\\Device\\Tcpip_") + wcslen(UuidString
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
159 ERR("HeapAlloc() failed\n");
160 rc
= ERROR_NOT_ENOUGH_MEMORY
;
163 wcscpy(ExportName
, L
"\\Device\\Tcpip_");
164 wcscat(ExportName
, UuidString
);
166 /* Write Tcpip parameters in new service Key */
167 rc
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, L
"SYSTEM\\CurrentControlSet\\Services", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_CREATE_SUB_KEY
, NULL
, &hKey
, NULL
);
168 if (rc
!= ERROR_SUCCESS
)
170 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
174 rc
= RegCreateKeyExW(hKey
, UuidString
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_CREATE_SUB_KEY
, NULL
, &hNetworkKey
, NULL
);
175 if (rc
!= ERROR_SUCCESS
)
177 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
183 rc
= RegCreateKeyExW(hNetworkKey
, L
"Parameters\\Tcpip", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_SET_VALUE
, NULL
, &hKey
, NULL
);
184 if (rc
!= ERROR_SUCCESS
)
186 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
189 RegCloseKey(hNetworkKey
);
192 rc
= RegSetValueExW(hKey
, L
"DefaultGateway", 0, REG_SZ
, (const BYTE
*)L
"0.0.0.0", (wcslen(L
"0.0.0.0") + 1) * sizeof(WCHAR
));
193 if (rc
!= ERROR_SUCCESS
)
195 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
199 rc
= RegSetValueExW(hKey
, L
"IPAddress", 0, REG_SZ
, (const BYTE
*)L
"0.0.0.0", (wcslen(L
"0.0.0.0") + 1) * sizeof(WCHAR
));
200 if (rc
!= ERROR_SUCCESS
)
202 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
206 rc
= RegSetValueExW(hKey
, L
"SubnetMask", 0, REG_SZ
, (const BYTE
*)L
"0.0.0.0", (wcslen(L
"0.0.0.0") + 1) * sizeof(WCHAR
));
207 if (rc
!= ERROR_SUCCESS
)
209 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
214 rc
= RegSetValueExW(hKey
, L
"EnableDHCP", 0, REG_DWORD
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
215 if (rc
!= ERROR_SUCCESS
)
217 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
223 /* Write 'Linkage' key in hardware key */
224 #if _WIN32_WINNT >= 0x502
225 hKey
= SetupDiOpenDevRegKey(DeviceInfoSet
, DeviceInfoData
, DICS_FLAG_GLOBAL
, 0, DIREG_DRV
, KEY_READ
| KEY_WRITE
);
227 hKey
= SetupDiOpenDevRegKey(DeviceInfoSet
, DeviceInfoData
, DICS_FLAG_GLOBAL
, 0, DIREG_DRV
, KEY_ALL_ACCESS
);
229 if (hKey
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
)
230 hKey
= SetupDiCreateDevRegKeyW(DeviceInfoSet
, DeviceInfoData
, DICS_FLAG_GLOBAL
, 0, DIREG_DRV
, NULL
, NULL
);
231 if (hKey
== INVALID_HANDLE_VALUE
)
235 ERR("SetupDiCreateDevRegKeyW() failed with error 0x%lx\n", rc
);
239 rc
= RegSetValueExW(hKey
, L
"NetCfgInstanceId", 0, REG_SZ
, (const BYTE
*)UuidString
, (wcslen(UuidString
) + 1) * sizeof(WCHAR
));
240 if (rc
!= ERROR_SUCCESS
)
242 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
246 rc
= RegSetValueExW(hKey
, L
"Characteristics", 0, REG_DWORD
, (const BYTE
*)&Characteristics
, sizeof(DWORD
));
247 if (rc
!= ERROR_SUCCESS
)
249 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
255 rc
= RegSetValueExW(hKey
, L
"BusType", 0, REG_SZ
, (const BYTE
*)BusType
, (wcslen(BusType
) + 1) * sizeof(WCHAR
));
256 if (rc
!= ERROR_SUCCESS
)
258 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
263 rc
= RegCreateKeyExW(hKey
, L
"Linkage", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_SET_VALUE
, NULL
, &hLinkageKey
, NULL
);
264 if (rc
!= ERROR_SUCCESS
)
266 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
270 rc
= RegSetValueExW(hLinkageKey
, L
"Export", 0, REG_SZ
, (const BYTE
*)DeviceName
, (wcslen(DeviceName
) + 1) * sizeof(WCHAR
));
271 if (rc
!= ERROR_SUCCESS
)
273 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
277 rc
= RegSetValueExW(hLinkageKey
, L
"RootDevice", 0, REG_SZ
, (const BYTE
*)UuidString
, (wcslen(UuidString
) + 1) * sizeof(WCHAR
));
278 if (rc
!= ERROR_SUCCESS
)
280 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
284 rc
= RegSetValueExW(hLinkageKey
, L
"UpperBind", 0, REG_SZ
, (const BYTE
*)L
"Tcpip", (wcslen(L
"Tcpip") + 1) * sizeof(WCHAR
));
285 if (rc
!= ERROR_SUCCESS
)
287 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
293 /* Write connection information in network subkey */
294 rc
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, L
"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_CREATE_SUB_KEY
, NULL
, &hNetworkKey
, NULL
);
295 if (rc
!= ERROR_SUCCESS
)
297 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
301 rc
= RegCreateKeyExW(hNetworkKey
, UuidString
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_CREATE_SUB_KEY
, NULL
, &hKey
, NULL
);
302 if (rc
!= ERROR_SUCCESS
)
304 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
308 rc
= RegCreateKeyExW(hKey
, L
"Connection", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_SET_VALUE
, NULL
, &hConnectionKey
, NULL
);
311 if (rc
!= ERROR_SUCCESS
)
313 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
317 if (!LoadStringW(netcfgx_hInstance
, IDS_NET_CONNECT
, szBuffer
, sizeof(szBuffer
)/sizeof(WCHAR
)))
319 wcscpy(szBuffer
, L
"Network Connection");
322 rc
= RegSetValueExW(hConnectionKey
, L
"Name", 0, REG_SZ
, (const BYTE
*)szBuffer
, (wcslen(szBuffer
) + 1) * sizeof(WCHAR
));
323 if (rc
!= ERROR_SUCCESS
)
325 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
329 rc
= RegSetValueExW(hConnectionKey
, L
"PnpInstanceId", 0, REG_SZ
, (const BYTE
*)InstanceId
, (wcslen(InstanceId
) + 1) * sizeof(WCHAR
));
330 if (rc
!= ERROR_SUCCESS
)
332 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
337 rc
= RegSetValueExW(hConnectionKey
, L
"ShowIcon", 0, REG_DWORD
, (const BYTE
*)&dwShowIcon
, sizeof(dwShowIcon
));
338 if (rc
!= ERROR_SUCCESS
)
340 ERR("RegSetValueExW() failed with error 0x%lx\n", rc
);
344 /* Write linkage information in Tcpip service */
345 rc
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Linkage", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_QUERY_VALUE
| KEY_SET_VALUE
, NULL
, &hKey
, NULL
);
346 if (rc
!= ERROR_SUCCESS
)
348 ERR("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
351 rc
= AppendStringToMultiSZ(hKey
, L
"Bind", DeviceName
);
352 if (rc
!= ERROR_SUCCESS
)
354 ERR("AppendStringToMultiSZ() failed with error 0x%lx\n", rc
);
357 rc
= AppendStringToMultiSZ(hKey
, L
"Export", ExportName
);
358 if (rc
!= ERROR_SUCCESS
)
360 ERR("AppendStringToMultiSZ() failed with error 0x%lx\n", rc
);
363 rc
= AppendStringToMultiSZ(hKey
, L
"Route", UuidString
);
364 if (rc
!= ERROR_SUCCESS
)
366 ERR("AppendStringToMultiSZ() failed with error 0x%lx\n", rc
);
373 HeapFree(GetProcessHeap(), 0, InstanceId
);
374 HeapFree(GetProcessHeap(), 0, DeviceName
);
375 HeapFree(GetProcessHeap(), 0, ExportName
);
378 if (hNetworkKey
!= NULL
)
379 RegCloseKey(hNetworkKey
);
380 if (hLinkageKey
!= NULL
)
381 RegCloseKey(hLinkageKey
);
382 if (hConnectionKey
!= NULL
)
383 RegCloseKey(hConnectionKey
);
390 InstallNetClient(VOID
)
392 FIXME("Installation of network clients is not yet supported\n");
393 return ERROR_GEN_FAILURE
;
398 InstallNetService(VOID
)
400 FIXME("Installation of network services is not yet supported\n");
401 return ERROR_GEN_FAILURE
;
406 InstallNetTransport(VOID
)
408 FIXME("Installation of network protocols is not yet supported\n");
409 return ERROR_GEN_FAILURE
;
415 IN DI_FUNCTION InstallFunction
,
416 IN HDEVINFO DeviceInfoSet
,
417 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL
)
419 SP_DRVINFO_DATA_W DriverInfoData
;
420 SP_DRVINFO_DETAIL_DATA_W DriverInfoDetail
;
421 WCHAR SectionName
[LINE_LEN
];
422 HINF hInf
= INVALID_HANDLE_VALUE
;
423 INFCONTEXT InfContext
;
425 INT CharacteristicsInt
;
426 DWORD Characteristics
;
427 LPWSTR BusType
= NULL
;
428 RPC_STATUS RpcStatus
;
430 LPWSTR UuidRpcString
= NULL
;
431 LPWSTR UuidString
= NULL
;
435 if (InstallFunction
!= DIF_INSTALLDEVICE
)
436 return ERROR_DI_DO_DEFAULT
;
438 TRACE("%lu %p %p\n", InstallFunction
, DeviceInfoSet
, DeviceInfoData
);
440 /* Get driver info details */
441 DriverInfoData
.cbSize
= sizeof(SP_DRVINFO_DATA_W
);
442 if (!SetupDiGetSelectedDriverW(DeviceInfoSet
, DeviceInfoData
, &DriverInfoData
))
445 ERR("SetupDiGetSelectedDriverW() failed with error 0x%lx\n", rc
);
449 DriverInfoDetail
.cbSize
= sizeof(SP_DRVINFO_DETAIL_DATA_W
);
450 if (!SetupDiGetDriverInfoDetailW(DeviceInfoSet
, DeviceInfoData
, &DriverInfoData
, &DriverInfoDetail
, sizeof(DriverInfoDetail
), NULL
)
451 && GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
454 ERR("SetupDiGetDriverInfoDetailW() failed with error 0x%lx\n", rc
);
458 hInf
= SetupOpenInfFileW(DriverInfoDetail
.InfFileName
, NULL
, INF_STYLE_WIN4
, &ErrorLine
);
459 if (hInf
== INVALID_HANDLE_VALUE
)
462 ERR("SetupOpenInfFileW() failed with error 0x%lx\n", rc
);
466 if (!SetupDiGetActualSectionToInstallW(hInf
, DriverInfoDetail
.SectionName
, SectionName
, LINE_LEN
, NULL
, NULL
))
469 ERR("SetupDiGetActualSectionToInstallW() failed with error 0x%lx\n", rc
);
473 /* Get Characteristics and BusType (optional) from .inf file */
474 if (!SetupFindFirstLineW(hInf
, SectionName
, L
"Characteristics", &InfContext
))
477 ERR("Unable to find key %S in section %S of file %S (error 0x%lx)\n",
478 L
"Characteristics", SectionName
, DriverInfoDetail
.InfFileName
, rc
);
482 if (!SetupGetIntField(&InfContext
, 1, &CharacteristicsInt
))
485 ERR("SetupGetIntField() failed with error 0x%lx\n", rc
);
489 Characteristics
= (DWORD
)CharacteristicsInt
;
490 if (IsEqualIID(&DeviceInfoData
->ClassGuid
, &GUID_DEVCLASS_NET
))
492 if (SetupFindFirstLineW(hInf
, SectionName
, L
"BusType", &InfContext
))
494 if (!SetupGetStringFieldW(&InfContext
, 1, NULL
, 0, &dwLength
))
497 ERR("SetupGetStringFieldW() failed with error 0x%lx\n", rc
);
501 BusType
= HeapAlloc(GetProcessHeap(), 0, dwLength
* sizeof(WCHAR
));
504 ERR("HeapAlloc() failed\n");
505 rc
= ERROR_NOT_ENOUGH_MEMORY
;
509 if (!SetupGetStringFieldW(&InfContext
, 1, BusType
, dwLength
, NULL
))
512 ERR("SetupGetStringFieldW() failed with error 0x%lx\n", rc
);
518 /* Create a new UUID */
519 RpcStatus
= UuidCreate(&Uuid
);
520 if (RpcStatus
!= RPC_S_OK
&& RpcStatus
!= RPC_S_UUID_LOCAL_ONLY
)
522 ERR("UuidCreate() failed with RPC status 0x%lx\n", RpcStatus
);
523 rc
= ERROR_GEN_FAILURE
;
527 RpcStatus
= UuidToStringW(&Uuid
, &UuidRpcString
);
528 if (RpcStatus
!= RPC_S_OK
)
530 ERR("UuidToStringW() failed with RPC status 0x%lx\n", RpcStatus
);
531 rc
= ERROR_GEN_FAILURE
;
535 /* Add curly braces around Uuid */
536 UuidString
= HeapAlloc(GetProcessHeap(), 0, (2 + wcslen(UuidRpcString
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
539 ERR("HeapAlloc() failed\n");
540 rc
= ERROR_NOT_ENOUGH_MEMORY
;
544 wcscpy(UuidString
, L
"{");
545 wcscat(UuidString
, UuidRpcString
);
546 wcscat(UuidString
, L
"}");
548 if (IsEqualIID(&DeviceInfoData
->ClassGuid
, &GUID_DEVCLASS_NET
))
549 rc
= InstallNetDevice(DeviceInfoSet
, DeviceInfoData
, UuidString
, Characteristics
, BusType
);
550 else if (IsEqualIID(&DeviceInfoData
->ClassGuid
, &GUID_DEVCLASS_NETCLIENT
))
551 rc
= InstallNetClient();
552 else if (IsEqualIID(&DeviceInfoData
->ClassGuid
, &GUID_DEVCLASS_NETSERVICE
))
553 rc
= InstallNetService();
554 else if (IsEqualIID(&DeviceInfoData
->ClassGuid
, &GUID_DEVCLASS_NETTRANS
))
555 rc
= InstallNetTransport();
558 ERR("Invalid class guid\n");
559 rc
= ERROR_GEN_FAILURE
;
563 if (hInf
!= INVALID_HANDLE_VALUE
)
564 SetupCloseInfFile(hInf
);
565 if (UuidRpcString
!= NULL
)
566 RpcStringFreeW(&UuidRpcString
);
567 HeapFree(GetProcessHeap(), 0, BusType
);
568 HeapFree(GetProcessHeap(), 0, UuidString
);
570 TRACE("Returning 0x%lx\n", rc
);