2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Configuration of networkd devices
4 * FILE: lib/netcfgx/netcfgx.c
5 * PURPOSE: Network devices installer
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
15 /* Append a REG_SZ to an existing REG_MULTI_SZ string in the registry.
16 * If the value doesn't exist, create it.
17 * Returns ERROR_SUCCESS if success. Othewise, returns an error code
20 AppendStringToMultiSZ(
23 IN PCWSTR ValueToAppend
)
28 DWORD dwRequired
, dwLength
;
32 rc
= RegQueryValueExW(
39 if (rc
!= ERROR_FILE_NOT_FOUND
)
41 if (rc
!= ERROR_SUCCESS
)
43 if (dwRegType
!= REG_MULTI_SZ
)
45 rc
= ERROR_GEN_FAILURE
;
49 dwTmp
= dwLength
= dwRequired
+ wcslen(ValueToAppend
) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
50 Buffer
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
53 rc
= ERROR_NOT_ENOUGH_MEMORY
;
56 rc
= RegQueryValueExW(
63 if (rc
!= ERROR_SUCCESS
)
68 dwRequired
= sizeof(WCHAR
);
69 dwLength
= wcslen(ValueToAppend
) * sizeof(WCHAR
) + 2 * sizeof(UNICODE_NULL
);
70 Buffer
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
73 rc
= ERROR_NOT_ENOUGH_MEMORY
;
78 /* Append the value */
79 wcscpy(&Buffer
[dwRequired
/ sizeof(WCHAR
) - 1], ValueToAppend
);
80 /* Terminate the REG_MULTI_SZ string */
81 Buffer
[dwLength
/ sizeof(WCHAR
) - 1] = UNICODE_NULL
;
92 HeapFree(GetProcessHeap(), 0, Buffer
);
98 IN DI_FUNCTION InstallFunction
,
99 IN HDEVINFO DeviceInfoSet
,
100 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL
)
102 RPC_STATUS RpcStatus
;
104 LPWSTR UuidRpcString
= NULL
;
105 LPWSTR UuidString
= NULL
;
106 LPWSTR DeviceName
= NULL
;
107 LPWSTR ExportName
= NULL
;
109 HKEY hKey
= INVALID_HANDLE_VALUE
;
110 HKEY hLinkageKey
= INVALID_HANDLE_VALUE
;
111 HKEY hNetworkKey
= INVALID_HANDLE_VALUE
;
112 HKEY hConnectionKey
= INVALID_HANDLE_VALUE
;
114 if (InstallFunction
!= DIF_INSTALLDEVICE
)
115 return ERROR_DI_DO_DEFAULT
;
117 /* Create a new UUID */
118 RpcStatus
= UuidCreate(&Uuid
);
119 if (RpcStatus
!= RPC_S_OK
&& RpcStatus
!= RPC_S_UUID_LOCAL_ONLY
)
121 DPRINT("UuidCreate() failed with RPC status 0x%lx\n", RpcStatus
);
122 rc
= ERROR_GEN_FAILURE
;
125 RpcStatus
= UuidToStringW(&Uuid
, &UuidRpcString
);
126 if (RpcStatus
!= RPC_S_OK
)
128 DPRINT("UuidToStringW() failed with RPC status 0x%lx\n", RpcStatus
);
129 rc
= ERROR_GEN_FAILURE
;
133 /* Add curly braces around Uuid */
134 UuidString
= HeapAlloc(GetProcessHeap(), 0, (2 + wcslen(UuidRpcString
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
137 DPRINT("HeapAlloc() failed\n");
138 rc
= ERROR_NOT_ENOUGH_MEMORY
;
141 wcscpy(UuidString
, L
"{");
142 wcscat(UuidString
, UuidRpcString
);
143 wcscat(UuidString
, L
"}");
145 /* Create device name */
146 DeviceName
= HeapAlloc(GetProcessHeap(), 0, (wcslen(L
"\\Device\\") + wcslen(UuidString
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
149 DPRINT("HeapAlloc() failed\n");
150 rc
= ERROR_NOT_ENOUGH_MEMORY
;
153 wcscpy(DeviceName
, L
"\\Device\\");
154 wcscat(DeviceName
, UuidString
);
156 /* Create export name */
157 ExportName
= HeapAlloc(GetProcessHeap(), 0, (wcslen(L
"\\Device\\Tcpip_") + wcslen(UuidString
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
));
160 DPRINT("HeapAlloc() failed\n");
161 rc
= ERROR_NOT_ENOUGH_MEMORY
;
164 wcscpy(ExportName
, L
"\\Device\\Tcpip_");
165 wcscat(ExportName
, UuidString
);
167 /* Write Tcpip parameters in new service Key */
168 rc
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, L
"SYSTEM\\CurrentControlSet\\Services", 0, NULL
, REG_OPTION_NON_VOLATILE
, 0, NULL
, &hKey
, NULL
);
169 if (rc
!= ERROR_SUCCESS
)
171 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
174 rc
= RegCreateKeyExW(hKey
, UuidString
, 0, NULL
, REG_OPTION_NON_VOLATILE
, 0, NULL
, &hNetworkKey
, NULL
);
175 if (rc
!= ERROR_SUCCESS
)
177 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
181 hKey
= INVALID_HANDLE_VALUE
;
182 rc
= RegCreateKeyExW(hNetworkKey
, L
"Parameters\\Tcpip", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_SET_VALUE
, NULL
, &hKey
, NULL
);
183 if (rc
!= ERROR_SUCCESS
)
185 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
188 RegCloseKey(hNetworkKey
);
189 hNetworkKey
= INVALID_HANDLE_VALUE
;
190 rc
= RegSetValueExW(hKey
, L
"DefaultGateway", 0, REG_SZ
, (const BYTE
*)L
"0.0.0.0", (wcslen(L
"0.0.0.0") + 1) * sizeof(WCHAR
));
191 if (rc
!= ERROR_SUCCESS
)
193 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
196 rc
= RegSetValueExW(hKey
, L
"IPAddress", 0, REG_SZ
, (const BYTE
*)L
"0.0.0.0", (wcslen(L
"0.0.0.0") + 1) * sizeof(WCHAR
));
197 if (rc
!= ERROR_SUCCESS
)
199 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
202 rc
= RegSetValueExW(hKey
, L
"SubnetMask", 0, REG_SZ
, (const BYTE
*)L
"0.0.0.0", (wcslen(L
"0.0.0.0") + 1) * sizeof(WCHAR
));
203 if (rc
!= ERROR_SUCCESS
)
205 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
209 hKey
= INVALID_HANDLE_VALUE
;
211 /* Write 'Linkage' key in hardware key */
212 hKey
= SetupDiCreateDevRegKeyW(DeviceInfoSet
, DeviceInfoData
, DICS_FLAG_GLOBAL
, 0, DIREG_DRV
, NULL
, NULL
);
213 if (hKey
== INVALID_HANDLE_VALUE
)
216 DPRINT("SetupDiCreateDevRegKeyW() failed with error 0x%lx\n", rc
);
219 rc
= RegSetValueExW(hKey
, L
"NetCfgInstanceId", 0, REG_SZ
, (const BYTE
*)UuidString
, (wcslen(UuidString
) + 1) * sizeof(WCHAR
));
220 if (rc
!= ERROR_SUCCESS
)
222 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
225 rc
= RegCreateKeyExW(hKey
, L
"Linkage", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_SET_VALUE
, NULL
, &hLinkageKey
, NULL
);
226 if (rc
!= ERROR_SUCCESS
)
228 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
231 rc
= RegSetValueExW(hLinkageKey
, L
"Export", 0, REG_SZ
, (const BYTE
*)DeviceName
, (wcslen(DeviceName
) + 1) * sizeof(WCHAR
));
232 if (rc
!= ERROR_SUCCESS
)
234 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
237 rc
= RegSetValueExW(hLinkageKey
, L
"RootDevice", 0, REG_SZ
, (const BYTE
*)UuidString
, (wcslen(UuidString
) + 1) * sizeof(WCHAR
));
238 if (rc
!= ERROR_SUCCESS
)
240 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
243 rc
= RegSetValueExW(hLinkageKey
, L
"UpperBind", 0, REG_SZ
, (const BYTE
*)L
"Tcpip", (wcslen(L
"Tcpip") + 1) * sizeof(WCHAR
));
244 if (rc
!= ERROR_SUCCESS
)
246 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
250 hKey
= INVALID_HANDLE_VALUE
;
252 /* Write connection information in network subkey */
253 rc
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, L
"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, NULL
, REG_OPTION_NON_VOLATILE
, 0, NULL
, &hNetworkKey
, NULL
);
254 if (rc
!= ERROR_SUCCESS
)
256 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
259 rc
= RegCreateKeyExW(hNetworkKey
, UuidString
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_SET_VALUE
, NULL
, &hKey
, NULL
);
260 if (rc
!= ERROR_SUCCESS
)
262 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
265 rc
= RegSetValueExW(hKey
, L
"Name", 0, REG_SZ
, (const BYTE
*)L
"Network connection", (wcslen(L
"Network connection") + 1) * sizeof(WCHAR
));
266 if (rc
!= ERROR_SUCCESS
)
268 DPRINT("RegSetValueExW() failed with error 0x%lx\n", rc
);
272 hKey
= INVALID_HANDLE_VALUE
;
274 /* Write linkage information in Tcpip service */
275 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
);
276 if (rc
!= ERROR_SUCCESS
)
278 DPRINT("RegCreateKeyExW() failed with error 0x%lx\n", rc
);
281 rc
= AppendStringToMultiSZ(hKey
, L
"Bind", DeviceName
);
282 if (rc
!= ERROR_SUCCESS
)
284 rc
= AppendStringToMultiSZ(hKey
, L
"Export", ExportName
);
285 if (rc
!= ERROR_SUCCESS
)
287 rc
= AppendStringToMultiSZ(hKey
, L
"Route", UuidString
);
288 if (rc
!= ERROR_SUCCESS
)
294 if (UuidRpcString
!= NULL
)
295 RpcStringFreeW(&UuidRpcString
);
296 HeapFree(GetProcessHeap(), 0, UuidString
);
297 HeapFree(GetProcessHeap(), 0, DeviceName
);
298 HeapFree(GetProcessHeap(), 0, ExportName
);
299 if (hKey
!= INVALID_HANDLE_VALUE
)
301 if (hLinkageKey
!= INVALID_HANDLE_VALUE
)
302 RegCloseKey(hLinkageKey
);
303 if (hNetworkKey
!= INVALID_HANDLE_VALUE
)
304 RegCloseKey(hNetworkKey
);
305 if (hConnectionKey
!= INVALID_HANDLE_VALUE
)
306 RegCloseKey(hConnectionKey
);
308 return rc
== ERROR_SUCCESS
? ERROR_DI_DO_DEFAULT
: rc
;