reg.c 74.3 KB
Newer Older
1 2
/*
 * SHLWAPI registry functions
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Copyright 1998 Juergen Schmied
 * Copyright 2001 Guy Albertelli
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22
#include <stdarg.h>
23
#include <string.h>
24
#include "windef.h"
25 26
#include "winbase.h"
#include "winuser.h"
27
#include "winreg.h"
28
#include "wine/debug.h"
29
#define NO_SHLWAPI_STREAM
30
#include "shlwapi.h"
31
#include "wine/unicode.h"
32

33
WINE_DEFAULT_DEBUG_CHANNEL(shell);
34

35
/* Key/Value names for MIME content types */
36
static const char lpszContentTypeA[] = "Content Type";
37 38
static const WCHAR lpszContentTypeW[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};

39
static const char szMimeDbContentA[] = "MIME\\Database\\Content Type\\";
40 41
static const WCHAR szMimeDbContentW[] = { 'M', 'I', 'M','E','\\',
  'D','a','t','a','b','a','s','e','\\','C','o','n','t','e','n','t',
42
  ' ','T','y','p','e','\\', 0 };
43 44
static const DWORD dwLenMimeDbContent = 27; /* strlen of szMimeDbContentA/W */

45
static const char szExtensionA[] = "Extension";
46 47
static const WCHAR szExtensionW[] = { 'E', 'x', 't','e','n','s','i','o','n','\0' };

48 49
/* internal structure of what the HUSKEY points to */
typedef struct {
50 51 52 53 54 55
    HKEY     HKCUstart; /* Start key in CU hive */
    HKEY     HKCUkey;   /* Opened key in CU hive */
    HKEY     HKLMstart; /* Start key in LM hive */
    HKEY     HKLMkey;   /* Opened key in LM hive */
    WCHAR    lpszPath[MAX_PATH];
} SHUSKEY, *LPSHUSKEY;
56

57
INT     WINAPI SHStringFromGUIDW(REFGUID,LPWSTR,INT);
58
HRESULT WINAPI SHRegGetCLSIDKeyW(REFGUID,LPCWSTR,BOOL,BOOL,PHKEY);
Jon Griffiths's avatar
Jon Griffiths committed
59

60

61 62 63 64 65 66 67 68
#define REG_HKCU  TRUE
#define REG_HKLM  FALSE
/*************************************************************************
 * REG_GetHKEYFromHUSKEY
 *
 * Function:  Return the proper registry key from the HUSKEY structure
 *            also allow special predefined values.
 */
69
static HKEY REG_GetHKEYFromHUSKEY(HUSKEY hUSKey, BOOL which)
70
{
71 72
        HKEY test = hUSKey;
        LPSHUSKEY mihk = hUSKey;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

	if ((test == HKEY_CLASSES_ROOT)        ||
	    (test == HKEY_CURRENT_CONFIG)      ||
	    (test == HKEY_CURRENT_USER)        ||
	    (test == HKEY_DYN_DATA)            ||
	    (test == HKEY_LOCAL_MACHINE)       ||
	    (test == HKEY_PERFORMANCE_DATA)    ||
/* FIXME:  need to define for Win2k, ME, XP
 *	    (test == HKEY_PERFORMANCE_TEXT)    ||
 *	    (test == HKEY_PERFORMANCE_NLSTEXT) ||
 */
	    (test == HKEY_USERS)) return test;
	if (which == REG_HKCU) return mihk->HKCUkey;
	return mihk->HKLMkey;
}


90 91 92
/*************************************************************************
 * SHRegOpenUSKeyA	[SHLWAPI.@]
 *
93 94 95 96 97 98 99 100
 * Open a user-specific registry key.
 *
 * PARAMS
 *  Path           [I] Key name to open
 *  AccessType     [I] Access type
 *  hRelativeUSKey [I] Relative user key
 *  phNewUSKey     [O] Destination for created key
 *  fIgnoreHKCU    [I] TRUE=Don't check HKEY_CURRENT_USER
Jon Griffiths's avatar
Jon Griffiths committed
101 102 103 104
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from RegOpenKeyExA().
105
 */
106 107
LONG WINAPI SHRegOpenUSKeyA(LPCSTR Path, REGSAM AccessType, HUSKEY hRelativeUSKey,
                            PHUSKEY phNewUSKey, BOOL fIgnoreHKCU)
108
{
109
    WCHAR szPath[MAX_PATH];
110

111 112
    if (Path)
      MultiByteToWideChar(CP_ACP, 0, Path, -1, szPath, MAX_PATH);
113

114 115
    return SHRegOpenUSKeyW(Path ? szPath : NULL, AccessType, hRelativeUSKey,
                           phNewUSKey, fIgnoreHKCU);
116 117 118 119 120
}

/*************************************************************************
 * SHRegOpenUSKeyW	[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
121
 * See SHRegOpenUSKeyA.
122
 */
123 124
LONG WINAPI SHRegOpenUSKeyW(LPCWSTR Path, REGSAM AccessType, HUSKEY hRelativeUSKey,
                            PHUSKEY phNewUSKey, BOOL fIgnoreHKCU)
125
{
126
    LONG ret2, ret1 = ~ERROR_SUCCESS;
127 128
    LPSHUSKEY hKey;

129
    TRACE("(%s,0x%x,%p,%p,%d)\n", debugstr_w(Path),(LONG)AccessType,
130
          hRelativeUSKey, phNewUSKey, fIgnoreHKCU);
131

132 133
    if (phNewUSKey)
        *phNewUSKey = NULL;
134

135
    /* Create internal HUSKEY */
136
    hKey = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*hKey));
137
    lstrcpynW(hKey->lpszPath, Path, sizeof(hKey->lpszPath)/sizeof(WCHAR));
138

139 140 141 142 143 144 145 146
    if (hRelativeUSKey)
    {
        hKey->HKCUstart = SHRegDuplicateHKey(REG_GetHKEYFromHUSKEY(hRelativeUSKey, REG_HKCU));
        hKey->HKLMstart = SHRegDuplicateHKey(REG_GetHKEYFromHUSKEY(hRelativeUSKey, REG_HKLM));

        /* FIXME: if either of these keys is NULL, create the start key from
         *        the relative keys start+path
         */
147
    }
148 149 150 151
    else
    {
        hKey->HKCUstart = HKEY_CURRENT_USER;
        hKey->HKLMstart = HKEY_LOCAL_MACHINE;
152 153
    }

154 155 156 157 158
    if (!fIgnoreHKCU)
    {
        ret1 = RegOpenKeyExW(hKey->HKCUstart, hKey->lpszPath, 0, AccessType, &hKey->HKCUkey);
        if (ret1)
            hKey->HKCUkey = 0;
159
    }
160 161 162 163 164 165

    ret2 = RegOpenKeyExW(hKey->HKLMstart, hKey->lpszPath, 0, AccessType, &hKey->HKLMkey);
    if (ret2)
        hKey->HKLMkey = 0;

    if (ret1 || ret2)
166
        TRACE("one or more opens failed: HKCU=%d HKLM=%d\n", ret1, ret2);
167 168 169 170 171 172

    if (ret1 && ret2)
    {
        /* Neither open succeeded: fail */
        SHRegCloseUSKey(hKey);
        return ret2;
173 174
    }

175
    TRACE("HUSKEY=%p\n", hKey);
176
    if (phNewUSKey)
177
        *phNewUSKey = hKey;
178 179 180 181 182 183
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegCloseUSKey	[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
184 185 186 187 188
 * Close a user-specific registry key
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from RegCloseKey().
189 190
 */
LONG WINAPI SHRegCloseUSKey(
Jon Griffiths's avatar
Jon Griffiths committed
191
        HUSKEY hUSKey) /* [I] Key to close */
192
{
193
    LPSHUSKEY hKey = hUSKey;
194
    LONG ret = ERROR_SUCCESS;
195

196 197 198 199 200 201 202
    if (hKey->HKCUkey)
        ret = RegCloseKey(hKey->HKCUkey);
    if (hKey->HKCUstart && hKey->HKCUstart != HKEY_CURRENT_USER)
        ret = RegCloseKey(hKey->HKCUstart);
    if (hKey->HKLMkey)
        ret = RegCloseKey(hKey->HKLMkey);
    if (hKey->HKLMstart && hKey->HKLMstart != HKEY_LOCAL_MACHINE)
203
        ret = RegCloseKey(hKey->HKLMstart);
204 205

    HeapFree(GetProcessHeap(), 0, hKey);
206 207 208
    return ret;
}

209 210 211 212 213 214 215 216 217
/*************************************************************************
 * SHRegCreateUSKeyA  [SHLWAPI.@]
 *
 * Create or open a user-specific registry key.
 * 
 * PARAMS
 *  pszPath        [I] Key name to create or open.
 *  samDesired     [I] Wanted security access.
 *  hRelativeUSKey [I] Base path if pszPath is relative. NULL otherwise.
Austin English's avatar
Austin English committed
218
 *  phNewUSKey     [O] Receives a handle to the new or opened key.
219 220 221 222 223 224 225 226 227
 *  dwFlags        [I] Base key under which the key should be opened.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: Nonzero error code from winerror.h
 */
LONG WINAPI SHRegCreateUSKeyA(LPCSTR pszPath, REGSAM samDesired, HUSKEY hRelativeUSKey,
                              PHUSKEY phNewUSKey, DWORD dwFlags)
{
228
    FIXME("(%s, 0x%08x, %p, %p, 0x%08x) stub\n", debugstr_a(pszPath), samDesired,
229 230 231 232 233 234 235 236 237 238 239 240
          hRelativeUSKey, phNewUSKey, dwFlags);
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegCreateUSKeyW  [SHLWAPI.@]
 *
 * See SHRegCreateUSKeyA.
 */
LONG WINAPI SHRegCreateUSKeyW(LPCWSTR pszPath, REGSAM samDesired, HUSKEY hRelativeUSKey,
                              PHUSKEY phNewUSKey, DWORD dwFlags)
{
241
    FIXME("(%s, 0x%08x, %p, %p, 0x%08x) stub\n", debugstr_w(pszPath), samDesired,
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
          hRelativeUSKey, phNewUSKey, dwFlags);
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegDeleteEmptyUSKeyA  [SHLWAPI.@]
 *
 * Delete an empty user-specific registry key.
 *
 * PARAMS
 *  hUSKey      [I] Handle to an open registry key.
 *  pszValue    [I] Empty key name.
 *  delRegFlags [I] Flag that specifies the base from which to delete 
 *                  the key.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: Nonzero error code from winerror.h
 */
LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, LPCSTR pszValue, SHREGDEL_FLAGS delRegFlags)
{
    FIXME("(%p, %s, 0x%08x) stub\n", hUSKey, debugstr_a(pszValue), delRegFlags);
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegDeleteEmptyUSKeyW  [SHLWAPI.@]
 *
 * See SHRegDeleteEmptyUSKeyA.
 */
LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, LPCWSTR pszValue, SHREGDEL_FLAGS delRegFlags)
{
    FIXME("(%p, %s, 0x%08x) stub\n", hUSKey, debugstr_w(pszValue), delRegFlags);
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegDeleteUSValueA  [SHLWAPI.@]
 *
 * Delete a user-specific registry value.
 *
 * PARAMS
 *  hUSKey      [I] Handle to an open registry key.
 *  pszValue    [I] Specifies the value to delete.
 *  delRegFlags [I] Flag that specifies the base of the key from which to
 *                  delete the value.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: Nonzero error code from winerror.h
 */
LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, LPCSTR pszValue, SHREGDEL_FLAGS delRegFlags)
{
    FIXME("(%p, %s, 0x%08x) stub\n", hUSKey, debugstr_a(pszValue), delRegFlags);
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegDeleteUSValueW  [SHLWAPI.@]
 *
 * See SHRegDeleteUSValueA.
 */
LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, LPCWSTR pszValue, SHREGDEL_FLAGS delRegFlags)
{
    FIXME("(%p, %s, 0x%08x) stub\n", hUSKey, debugstr_w(pszValue), delRegFlags);
    return ERROR_SUCCESS;
}

/*************************************************************************
 * SHRegEnumUSValueA  [SHLWAPI.@]
 *
 * Enumerate values of a specified registry key.
 *
 * PARAMS
 *  hUSKey           [I]   Handle to an open registry key.
 *  dwIndex          [I]   Index of the value to be retrieved.
 *  pszValueName     [O]   Buffer to receive the value name.
 *  pcchValueNameLen [I]   Size of pszValueName in characters.
 *  pdwType          [O]   Receives data type of the value.
 *  pvData           [O]   Receives value data. May be NULL.
 *  pcbData          [I/O] Size of pvData in bytes.
 *  enumRegFlags     [I]   Flag that specifies the base key under which to
 *                         enumerate values.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: Nonzero error code from winerror.h
 */
LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD dwIndex, LPSTR pszValueName,
                              LPDWORD pcchValueNameLen, LPDWORD pdwType, LPVOID pvData,
                              LPDWORD pcbData, SHREGENUM_FLAGS enumRegFlags)
{
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    HKEY dokey;

    TRACE("(%p, 0x%08x, %p, %p, %p, %p, %p, 0x%08x)\n", hUSKey, dwIndex,
          pszValueName, pcchValueNameLen, pdwType, pvData, pcbData, enumRegFlags);

    if (((enumRegFlags == SHREGENUM_HKCU) ||
         (enumRegFlags == SHREGENUM_DEFAULT)) &&
        (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
        return RegEnumValueA(dokey, dwIndex, pszValueName, pcchValueNameLen,
                             NULL, pdwType, pvData, pcbData);
    }

    if (((enumRegFlags == SHREGENUM_HKLM) ||
         (enumRegFlags == SHREGENUM_DEFAULT)) &&
        (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
        return RegEnumValueA(dokey, dwIndex, pszValueName, pcchValueNameLen,
                             NULL, pdwType, pvData, pcbData);
    }
    FIXME("no support for SHREGENUM_BOTH\n");
353 354 355 356 357 358 359 360 361 362 363 364
    return ERROR_INVALID_FUNCTION;
}

/*************************************************************************
 * SHRegEnumUSValueW  [SHLWAPI.@]
 *
 * See SHRegEnumUSValueA.
 */
LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD dwIndex, LPWSTR pszValueName,
                              LPDWORD pcchValueNameLen, LPDWORD pdwType, LPVOID pvData,
                              LPDWORD pcbData, SHREGENUM_FLAGS enumRegFlags)
{
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    HKEY dokey;

    TRACE("(%p, 0x%08x, %p, %p, %p, %p, %p, 0x%08x)\n", hUSKey, dwIndex,
          pszValueName, pcchValueNameLen, pdwType, pvData, pcbData, enumRegFlags);

    if (((enumRegFlags == SHREGENUM_HKCU) ||
         (enumRegFlags == SHREGENUM_DEFAULT)) &&
        (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
        return RegEnumValueW(dokey, dwIndex, pszValueName, pcchValueNameLen,
                             NULL, pdwType, pvData, pcbData);
    }

    if (((enumRegFlags == SHREGENUM_HKLM) ||
         (enumRegFlags == SHREGENUM_DEFAULT)) &&
        (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
        return RegEnumValueW(dokey, dwIndex, pszValueName, pcchValueNameLen,
                             NULL, pdwType, pvData, pcbData);
    }
    FIXME("no support for SHREGENUM_BOTH\n");
384 385 386
    return ERROR_INVALID_FUNCTION;
}

387 388
/*************************************************************************
 *      SHRegQueryUSValueA	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
389 390 391 392 393 394
 *
 * Query a user-specific registry value.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from RegQueryValueExA().
395 396
 */
LONG WINAPI SHRegQueryUSValueA(
Jon Griffiths's avatar
Jon Griffiths committed
397 398 399 400 401 402 403 404
	HUSKEY hUSKey, /* [I] Key to query */
	LPCSTR pszValue, /* [I] Value name under hUSKey */
	LPDWORD pdwType, /* [O] Destination for value type */
	LPVOID pvData, /* [O] Destination for value data */
	LPDWORD pcbData, /* [O] Destination for value length */
	BOOL fIgnoreHKCU,  /* [I] TRUE=Don't check HKEY_CURRENT_USER */
	LPVOID pvDefaultData, /* [I] Default data if pszValue does not exist */
	DWORD dwDefaultDataSize) /* [I] Length of pvDefaultData */
405 406 407 408 409 410 411
{
	LONG ret = ~ERROR_SUCCESS;
	LONG i, maxmove;
	HKEY dokey;
	CHAR *src, *dst;

	/* if user wants HKCU, and it exists, then try it */
412
	if (!fIgnoreHKCU && (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
413
	    ret = RegQueryValueExA(dokey,
414
				   pszValue, 0, pdwType, pvData, pcbData);
415
	    TRACE("HKCU RegQueryValue returned %08x\n", ret);
416
	}
417 418 419

	/* if HKCU did not work and HKLM exists, then try it */
	if ((ret != ERROR_SUCCESS) &&
420
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
421 422
	    ret = RegQueryValueExA(dokey,
				   pszValue, 0, pdwType, pvData, pcbData);
423
	    TRACE("HKLM RegQueryValue returned %08x\n", ret);
424
	}
425 426 427 428 429

	/* if neither worked, and default data exists, then use it */
	if (ret != ERROR_SUCCESS) {
	    if (pvDefaultData && (dwDefaultDataSize != 0)) {
		maxmove = (dwDefaultDataSize >= *pcbData) ? *pcbData : dwDefaultDataSize;
430 431
                src = pvDefaultData;
                dst = pvData;
432 433 434 435 436 437 438 439 440 441 442 443
		for(i=0; i<maxmove; i++) *dst++ = *src++;
		*pcbData = maxmove;
		TRACE("setting default data\n");
		ret = ERROR_SUCCESS;
	    }
	}
	return ret;
}


/*************************************************************************
 *      SHRegQueryUSValueW	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
444 445
 *
 * See SHRegQueryUSValueA.
446 447
 */
LONG WINAPI SHRegQueryUSValueW(
Jon Griffiths's avatar
Jon Griffiths committed
448
	HUSKEY hUSKey,
449 450 451 452 453 454 455 456 457 458 459 460 461 462
	LPCWSTR pszValue,
	LPDWORD pdwType,
	LPVOID pvData,
	LPDWORD pcbData,
	BOOL fIgnoreHKCU,
	LPVOID pvDefaultData,
	DWORD dwDefaultDataSize)
{
	LONG ret = ~ERROR_SUCCESS;
	LONG i, maxmove;
	HKEY dokey;
	CHAR *src, *dst;

	/* if user wants HKCU, and it exists, then try it */
463
	if (!fIgnoreHKCU && (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
464
	    ret = RegQueryValueExW(dokey,
465
				   pszValue, 0, pdwType, pvData, pcbData);
466
	    TRACE("HKCU RegQueryValue returned %08x\n", ret);
467
	}
468 469 470

	/* if HKCU did not work and HKLM exists, then try it */
	if ((ret != ERROR_SUCCESS) &&
471
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
472 473
	    ret = RegQueryValueExW(dokey,
				   pszValue, 0, pdwType, pvData, pcbData);
474
	    TRACE("HKLM RegQueryValue returned %08x\n", ret);
475
	}
476 477 478 479 480

	/* if neither worked, and default data exists, then use it */
	if (ret != ERROR_SUCCESS) {
	    if (pvDefaultData && (dwDefaultDataSize != 0)) {
		maxmove = (dwDefaultDataSize >= *pcbData) ? *pcbData : dwDefaultDataSize;
481 482
                src = pvDefaultData;
                dst = pvData;
483 484 485 486 487 488 489 490 491
		for(i=0; i<maxmove; i++) *dst++ = *src++;
		*pcbData = maxmove;
		TRACE("setting default data\n");
		ret = ERROR_SUCCESS;
	    }
	}
	return ret;
}

492 493 494
/*************************************************************************
 * SHRegGetUSValueA	[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
495 496 497 498 499 500 501 502
 * Get a user-specific registry value.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from SHRegOpenUSKeyA() or SHRegQueryUSValueA().
 *
 * NOTES
 *   This function opens pSubKey, queries the value, and then closes the key.
503 504
 */
LONG WINAPI SHRegGetUSValueA(
Jon Griffiths's avatar
Jon Griffiths committed
505 506 507 508 509 510 511 512
	LPCSTR   pSubKey, /* [I] Key name to open */
	LPCSTR   pValue, /* [I] Value name to open */
	LPDWORD  pwType, /* [O] Destination for the type of the value */
	LPVOID   pvData, /* [O] Destination for the value */
	LPDWORD  pcbData, /* [I] Destination for the length of the value **/
	BOOL     flagIgnoreHKCU, /* [I] TRUE=Don't check HKEY_CURRENT_USER */
	LPVOID   pDefaultData, /* [I] Default value if it doesn't exist */
	DWORD    wDefaultDataSize) /* [I] Length of pDefaultData */
513
{
514
	HUSKEY myhuskey;
515
	LONG ret;
516 517

	if (!pvData || !pcbData) return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
518
	TRACE("key '%s', value '%s', datalen %d,  %s\n",
519
	      debugstr_a(pSubKey), debugstr_a(pValue), *pcbData,
520
	      (flagIgnoreHKCU) ? "Ignoring HKCU" : "Tries HKCU then HKLM");
521 522 523

	ret = SHRegOpenUSKeyA(pSubKey, 0x1, 0, &myhuskey, flagIgnoreHKCU);
	if (ret == ERROR_SUCCESS) {
524 525 526
	    ret = SHRegQueryUSValueA(myhuskey, pValue, pwType, pvData,
				     pcbData, flagIgnoreHKCU, pDefaultData,
				     wDefaultDataSize);
527 528 529
	    SHRegCloseUSKey(myhuskey);
	}
	return ret;
530 531 532 533 534
}

/*************************************************************************
 * SHRegGetUSValueW	[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
535
 * See SHRegGetUSValueA.
536 537 538 539 540 541
 */
LONG WINAPI SHRegGetUSValueW(
	LPCWSTR  pSubKey,
	LPCWSTR  pValue,
	LPDWORD  pwType,
	LPVOID   pvData,
542
	LPDWORD  pcbData,
543 544 545 546
	BOOL     flagIgnoreHKCU,
	LPVOID   pDefaultData,
	DWORD    wDefaultDataSize)
{
547
	HUSKEY myhuskey;
548
	LONG ret;
549

550
	if (!pvData || !pcbData) return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
551
	TRACE("key '%s', value '%s', datalen %d,  %s\n",
552
	      debugstr_w(pSubKey), debugstr_w(pValue), *pcbData,
553
	      (flagIgnoreHKCU) ? "Ignoring HKCU" : "Tries HKCU then HKLM");
554

555 556
	ret = SHRegOpenUSKeyW(pSubKey, 0x1, 0, &myhuskey, flagIgnoreHKCU);
	if (ret == ERROR_SUCCESS) {
557 558
	    ret = SHRegQueryUSValueW(myhuskey, pValue, pwType, pvData,
				     pcbData, flagIgnoreHKCU, pDefaultData,
Jon Griffiths's avatar
Jon Griffiths committed
559
				     wDefaultDataSize);
560 561 562 563 564 565 566
	    SHRegCloseUSKey(myhuskey);
	}
	return ret;
}

/*************************************************************************
 * SHRegSetUSValueA   [SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
567 568 569
 *
 * Set a user-specific registry value.
 *
570 571 572 573 574 575 576 577
 * PARAMS
 *  pszSubKey [I] Name of key to set the value in
 *  pszValue  [I] Name of value under pszSubKey to set the value in
 *  dwType    [I] Type of the value
 *  pvData    [I] Data to set as the value
 *  cbData    [I] length of pvData
 *  dwFlags   [I] SHREGSET_ flags from "shlwapi.h"
 *
Jon Griffiths's avatar
Jon Griffiths committed
578 579 580 581 582 583 584
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from SHRegOpenUSKeyA() or SHRegWriteUSValueA(), or
 *           ERROR_INVALID_FUNCTION if pvData is NULL.
 *
 * NOTES
 *   This function opens pszSubKey, sets the value, and then closes the key.
585
 */
586 587
LONG WINAPI SHRegSetUSValueA(LPCSTR pszSubKey, LPCSTR pszValue, DWORD dwType,
                             LPVOID pvData, DWORD cbData, DWORD dwFlags)
588
{
589 590 591 592
  BOOL ignoreHKCU = TRUE;
  HUSKEY hkey;
  LONG ret;

593
  TRACE("(%s,%s,%d,%p,%d,0x%08x\n", debugstr_a(pszSubKey), debugstr_a(pszValue),
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
        dwType, pvData, cbData, dwFlags);

  if (!pvData)
    return ERROR_INVALID_FUNCTION;

  if (dwFlags & SHREGSET_HKCU || dwFlags & SHREGSET_FORCE_HKCU)
    ignoreHKCU = FALSE;

  ret = SHRegOpenUSKeyA(pszSubKey, KEY_ALL_ACCESS, 0, &hkey, ignoreHKCU);
  if (ret == ERROR_SUCCESS)
  {
    ret = SHRegWriteUSValueA(hkey, pszValue, dwType, pvData, cbData, dwFlags);
    SHRegCloseUSKey(hkey);
  }
  return ret;
609 610 611 612
}

/*************************************************************************
 * SHRegSetUSValueW   [SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
613 614
 *
 * See SHRegSetUSValueA.
615
 */
616 617
LONG WINAPI SHRegSetUSValueW(LPCWSTR pszSubKey, LPCWSTR pszValue, DWORD dwType,
                             LPVOID pvData, DWORD cbData, DWORD dwFlags)
618
{
619 620 621 622
  BOOL ignoreHKCU = TRUE;
  HUSKEY hkey;
  LONG ret;

623
  TRACE("(%s,%s,%d,%p,%d,0x%08x\n", debugstr_w(pszSubKey), debugstr_w(pszValue),
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        dwType, pvData, cbData, dwFlags);

  if (!pvData)
    return ERROR_INVALID_FUNCTION;

  if (dwFlags & SHREGSET_HKCU || dwFlags & SHREGSET_FORCE_HKCU)
    ignoreHKCU = FALSE;

  ret = SHRegOpenUSKeyW(pszSubKey, KEY_ALL_ACCESS, 0, &hkey, ignoreHKCU);
  if (ret == ERROR_SUCCESS)
  {
    ret = SHRegWriteUSValueW(hkey, pszValue, dwType, pvData, cbData, dwFlags);
    SHRegCloseUSKey(hkey);
  }
  return ret;
639 640
}

641 642
/*************************************************************************
 * SHRegGetBoolUSValueA   [SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
643 644 645 646 647 648 649 650 651 652 653 654 655
 *
 * Get a user-specific registry boolean value.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from SHRegOpenUSKeyA() or SHRegQueryUSValueA().
 *
 * NOTES
 *   This function opens pszSubKey, queries the value, and then closes the key.
 *
 *   Boolean values are one of the following:
 *   True: YES,TRUE,non-zero
 *   False: NO,FALSE,0
656 657
 */
BOOL WINAPI SHRegGetBoolUSValueA(
Jon Griffiths's avatar
Jon Griffiths committed
658 659 660 661
	LPCSTR pszSubKey, /* [I] Key name to open */
	LPCSTR pszValue, /* [I] Value name to open */
	BOOL fIgnoreHKCU, /* [I] TRUE=Don't check HKEY_CURRENT_USER */
	BOOL fDefault) /* [I] Default value to use if pszValue is not present */
662
{
663 664 665 666 667 668
	DWORD type, datalen, work;
	BOOL ret = fDefault;
	CHAR data[10];

	TRACE("key '%s', value '%s', %s\n",
	      debugstr_a(pszSubKey), debugstr_a(pszValue),
669
	      (fIgnoreHKCU) ? "Ignoring HKCU" : "Tries HKCU then HKLM");
670 671

	datalen = sizeof(data)-1;
672 673 674
	if (!SHRegGetUSValueA( pszSubKey, pszValue, &type,
			       data, &datalen,
			       fIgnoreHKCU, 0, 0)) {
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	    /* process returned data via type into bool */
	    switch (type) {
	    case REG_SZ:
		data[9] = '\0';     /* set end of string */
		if (lstrcmpiA(data, "YES") == 0) ret = TRUE;
		if (lstrcmpiA(data, "TRUE") == 0) ret = TRUE;
		if (lstrcmpiA(data, "NO") == 0) ret = FALSE;
		if (lstrcmpiA(data, "FALSE") == 0) ret = FALSE;
		break;
	    case REG_DWORD:
		work = *(LPDWORD)data;
		ret = (work != 0);
		break;
	    case REG_BINARY:
		if (datalen == 1) {
		    ret = (data[0] != '\0');
		    break;
		}
	    default:
694
		FIXME("Unsupported registry data type %d\n", type);
695 696
		ret = FALSE;
	    }
697
	    TRACE("got value (type=%d), returning <%s>\n", type,
698 699 700 701 702 703 704 705
		  (ret) ? "TRUE" : "FALSE");
	}
	else {
	    ret = fDefault;
	    TRACE("returning default data <%s>\n",
		  (ret) ? "TRUE" : "FALSE");
	}
	return ret;
706 707 708 709
}

/*************************************************************************
 * SHRegGetBoolUSValueW	  [SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
710 711
 *
 * See SHRegGetBoolUSValueA.
712 713 714 715 716 717 718
 */
BOOL WINAPI SHRegGetBoolUSValueW(
	LPCWSTR pszSubKey,
	LPCWSTR pszValue,
	BOOL fIgnoreHKCU,
	BOOL fDefault)
{
719 720 721 722
	static const WCHAR wYES[]=  {'Y','E','S','\0'};
	static const WCHAR wTRUE[]= {'T','R','U','E','\0'};
	static const WCHAR wNO[]=   {'N','O','\0'};
	static const WCHAR wFALSE[]={'F','A','L','S','E','\0'};
723 724 725 726 727 728
	DWORD type, datalen, work;
	BOOL ret = fDefault;
	WCHAR data[10];

	TRACE("key '%s', value '%s', %s\n",
	      debugstr_w(pszSubKey), debugstr_w(pszValue),
729
	      (fIgnoreHKCU) ? "Ignoring HKCU" : "Tries HKCU then HKLM");
730 731

	datalen = (sizeof(data)-1) * sizeof(WCHAR);
732 733 734
	if (!SHRegGetUSValueW( pszSubKey, pszValue, &type,
			       data, &datalen,
			       fIgnoreHKCU, 0, 0)) {
735 736 737
	    /* process returned data via type into bool */
	    switch (type) {
	    case REG_SZ:
738
		data[9] = '\0';     /* set end of string */
739 740 741 742
		if (lstrcmpiW(data, wYES)==0 || lstrcmpiW(data, wTRUE)==0)
		    ret = TRUE;
		else if (lstrcmpiW(data, wNO)==0 || lstrcmpiW(data, wFALSE)==0)
		    ret = FALSE;
743 744 745 746 747 748 749
		break;
	    case REG_DWORD:
		work = *(LPDWORD)data;
		ret = (work != 0);
		break;
	    case REG_BINARY:
		if (datalen == 1) {
750
		    ret = (data[0] != '\0');
751 752 753
		    break;
		}
	    default:
754
		FIXME("Unsupported registry data type %d\n", type);
755 756
		ret = FALSE;
	    }
757
	    TRACE("got value (type=%d), returning <%s>\n", type,
758 759 760 761 762 763 764 765
		  (ret) ? "TRUE" : "FALSE");
	}
	else {
	    ret = fDefault;
	    TRACE("returning default data <%s>\n",
		  (ret) ? "TRUE" : "FALSE");
	}
	return ret;
766 767
}

768 769
/*************************************************************************
 *      SHRegQueryInfoUSKeyA	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
770 771 772 773 774 775
 *
 * Get information about a user-specific registry key.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from RegQueryInfoKeyA().
776
 */
777
LONG WINAPI SHRegQueryInfoUSKeyA(
Jon Griffiths's avatar
Jon Griffiths committed
778 779 780 781 782 783
	HUSKEY hUSKey, /* [I] Key to query */
	LPDWORD pcSubKeys, /* [O] Destination for number of sub keys */
	LPDWORD pcchMaxSubKeyLen, /* [O] Destination for the length of the biggest sub key name */
	LPDWORD pcValues, /* [O] Destination for number of values */
	LPDWORD pcchMaxValueNameLen,/* [O] Destination for the length of the biggest value */
	SHREGENUM_FLAGS enumRegFlags)  /* [in] SHREGENUM_ flags from "shlwapi.h" */
784
{
785 786 787
	HKEY dokey;
	LONG ret;

Jon Griffiths's avatar
Jon Griffiths committed
788 789
	TRACE("(%p,%p,%p,%p,%p,%d)\n",
	      hUSKey,pcSubKeys,pcchMaxSubKeyLen,pcValues,
790 791 792 793
	      pcchMaxValueNameLen,enumRegFlags);

	/* if user wants HKCU, and it exists, then try it */
	if (((enumRegFlags == SHREGENUM_HKCU) ||
794
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
795 796 797 798 799
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
	    ret = RegQueryInfoKeyA(dokey, 0, 0, 0,
				   pcSubKeys, pcchMaxSubKeyLen, 0,
				   pcValues, pcchMaxValueNameLen, 0, 0, 0);
	    if ((ret == ERROR_SUCCESS) ||
800
		(enumRegFlags == SHREGENUM_HKCU))
801 802 803
		return ret;
	}
	if (((enumRegFlags == SHREGENUM_HKLM) ||
804
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
805 806 807 808 809 810
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
	    return RegQueryInfoKeyA(dokey, 0, 0, 0,
				    pcSubKeys, pcchMaxSubKeyLen, 0,
				    pcValues, pcchMaxValueNameLen, 0, 0, 0);
	}
	return ERROR_INVALID_FUNCTION;
811 812 813 814
}

/*************************************************************************
 *      SHRegQueryInfoUSKeyW	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
815 816
 *
 * See SHRegQueryInfoUSKeyA.
817
 */
818
LONG WINAPI SHRegQueryInfoUSKeyW(
Jon Griffiths's avatar
Jon Griffiths committed
819
	HUSKEY hUSKey,
820 821 822 823 824 825
	LPDWORD pcSubKeys,
	LPDWORD pcchMaxSubKeyLen,
	LPDWORD pcValues,
	LPDWORD pcchMaxValueNameLen,
	SHREGENUM_FLAGS enumRegFlags)
{
826 827 828
	HKEY dokey;
	LONG ret;

Jon Griffiths's avatar
Jon Griffiths committed
829 830
	TRACE("(%p,%p,%p,%p,%p,%d)\n",
	      hUSKey,pcSubKeys,pcchMaxSubKeyLen,pcValues,
831
	      pcchMaxValueNameLen,enumRegFlags);
832 833 834

	/* if user wants HKCU, and it exists, then try it */
	if (((enumRegFlags == SHREGENUM_HKCU) ||
835
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
836 837 838 839 840
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
	    ret = RegQueryInfoKeyW(dokey, 0, 0, 0,
				   pcSubKeys, pcchMaxSubKeyLen, 0,
				   pcValues, pcchMaxValueNameLen, 0, 0, 0);
	    if ((ret == ERROR_SUCCESS) ||
841
		(enumRegFlags == SHREGENUM_HKCU))
842 843 844
		return ret;
	}
	if (((enumRegFlags == SHREGENUM_HKLM) ||
845
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
846 847 848 849 850 851
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
	    return RegQueryInfoKeyW(dokey, 0, 0, 0,
				    pcSubKeys, pcchMaxSubKeyLen, 0,
				    pcValues, pcchMaxValueNameLen, 0, 0, 0);
	}
	return ERROR_INVALID_FUNCTION;
852 853
}

854 855
/*************************************************************************
 *      SHRegEnumUSKeyA   	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
856 857 858 859 860 861
 *
 * Enumerate a user-specific registry key.
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: An error code from RegEnumKeyExA().
862 863
 */
LONG WINAPI SHRegEnumUSKeyA(
Jon Griffiths's avatar
Jon Griffiths committed
864 865 866 867 868
	HUSKEY hUSKey,                 /* [in] Key to enumerate */
	DWORD dwIndex,                 /* [in] Index within hUSKey */
	LPSTR pszName,                 /* [out] Name of the enumerated value */
	LPDWORD pcchValueNameLen,      /* [in/out] Length of pszName */
	SHREGENUM_FLAGS enumRegFlags)  /* [in] SHREGENUM_ flags from "shlwapi.h" */
869
{
870 871
	HKEY dokey;

872
	TRACE("(%p,%d,%p,%p(%d),%d)\n",
Jon Griffiths's avatar
Jon Griffiths committed
873
	      hUSKey, dwIndex, pszName, pcchValueNameLen,
874 875 876
	      *pcchValueNameLen, enumRegFlags);

	if (((enumRegFlags == SHREGENUM_HKCU) ||
877
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
878
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
879
	    return RegEnumKeyExA(dokey, dwIndex, pszName, pcchValueNameLen,
880 881 882 883
				0, 0, 0, 0);
	}

	if (((enumRegFlags == SHREGENUM_HKLM) ||
884
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
885
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
886
	    return RegEnumKeyExA(dokey, dwIndex, pszName, pcchValueNameLen,
887 888
				0, 0, 0, 0);
	}
Andreas Mohr's avatar
Andreas Mohr committed
889
	FIXME("no support for SHREGENUM_BOTH\n");
890
	return ERROR_INVALID_FUNCTION;
891 892 893 894
}

/*************************************************************************
 *      SHRegEnumUSKeyW   	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
895 896
 *
 * See SHRegEnumUSKeyA.
897 898
 */
LONG WINAPI SHRegEnumUSKeyW(
Jon Griffiths's avatar
Jon Griffiths committed
899 900 901 902 903
	HUSKEY hUSKey,
	DWORD dwIndex,
	LPWSTR pszName,
	LPDWORD pcchValueNameLen,
	SHREGENUM_FLAGS enumRegFlags)
904
{
905 906
	HKEY dokey;

907
	TRACE("(%p,%d,%p,%p(%d),%d)\n",
Jon Griffiths's avatar
Jon Griffiths committed
908
	      hUSKey, dwIndex, pszName, pcchValueNameLen,
909 910 911
	      *pcchValueNameLen, enumRegFlags);

	if (((enumRegFlags == SHREGENUM_HKCU) ||
912
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
913
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKCU))) {
914
	    return RegEnumKeyExW(dokey, dwIndex, pszName, pcchValueNameLen,
915 916 917 918
				0, 0, 0, 0);
	}

	if (((enumRegFlags == SHREGENUM_HKLM) ||
919
	     (enumRegFlags == SHREGENUM_DEFAULT)) &&
920
	    (dokey = REG_GetHKEYFromHUSKEY(hUSKey,REG_HKLM))) {
921
	    return RegEnumKeyExW(dokey, dwIndex, pszName, pcchValueNameLen,
922 923
				0, 0, 0, 0);
	}
Andreas Mohr's avatar
Andreas Mohr committed
924
	FIXME("no support for SHREGENUM_BOTH\n");
925 926 927
	return ERROR_INVALID_FUNCTION;
}

Jon Griffiths's avatar
Jon Griffiths committed
928

929 930
/*************************************************************************
 *      SHRegWriteUSValueA   	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
931
 *
Jon Griffiths's avatar
Jon Griffiths committed
932
 * Write a user-specific registry value.
Jon Griffiths's avatar
Jon Griffiths committed
933
 *
Jon Griffiths's avatar
Jon Griffiths committed
934 935 936 937 938 939 940 941 942 943
 * PARAMS
 *  hUSKey   [I] Key to write the value to
 *  pszValue [I] Name of value under hUSKey to write the value as
 *  dwType   [I] Type of the value
 *  pvData   [I] Data to set as the value
 *  cbData   [I] length of pvData
 *  dwFlags  [I] SHREGSET_ flags from "shlwapi.h"
 *
 * RETURNS
 *  Success: ERROR_SUCCESS.
944 945 946 947 948
 *  Failure: ERROR_INVALID_PARAMETER, if any parameter is invalid, otherwise
 *           an error code from RegSetValueExA().
 *
 * NOTES
 *  dwFlags must have at least SHREGSET_FORCE_HKCU or SHREGSET_FORCE_HKLM set.
949 950
 */
LONG  WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, LPCSTR pszValue, DWORD dwType,
951
                                LPVOID pvData, DWORD cbData, DWORD dwFlags)
952
{
953
    WCHAR szValue[MAX_PATH];
954

955 956
    if (pszValue)
      MultiByteToWideChar(CP_ACP, 0, pszValue, -1, szValue, MAX_PATH);
957

958 959
    return SHRegWriteUSValueW(hUSKey, pszValue ? szValue : NULL, dwType,
                               pvData, cbData, dwFlags);
960 961 962 963
}

/*************************************************************************
 *      SHRegWriteUSValueW   	[SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
964 965
 *
 * See SHRegWriteUSValueA.
966 967
 */
LONG  WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, LPCWSTR pszValue, DWORD dwType,
968
                                LPVOID pvData, DWORD cbData, DWORD dwFlags)
969
{
970
    DWORD dummy;
971
    LPSHUSKEY hKey = hUSKey;
972
    LONG ret = ERROR_SUCCESS;
973

974
    TRACE("(%p,%s,%d,%p,%d,%d)\n", hUSKey, debugstr_w(pszValue),
975 976 977 978 979 980 981 982 983 984 985 986
          dwType, pvData, cbData, dwFlags);

    if (!hUSKey || IsBadWritePtr(hUSKey, sizeof(SHUSKEY)) ||
        !(dwFlags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM)))
        return ERROR_INVALID_PARAMETER;

    if (dwFlags & (SHREGSET_FORCE_HKCU|SHREGSET_HKCU))
    {
        if (!hKey->HKCUkey)
        {
            /* Create the key */
            ret = RegCreateKeyW(hKey->HKCUstart, hKey->lpszPath, &hKey->HKCUkey);
987
            TRACE("Creating HKCU key, ret = %d\n", ret);
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
            if (ret && (dwFlags & (SHREGSET_FORCE_HKCU)))
            {
                hKey->HKCUkey = 0;
                return ret;
            }
        }

        if (!ret)
        {
            if ((dwFlags & SHREGSET_FORCE_HKCU) ||
                RegQueryValueExW(hKey->HKCUkey, pszValue, NULL, NULL, NULL, &dummy))
            {
                /* Doesn't exist or we are forcing: Write value */
                ret = RegSetValueExW(hKey->HKCUkey, pszValue, 0, dwType, pvData, cbData);
1002
                TRACE("Writing HKCU value, ret = %d\n", ret);
1003 1004
            }
        }
1005 1006
    }

1007 1008 1009 1010 1011 1012
    if (dwFlags & (SHREGSET_FORCE_HKLM|SHREGSET_HKLM))
    {
        if (!hKey->HKLMkey)
        {
            /* Create the key */
            ret = RegCreateKeyW(hKey->HKLMstart, hKey->lpszPath, &hKey->HKLMkey);
1013
            TRACE("Creating HKLM key, ret = %d\n", ret);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
            if (ret && (dwFlags & (SHREGSET_FORCE_HKLM)))
            {
                hKey->HKLMkey = 0;
                return ret;
            }
        }

        if (!ret)
        {
            if ((dwFlags & SHREGSET_FORCE_HKLM) ||
                RegQueryValueExW(hKey->HKLMkey, pszValue, NULL, NULL, NULL, &dummy))
            {
                /* Doesn't exist or we are forcing: Write value */
                ret = RegSetValueExW(hKey->HKLMkey, pszValue, 0, dwType, pvData, cbData);
1028
                TRACE("Writing HKLM value, ret = %d\n", ret);
1029 1030
            }
        }
1031 1032
    }

1033
    return ret;
1034 1035
}

1036 1037
/*************************************************************************
 * SHRegGetPathA   [SHLWAPI.@]
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
 *
 * Get a path from the registry.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key containing path to get
 *   lpszValue  [I] Name of value containing path to get
 *   lpszPath   [O] Buffer for returned path
 *   dwFlags    [I] Reserved
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. lpszPath contains the path.
Jon Griffiths's avatar
Jon Griffiths committed
1050
 *   Failure: An error code from RegOpenKeyExA() or SHQueryValueExA().
1051
 */
1052 1053
DWORD WINAPI SHRegGetPathA(HKEY hKey, LPCSTR lpszSubKey, LPCSTR lpszValue,
                           LPSTR lpszPath, DWORD dwFlags)
1054
{
1055
  DWORD dwSize = MAX_PATH;
1056

1057
  TRACE("(hkey=%p,%s,%s,%p,%d)\n", hKey, debugstr_a(lpszSubKey),
1058 1059
        debugstr_a(lpszValue), lpszPath, dwFlags);

1060
  return SHGetValueA(hKey, lpszSubKey, lpszValue, 0, lpszPath, &dwSize);
1061 1062 1063 1064
}

/*************************************************************************
 * SHRegGetPathW   [SHLWAPI.@]
1065 1066
 *
 * See SHRegGetPathA.
1067
 */
1068 1069
DWORD WINAPI SHRegGetPathW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue,
                           LPWSTR lpszPath, DWORD dwFlags)
1070
{
1071
  DWORD dwSize = MAX_PATH;
1072

1073
  TRACE("(hkey=%p,%s,%s,%p,%d)\n", hKey, debugstr_w(lpszSubKey),
1074 1075
        debugstr_w(lpszValue), lpszPath, dwFlags);

1076
  return SHGetValueW(hKey, lpszSubKey, lpszValue, 0, lpszPath, &dwSize);
1077 1078
}

1079

1080
/*************************************************************************
1081 1082 1083
 * SHRegSetPathA   [SHLWAPI.@]
 *
 * Write a path to the registry.
1084
 *
1085 1086 1087 1088 1089
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key containing path to set
 *   lpszValue  [I] Name of value containing path to set
 *   lpszPath   [O] Path to write
Jon Griffiths's avatar
Jon Griffiths committed
1090
 *   dwFlags    [I] Reserved, must be 0.
1091 1092 1093
 *
 * RETURNS
 *   Success: ERROR_SUCCESS.
Jon Griffiths's avatar
Jon Griffiths committed
1094
 *   Failure: An error code from SHSetValueA().
1095
 */
1096 1097 1098 1099 1100
DWORD WINAPI SHRegSetPathA(HKEY hKey, LPCSTR lpszSubKey, LPCSTR lpszValue,
                           LPCSTR lpszPath, DWORD dwFlags)
{
  char szBuff[MAX_PATH];

1101
  FIXME("(hkey=%p,%s,%s,%p,%d) - semi-stub\n",hKey, debugstr_a(lpszSubKey),
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
        debugstr_a(lpszValue), lpszPath, dwFlags);

  lstrcpyA(szBuff, lpszPath);

  /* FIXME: PathUnExpandEnvStringsA(szBuff); */

  return SHSetValueA(hKey,lpszSubKey, lpszValue, REG_SZ, szBuff,
                     lstrlenA(szBuff));
}

/*************************************************************************
 * SHRegSetPathW   [SHLWAPI.@]
 *
 * See SHRegSetPathA.
 */
DWORD WINAPI SHRegSetPathW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue,
                           LPCWSTR lpszPath, DWORD dwFlags)
1119
{
1120 1121
  WCHAR szBuff[MAX_PATH];

1122
  FIXME("(hkey=%p,%s,%s,%p,%d) - semi-stub\n",hKey, debugstr_w(lpszSubKey),
1123 1124 1125 1126 1127
        debugstr_w(lpszValue), lpszPath, dwFlags);

  lstrcpyW(szBuff, lpszPath);

  /* FIXME: PathUnExpandEnvStringsW(szBuff); */
1128

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
  return SHSetValueW(hKey,lpszSubKey, lpszValue, REG_SZ, szBuff,
                     lstrlenW(szBuff));
}

/*************************************************************************
 * SHGetValueA   [SHLWAPI.@]
 *
 * Get a value from the registry.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key containing value to get
 *   lpszValue  [I] Name of value to get
 *   pwType     [O] Pointer to the values type
 *   pvData     [O] Pointer to the values data
 *   pcbData    [O] Pointer to the values size
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. Output parameters contain the details read.
Jon Griffiths's avatar
Jon Griffiths committed
1148
 *   Failure: An error code from RegOpenKeyExA() or SHQueryValueExA().
1149 1150 1151 1152
 */
DWORD WINAPI SHGetValueA(HKEY hKey, LPCSTR lpszSubKey, LPCSTR lpszValue,
                         LPDWORD pwType, LPVOID pvData, LPDWORD pcbData)
{
1153 1154
  DWORD dwRet = 0;
  HKEY hSubKey = 0;
1155

1156
  TRACE("(hkey=%p,%s,%s,%p,%p,%p)\n", hKey, debugstr_a(lpszSubKey),
1157
        debugstr_a(lpszValue), pwType, pvData, pcbData);
1158

1159 1160 1161
  /*   lpszSubKey can be 0. In this case the value is taken from the
   *   current key.
   */
1162
  if(lpszSubKey)
1163 1164 1165
    dwRet = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_QUERY_VALUE, &hSubKey);

  if (! dwRet)
1166
  {
1167 1168 1169
    /* SHQueryValueEx expands Environment strings */
    dwRet = SHQueryValueExA(hSubKey ? hSubKey : hKey, lpszValue, 0, pwType, pvData, pcbData);
    if (hSubKey) RegCloseKey(hSubKey);
1170 1171
  }
  return dwRet;
1172 1173 1174 1175 1176
}

/*************************************************************************
 * SHGetValueW   [SHLWAPI.@]
 *
1177
 * See SHGetValueA.
1178
 */
1179 1180
DWORD WINAPI SHGetValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue,
                         LPDWORD pwType, LPVOID pvData, LPDWORD pcbData)
1181
{
1182 1183
  DWORD dwRet = 0;
  HKEY hSubKey = 0;
1184

1185
  TRACE("(hkey=%p,%s,%s,%p,%p,%p)\n", hKey, debugstr_w(lpszSubKey),
1186
        debugstr_w(lpszValue), pwType, pvData, pcbData);
1187

1188
  if(lpszSubKey)
1189 1190 1191
    dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_QUERY_VALUE, &hSubKey);

  if (! dwRet)
1192
  {
1193 1194
    dwRet = SHQueryValueExW(hSubKey ? hSubKey : hKey, lpszValue, 0, pwType, pvData, pcbData);
    if (hSubKey) RegCloseKey(hSubKey);
1195 1196 1197
  }
  return dwRet;
}
1198

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
/*************************************************************************
 * SHSetValueA   [SHLWAPI.@]
 *
 * Set a value in the registry.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key under hKey
 *   lpszValue  [I] Name of value to set
 *   dwType     [I] Type of the value
 *   pvData     [I] Data of the value
 *   cbData     [I] Size of the value
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. The value is set with the data given.
Jon Griffiths's avatar
Jon Griffiths committed
1214
 *   Failure: An error code from RegCreateKeyExA() or RegSetValueExA()
1215 1216
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
1217 1218
 *   If lpszSubKey does not exist, it is created before the value is set. If
 *   lpszSubKey is NULL or an empty string, then the value is added directly
1219 1220 1221 1222 1223 1224 1225 1226
 *   to hKey instead.
 */
DWORD WINAPI SHSetValueA(HKEY hKey, LPCSTR lpszSubKey, LPCSTR lpszValue,
                         DWORD dwType, LPCVOID pvData, DWORD cbData)
{
  DWORD dwRet = ERROR_SUCCESS, dwDummy;
  HKEY  hSubKey;

1227
  TRACE("(hkey=%p,%s,%s,%d,%p,%d)\n", hKey, debugstr_a(lpszSubKey),
1228 1229 1230
          debugstr_a(lpszValue), dwType, pvData, cbData);

  if (lpszSubKey && *lpszSubKey)
1231
    dwRet = RegCreateKeyExA(hKey, lpszSubKey, 0, NULL,
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
                            0, KEY_SET_VALUE, NULL, &hSubKey, &dwDummy);
  else
    hSubKey = hKey;
  if (!dwRet)
  {
    dwRet = RegSetValueExA(hSubKey, lpszValue, 0, dwType, pvData, cbData);
    if (hSubKey != hKey)
      RegCloseKey(hSubKey);
  }
  return dwRet;
1242 1243 1244
}

/*************************************************************************
1245 1246 1247
 * SHSetValueW   [SHLWAPI.@]
 *
 * See SHSetValueA.
1248
 */
1249 1250
DWORD WINAPI SHSetValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue,
                         DWORD dwType, LPCVOID pvData, DWORD cbData)
1251
{
1252 1253 1254
  DWORD dwRet = ERROR_SUCCESS, dwDummy;
  HKEY  hSubKey;

1255
  TRACE("(hkey=%p,%s,%s,%d,%p,%d)\n", hKey, debugstr_w(lpszSubKey),
1256 1257 1258
        debugstr_w(lpszValue), dwType, pvData, cbData);

  if (lpszSubKey && *lpszSubKey)
1259
    dwRet = RegCreateKeyExW(hKey, lpszSubKey, 0, NULL,
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
                            0, KEY_SET_VALUE, NULL, &hSubKey, &dwDummy);
  else
    hSubKey = hKey;
  if (!dwRet)
  {
    dwRet = RegSetValueExW(hSubKey, lpszValue, 0, dwType, pvData, cbData);
    if (hSubKey != hKey)
      RegCloseKey(hSubKey);
  }
  return dwRet;
1270 1271 1272
}

/*************************************************************************
1273 1274
 * SHQueryInfoKeyA   [SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
1275
 * Get information about a registry key. See RegQueryInfoKeyA().
Jon Griffiths's avatar
Jon Griffiths committed
1276 1277 1278
 *
 * RETURNS
 *  The result of calling RegQueryInfoKeyA().
1279
 */
1280 1281
LONG WINAPI SHQueryInfoKeyA(HKEY hKey, LPDWORD pwSubKeys, LPDWORD pwSubKeyMax,
                            LPDWORD pwValues, LPDWORD pwValueMax)
1282
{
1283
  TRACE("(hkey=%p,%p,%p,%p,%p)\n", hKey, pwSubKeys, pwSubKeyMax,
1284 1285 1286
        pwValues, pwValueMax);
  return RegQueryInfoKeyA(hKey, NULL, NULL, NULL, pwSubKeys, pwSubKeyMax,
                          NULL, pwValues, pwValueMax, NULL, NULL, NULL);
1287 1288 1289
}

/*************************************************************************
1290
 * SHQueryInfoKeyW   [SHLWAPI.@]
1291
 *
Jon Griffiths's avatar
Jon Griffiths committed
1292
 * See SHQueryInfoKeyA.
1293
 */
1294 1295
LONG WINAPI SHQueryInfoKeyW(HKEY hKey, LPDWORD pwSubKeys, LPDWORD pwSubKeyMax,
                            LPDWORD pwValues, LPDWORD pwValueMax)
1296
{
1297
  TRACE("(hkey=%p,%p,%p,%p,%p)\n", hKey, pwSubKeys, pwSubKeyMax,
1298 1299 1300
        pwValues, pwValueMax);
  return RegQueryInfoKeyW(hKey, NULL, NULL, NULL, pwSubKeys, pwSubKeyMax,
                          NULL, pwValues, pwValueMax, NULL, NULL, NULL);
1301 1302
}

1303 1304 1305 1306 1307 1308 1309
/*************************************************************************
 * SHQueryValueExA   [SHLWAPI.@]
 *
 * Get a value from the registry, expanding environment variable strings.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
1310
 *   lpszValue  [I] Name of value to query
1311 1312 1313 1314 1315 1316
 *   lpReserved [O] Reserved for future use; must be NULL
 *   pwType     [O] Optional pointer updated with the values type
 *   pvData     [O] Optional pointer updated with the values data
 *   pcbData    [O] Optional pointer updated with the values size
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
1317
 *   Success: ERROR_SUCCESS. Any non NULL output parameters are updated with
1318 1319 1320
 *            information about the value.
 *   Failure: ERROR_OUTOFMEMORY if memory allocation fails, or the type of the
 *            data is REG_EXPAND_SZ and pcbData is NULL. Otherwise an error
Jon Griffiths's avatar
Jon Griffiths committed
1321
 *            code from RegQueryValueExA() or ExpandEnvironmentStringsA().
1322 1323 1324 1325 1326 1327 1328 1329
 *
 * NOTES
 *   Either pwType, pvData or pcbData may be NULL if the caller doesn't want
 *   the type, data or size information for the value.
 *
 *   If the type of the data is REG_EXPAND_SZ, it is expanded to REG_SZ. The
 *   value returned will be truncated if it is of type REG_SZ and bigger than
 *   the buffer given to store it.
Juergen Schmied's avatar
Juergen Schmied committed
1330
 *
Jon Griffiths's avatar
Jon Griffiths committed
1331 1332
 *   REG_EXPAND_SZ:
 *     case-1: the unexpanded string is smaller than the expanded one
1333
 *       subcase-1: the buffer is too small to hold the unexpanded string:
Juergen Schmied's avatar
Juergen Schmied committed
1334 1335
 *          function fails and returns the size of the unexpanded string.
 *
1336
 *       subcase-2: buffer is too small to hold the expanded string:
Juergen Schmied's avatar
Juergen Schmied committed
1337
 *          the function return success (!!) and the result is truncated
1338
 *	    *** This is clearly an error in the native implementation. ***
Juergen Schmied's avatar
Juergen Schmied committed
1339
 *
Jon Griffiths's avatar
Jon Griffiths committed
1340
 *     case-2: the unexpanded string is bigger than the expanded one
Juergen Schmied's avatar
Juergen Schmied committed
1341 1342 1343
 *       The buffer must have enough space to hold the unexpanded
 *       string even if the result is smaller.
 *
1344
 */
1345 1346 1347
DWORD WINAPI SHQueryValueExA( HKEY hKey, LPCSTR lpszValue,
                              LPDWORD lpReserved, LPDWORD pwType,
                              LPVOID pvData, LPDWORD pcbData)
1348
{
1349 1350
  DWORD dwRet, dwType, dwUnExpDataLen = 0, dwExpDataLen;

1351
  TRACE("(hkey=%p,%s,%p,%p,%p,%p=%d)\n", hKey, debugstr_a(lpszValue),
1352 1353
        lpReserved, pwType, pvData, pcbData, pcbData ? *pcbData : 0);

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
  if (pcbData) dwUnExpDataLen = *pcbData;

  dwRet = RegQueryValueExA(hKey, lpszValue, lpReserved, &dwType, pvData, &dwUnExpDataLen);

  if (pcbData && (dwType == REG_EXPAND_SZ))
  {
    DWORD nBytesToAlloc;

    /* Expand type REG_EXPAND_SZ into REG_SZ */
    LPSTR szData;

1365
    /* If the caller didn't supply a buffer or the buffer is too small we have
1366 1367 1368
     * to allocate our own
     */
    if ((!pvData) || (dwRet == ERROR_MORE_DATA) )
1369
    {
1370
      char cNull = '\0';
1371
      nBytesToAlloc = dwUnExpDataLen;
1372

1373
      szData = LocalAlloc(LMEM_ZEROINIT, nBytesToAlloc);
1374 1375 1376
      RegQueryValueExA (hKey, lpszValue, lpReserved, NULL, (LPBYTE)szData, &nBytesToAlloc);
      dwExpDataLen = ExpandEnvironmentStringsA(szData, &cNull, 1);
      dwUnExpDataLen = max(nBytesToAlloc, dwExpDataLen);
1377
      LocalFree(szData);
1378 1379 1380
    }
    else
    {
1381
      nBytesToAlloc = (lstrlenA(pvData)+1) * sizeof (CHAR);
1382
      szData = LocalAlloc(LMEM_ZEROINIT, nBytesToAlloc);
1383 1384
      lstrcpyA(szData, pvData);
      dwExpDataLen = ExpandEnvironmentStringsA(szData, pvData, *pcbData / sizeof(CHAR));
1385
      if (dwExpDataLen > *pcbData) dwRet = ERROR_MORE_DATA;
1386
      dwUnExpDataLen = max(nBytesToAlloc, dwExpDataLen);
1387
      LocalFree(szData);
1388 1389 1390 1391 1392 1393 1394 1395
    }
  }

  /* Update the type and data size if the caller wanted them */
  if ( dwType == REG_EXPAND_SZ ) dwType = REG_SZ;
  if ( pwType ) *pwType = dwType;
  if ( pcbData ) *pcbData = dwUnExpDataLen;
  return dwRet;
1396
}
1397

1398

1399 1400 1401
/*************************************************************************
 * SHQueryValueExW   [SHLWAPI.@]
 *
1402
 * See SHQueryValueExA.
1403
 */
1404 1405 1406
DWORD WINAPI SHQueryValueExW(HKEY hKey, LPCWSTR lpszValue,
                             LPDWORD lpReserved, LPDWORD pwType,
                             LPVOID pvData, LPDWORD pcbData)
1407
{
1408 1409
  DWORD dwRet, dwType, dwUnExpDataLen = 0, dwExpDataLen;

1410
  TRACE("(hkey=%p,%s,%p,%p,%p,%p=%d)\n", hKey, debugstr_w(lpszValue),
1411 1412
        lpReserved, pwType, pvData, pcbData, pcbData ? *pcbData : 0);

1413 1414 1415
  if (pcbData) dwUnExpDataLen = *pcbData;

  dwRet = RegQueryValueExW(hKey, lpszValue, lpReserved, &dwType, pvData, &dwUnExpDataLen);
1416 1417
  if (dwRet!=ERROR_SUCCESS && dwRet!=ERROR_MORE_DATA)
      return dwRet;
1418 1419 1420 1421 1422 1423 1424 1425

  if (pcbData && (dwType == REG_EXPAND_SZ))
  {
    DWORD nBytesToAlloc;

    /* Expand type REG_EXPAND_SZ into REG_SZ */
    LPWSTR szData;

1426
    /* If the caller didn't supply a buffer or the buffer is too small we have
1427 1428 1429
     * to allocate our own
     */
    if ((!pvData) || (dwRet == ERROR_MORE_DATA) )
1430
    {
1431
      WCHAR cNull = '\0';
1432
      nBytesToAlloc = dwUnExpDataLen;
1433

1434
      szData = LocalAlloc(LMEM_ZEROINIT, nBytesToAlloc);
1435 1436 1437
      RegQueryValueExW (hKey, lpszValue, lpReserved, NULL, (LPBYTE)szData, &nBytesToAlloc);
      dwExpDataLen = ExpandEnvironmentStringsW(szData, &cNull, 1);
      dwUnExpDataLen = max(nBytesToAlloc, dwExpDataLen);
1438
      LocalFree(szData);
1439 1440 1441
    }
    else
    {
1442
      nBytesToAlloc = (lstrlenW(pvData) + 1) * sizeof(WCHAR);
1443
      szData = LocalAlloc(LMEM_ZEROINIT, nBytesToAlloc);
1444 1445
      lstrcpyW(szData, pvData);
      dwExpDataLen = ExpandEnvironmentStringsW(szData, pvData, *pcbData/sizeof(WCHAR) );
1446
      if (dwExpDataLen > *pcbData) dwRet = ERROR_MORE_DATA;
1447
      dwUnExpDataLen = max(nBytesToAlloc, dwExpDataLen);
1448
      LocalFree(szData);
1449 1450 1451 1452 1453 1454 1455 1456
    }
  }

  /* Update the type and data size if the caller wanted them */
  if ( dwType == REG_EXPAND_SZ ) dwType = REG_SZ;
  if ( pwType ) *pwType = dwType;
  if ( pcbData ) *pcbData = dwUnExpDataLen;
  return dwRet;
1457 1458 1459 1460
}

/*************************************************************************
 * SHDeleteKeyA   [SHLWAPI.@]
1461
 *
1462
 * Delete a registry key and any sub keys/values present
1463
 *
1464 1465 1466
 * This function forwards to the unicode version directly, to avoid
 * handling subkeys that are not representable in ASCII.
 *
1467 1468 1469 1470 1471 1472
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key to delete
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. The key is deleted.
Jon Griffiths's avatar
Jon Griffiths committed
1473 1474
 *   Failure: An error code from RegOpenKeyExA(), RegQueryInfoKeyA(),
 *            RegEnumKeyExA() or RegDeleteKeyA().
1475
 */
1476
DWORD WINAPI SHDeleteKeyA(HKEY hKey, LPCSTR lpszSubKey)
1477
{
1478
  WCHAR subkeyW[MAX_PATH];
1479

1480 1481
  MultiByteToWideChar (CP_ACP, 0, lpszSubKey, -1, subkeyW, sizeof(subkeyW)/sizeof(WCHAR));
  return SHDeleteKeyW(hKey, subkeyW);
1482 1483 1484 1485 1486 1487 1488 1489 1490
}

/*************************************************************************
 * SHDeleteKeyW   [SHLWAPI.@]
 *
 * See SHDeleteKeyA.
 */
DWORD WINAPI SHDeleteKeyW(HKEY hKey, LPCWSTR lpszSubKey)
{
1491
  DWORD dwRet, dwMaxSubkeyLen = 0, dwSize;
1492 1493 1494
  WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
  HKEY hSubKey = 0;

1495
  TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1496 1497 1498 1499

  dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
  if(!dwRet)
  {
1500 1501
    /* Find the maximum subkey length so that we can allocate a buffer */
    dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, NULL,
1502 1503
                             &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
    if(!dwRet)
1504
    {
1505 1506 1507 1508 1509 1510 1511 1512 1513
      dwMaxSubkeyLen++;
      if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
        /* Name too big: alloc a buffer for it */
        lpszName = HeapAlloc(GetProcessHeap(), 0, dwMaxSubkeyLen*sizeof(WCHAR));

      if(!lpszName)
        dwRet = ERROR_NOT_ENOUGH_MEMORY;
      else
      {
1514
        while (dwRet == ERROR_SUCCESS)
1515 1516
        {
          dwSize = dwMaxSubkeyLen;
1517 1518
          dwRet = RegEnumKeyExW(hSubKey, 0, lpszName, &dwSize, NULL, NULL, NULL, NULL);
          if (dwRet == ERROR_SUCCESS || dwRet == ERROR_MORE_DATA)
1519 1520
            dwRet = SHDeleteKeyW(hSubKey, lpszName);
        }
1521 1522 1523
        if (dwRet == ERROR_NO_MORE_ITEMS)
          dwRet = ERROR_SUCCESS;
    
1524 1525 1526
        if (lpszName != szNameBuf)
          HeapFree(GetProcessHeap(), 0, lpszName); /* Free buffer if allocated */
      }
1527 1528
    }

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
    RegCloseKey(hSubKey);
    if(!dwRet)
      dwRet = RegDeleteKeyW(hKey, lpszSubKey);
  }
  return dwRet;
}

/*************************************************************************
 * SHDeleteEmptyKeyA   [SHLWAPI.@]
 *
 * Delete a registry key with no sub keys.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key to delete
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. The key is deleted.
 *   Failure: If the key is not empty, returns ERROR_KEY_HAS_CHILDREN. Otherwise
Jon Griffiths's avatar
Jon Griffiths committed
1548 1549
 *            returns an error code from RegOpenKeyExA(), RegQueryInfoKeyA() or
 *            RegDeleteKeyA().
1550 1551 1552 1553 1554 1555
 */
DWORD WINAPI SHDeleteEmptyKeyA(HKEY hKey, LPCSTR lpszSubKey)
{
  DWORD dwRet, dwKeyCount = 0;
  HKEY hSubKey = 0;

1556
  TRACE("(hkey=%p,%s)\n", hKey, debugstr_a(lpszSubKey));
1557 1558 1559 1560 1561 1562 1563 1564

  dwRet = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
  if(!dwRet)
  {
    dwRet = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, &dwKeyCount,
                             NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    RegCloseKey(hSubKey);
    if(!dwRet)
1565
    {
1566 1567 1568 1569
      if (!dwKeyCount)
        dwRet = RegDeleteKeyA(hKey, lpszSubKey);
      else
        dwRet = ERROR_KEY_HAS_CHILDREN;
1570
    }
1571 1572 1573
  }
  return dwRet;
}
1574

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
/*************************************************************************
 * SHDeleteEmptyKeyW   [SHLWAPI.@]
 *
 * See SHDeleteEmptyKeyA.
 */
DWORD WINAPI SHDeleteEmptyKeyW(HKEY hKey, LPCWSTR lpszSubKey)
{
  DWORD dwRet, dwKeyCount = 0;
  HKEY hSubKey = 0;

1585
  TRACE("(hkey=%p, %s)\n", hKey, debugstr_w(lpszSubKey));
1586

1587 1588 1589 1590 1591
  dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
  if(!dwRet)
  {
    dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
                             NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1592
    RegCloseKey(hSubKey);
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    if(!dwRet)
    {
      if (!dwKeyCount)
        dwRet = RegDeleteKeyW(hKey, lpszSubKey);
      else
        dwRet = ERROR_KEY_HAS_CHILDREN;
    }
  }
  return dwRet;
}

/*************************************************************************
 * SHDeleteOrphanKeyA   [SHLWAPI.@]
 *
 * Delete a registry key with no sub keys or values.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key to possibly delete
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. The key has been deleted if it was an orphan.
Jon Griffiths's avatar
Jon Griffiths committed
1615
 *   Failure: An error from RegOpenKeyExA(), RegQueryValueExA(), or RegDeleteKeyA().
1616 1617 1618 1619 1620 1621
 */
DWORD WINAPI SHDeleteOrphanKeyA(HKEY hKey, LPCSTR lpszSubKey)
{
  HKEY hSubKey;
  DWORD dwKeyCount = 0, dwValueCount = 0, dwRet;

1622
  TRACE("(hkey=%p,%s)\n", hKey, debugstr_a(lpszSubKey));
1623 1624

  dwRet = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1625

1626 1627 1628 1629 1630
  if(!dwRet)
  {
    /* Get subkey and value count */
    dwRet = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, &dwKeyCount,
                             NULL, NULL, &dwValueCount, NULL, NULL, NULL, NULL);
1631

1632 1633 1634 1635 1636 1637 1638
    if(!dwRet && !dwKeyCount && !dwValueCount)
    {
      dwRet = RegDeleteKeyA(hKey, lpszSubKey);
    }
    RegCloseKey(hSubKey);
  }
  return dwRet;
1639 1640 1641
}

/*************************************************************************
1642
 * SHDeleteOrphanKeyW   [SHLWAPI.@]
1643
 *
1644
 * See SHDeleteOrphanKeyA.
1645
 */
1646
DWORD WINAPI SHDeleteOrphanKeyW(HKEY hKey, LPCWSTR lpszSubKey)
1647
{
1648 1649 1650
  HKEY hSubKey;
  DWORD dwKeyCount = 0, dwValueCount = 0, dwRet;

1651
  TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

  dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);

  if(!dwRet)
  {
    /* Get subkey and value count */
    dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
                             NULL, NULL, &dwValueCount, NULL, NULL, NULL, NULL);

    if(!dwRet && !dwKeyCount && !dwValueCount)
    {
      dwRet = RegDeleteKeyW(hKey, lpszSubKey);
    }
    RegCloseKey(hSubKey);
  }
  return dwRet;
1668
}
1669

1670 1671 1672
/*************************************************************************
 * SHDeleteValueA   [SHLWAPI.@]
 *
1673 1674 1675 1676 1677 1678 1679 1680 1681
 * Delete a value from the registry.
 *
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   lpszSubKey [I] Name of sub key containing value to delete
 *   lpszValue  [I] Name of value to delete
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. The value is deleted.
Jon Griffiths's avatar
Jon Griffiths committed
1682
 *   Failure: An error code from RegOpenKeyExA() or RegDeleteValueA().
1683
 */
1684 1685 1686 1687 1688
DWORD WINAPI SHDeleteValueA(HKEY hKey, LPCSTR lpszSubKey, LPCSTR lpszValue)
{
  DWORD dwRet;
  HKEY hSubKey;

1689
  TRACE("(hkey=%p,%s,%s)\n", hKey, debugstr_a(lpszSubKey), debugstr_a(lpszValue));
1690 1691 1692 1693 1694 1695 1696 1697

  dwRet = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_SET_VALUE, &hSubKey);
  if (!dwRet)
  {
    dwRet = RegDeleteValueA(hSubKey, lpszValue);
    RegCloseKey(hSubKey);
  }
  return dwRet;
1698 1699 1700 1701 1702
}

/*************************************************************************
 * SHDeleteValueW   [SHLWAPI.@]
 *
1703
 * See SHDeleteValueA.
1704
 */
1705 1706 1707 1708 1709
DWORD WINAPI SHDeleteValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue)
{
  DWORD dwRet;
  HKEY hSubKey;

1710
  TRACE("(hkey=%p,%s,%s)\n", hKey, debugstr_w(lpszSubKey), debugstr_w(lpszValue));
1711 1712 1713 1714 1715 1716 1717 1718

  dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_SET_VALUE, &hSubKey);
  if (!dwRet)
  {
    dwRet = RegDeleteValueW(hSubKey, lpszValue);
    RegCloseKey(hSubKey);
  }
  return dwRet;
1719 1720
}

1721
/*************************************************************************
1722 1723 1724
 * SHEnumKeyExA   [SHLWAPI.@]
 *
 * Enumerate sub keys in a registry key.
1725
 *
1726 1727 1728 1729 1730
 * PARAMS
 *   hKey       [I] Handle to registry key
 *   dwIndex    [I] Index of key to enumerate
 *   lpszSubKey [O] Pointer updated with the subkey name
 *   pwLen      [O] Pointer updated with the subkey length
1731
 *
Jon Griffiths's avatar
Jon Griffiths committed
1732
 * RETURNS
1733
 *   Success: ERROR_SUCCESS. lpszSubKey and pwLen are updated.
Jon Griffiths's avatar
Jon Griffiths committed
1734
 *   Failure: An error code from RegEnumKeyExA().
1735
 */
1736 1737
LONG WINAPI SHEnumKeyExA(HKEY hKey, DWORD dwIndex, LPSTR lpszSubKey,
                         LPDWORD pwLen)
1738
{
1739
  TRACE("(hkey=%p,%d,%s,%p)\n", hKey, dwIndex, debugstr_a(lpszSubKey), pwLen);
1740

1741 1742
  return RegEnumKeyExA(hKey, dwIndex, lpszSubKey, pwLen, NULL, NULL, NULL, NULL);
}
1743

1744 1745 1746 1747 1748 1749 1750 1751
/*************************************************************************
 * SHEnumKeyExW   [SHLWAPI.@]
 *
 * See SHEnumKeyExA.
 */
LONG WINAPI SHEnumKeyExW(HKEY hKey, DWORD dwIndex, LPWSTR lpszSubKey,
                         LPDWORD pwLen)
{
1752
  TRACE("(hkey=%p,%d,%s,%p)\n", hKey, dwIndex, debugstr_w(lpszSubKey), pwLen);
1753

1754 1755
  return RegEnumKeyExW(hKey, dwIndex, lpszSubKey, pwLen, NULL, NULL, NULL, NULL);
}
1756

1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
/*************************************************************************
 * SHEnumValueA   [SHLWAPI.@]
 *
 * Enumerate values in a registry key.
 *
 * PARAMS
 *   hKey      [I] Handle to registry key
 *   dwIndex   [I] Index of key to enumerate
 *   lpszValue [O] Pointer updated with the values name
 *   pwLen     [O] Pointer updated with the values length
 *   pwType    [O] Pointer updated with the values type
 *   pvData    [O] Pointer updated with the values data
 *   pcbData   [O] Pointer updated with the values size
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. Output parameters are updated.
Jon Griffiths's avatar
Jon Griffiths committed
1773
 *   Failure: An error code from RegEnumValueA().
1774 1775 1776 1777 1778
 */
LONG WINAPI SHEnumValueA(HKEY hKey, DWORD dwIndex, LPSTR lpszValue,
                         LPDWORD pwLen, LPDWORD pwType,
                         LPVOID pvData, LPDWORD pcbData)
{
1779
  TRACE("(hkey=%p,%d,%s,%p,%p,%p,%p)\n", hKey, dwIndex,
1780
        debugstr_a(lpszValue), pwLen, pwType, pvData, pcbData);
1781

1782 1783
  return RegEnumValueA(hKey, dwIndex, lpszValue, pwLen, NULL,
                       pwType, pvData, pcbData);
1784 1785 1786
}

/*************************************************************************
1787
 * SHEnumValueW   [SHLWAPI.@]
1788
 *
1789
 * See SHEnumValueA.
1790
 */
1791 1792 1793
LONG WINAPI SHEnumValueW(HKEY hKey, DWORD dwIndex, LPWSTR lpszValue,
                         LPDWORD pwLen, LPDWORD pwType,
                         LPVOID pvData, LPDWORD pcbData)
1794
{
1795
  TRACE("(hkey=%p,%d,%s,%p,%p,%p,%p)\n", hKey, dwIndex,
1796 1797 1798 1799
        debugstr_w(lpszValue), pwLen, pwType, pvData, pcbData);

  return RegEnumValueW(hKey, dwIndex, lpszValue, pwLen, NULL,
                       pwType, pvData, pcbData);
1800 1801
}

1802
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1803
 * @   [SHLWAPI.205]
1804
 *
Jon Griffiths's avatar
Jon Griffiths committed
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
 * Get a value from the registry.
 *
 * PARAMS
 *   hKey    [I] Handle to registry key
 *   pSubKey [I] Name of sub key containing value to get
 *   pValue  [I] Name of value to get
 *   pwType  [O] Destination for the values type
 *   pvData  [O] Destination for the values data
 *   pbData  [O] Destination for the values size
 *
 * RETURNS
 *   Success: ERROR_SUCCESS. Output parameters contain the details read.
 *   Failure: An error code from RegOpenKeyExA() or SHQueryValueExA(),
 *            or ERROR_INVALID_FUNCTION in the machine is in safe mode.
1819
 */
1820
DWORD WINAPI SHGetValueGoodBootA(HKEY hkey, LPCSTR pSubKey, LPCSTR pValue,
1821 1822 1823 1824 1825 1826 1827 1828
                         LPDWORD pwType, LPVOID pvData, LPDWORD pbData)
{
  if (GetSystemMetrics(SM_CLEANBOOT))
    return ERROR_INVALID_FUNCTION;
  return SHGetValueA(hkey, pSubKey, pValue, pwType, pvData, pbData);
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1829
 * @   [SHLWAPI.206]
1830
 *
1831
 * Unicode version of SHGetValueGoodBootW.
1832
 */
1833
DWORD WINAPI SHGetValueGoodBootW(HKEY hkey, LPCWSTR pSubKey, LPCWSTR pValue,
1834 1835 1836 1837 1838 1839 1840 1841
                         LPDWORD pwType, LPVOID pvData, LPDWORD pbData)
{
  if (GetSystemMetrics(SM_CLEANBOOT))
    return ERROR_INVALID_FUNCTION;
  return SHGetValueW(hkey, pSubKey, pValue, pwType, pvData, pbData);
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1842
 * @   [SHLWAPI.320]
1843
 *
1844
 * Set a MIME content type in the registry.
1845 1846
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
1847
 *   lpszSubKey [I] Name of key under HKEY_CLASSES_ROOT.
1848 1849 1850 1851 1852
 *   lpszValue  [I] Value to set
 *
 * RETURNS
 *   Success: TRUE
 *   Failure: FALSE
1853
 */
1854
BOOL WINAPI RegisterMIMETypeForExtensionA(LPCSTR lpszSubKey, LPCSTR lpszValue)
1855
{
1856 1857 1858 1859
  DWORD dwRet;

  if (!lpszValue)
  {
1860
    WARN("Invalid lpszValue would crash under Win32!\n");
1861 1862 1863 1864 1865 1866
    return FALSE;
  }

  dwRet = SHSetValueA(HKEY_CLASSES_ROOT, lpszSubKey, lpszContentTypeA,
                      REG_SZ, lpszValue, strlen(lpszValue));
  return dwRet ? FALSE : TRUE;
1867 1868 1869
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1870
 * @   [SHLWAPI.321]
1871
 *
1872
 * Unicode version of RegisterMIMETypeForExtensionA.
1873
 */
1874
BOOL WINAPI RegisterMIMETypeForExtensionW(LPCWSTR lpszSubKey, LPCWSTR lpszValue)
1875
{
1876 1877 1878 1879
  DWORD dwRet;

  if (!lpszValue)
  {
1880
    WARN("Invalid lpszValue would crash under Win32!\n");
1881 1882 1883 1884 1885 1886
    return FALSE;
  }

  dwRet = SHSetValueW(HKEY_CLASSES_ROOT, lpszSubKey, lpszContentTypeW,
                      REG_SZ, lpszValue, strlenW(lpszValue));
  return dwRet ? FALSE : TRUE;
1887 1888 1889
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1890
 * @   [SHLWAPI.322]
1891
 *
1892
 * Delete a MIME content type from the registry.
1893 1894 1895 1896 1897 1898 1899
 *
 * PARAMS
 *   lpszSubKey [I] Name of sub key
 *
 * RETURNS
 *   Success: TRUE
 *   Failure: FALSE
1900
 */
1901
BOOL WINAPI UnregisterMIMETypeForExtensionA(LPCSTR lpszSubKey)
1902 1903 1904 1905 1906 1907
{
  HRESULT ret = SHDeleteValueA(HKEY_CLASSES_ROOT, lpszSubKey, lpszContentTypeA);
  return ret ? FALSE : TRUE;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1908
 * @   [SHLWAPI.323]
1909
 *
1910
 * Unicode version of UnregisterMIMETypeForExtensionA.
1911
 */
1912
BOOL WINAPI UnregisterMIMETypeForExtensionW(LPCWSTR lpszSubKey)
1913 1914 1915 1916 1917
{
  HRESULT ret = SHDeleteValueW(HKEY_CLASSES_ROOT, lpszSubKey, lpszContentTypeW);
  return ret ? FALSE : TRUE;
}

1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
/*************************************************************************
 * @   [SHLWAPI.328]
 *
 * Get the registry path to a MIME content key.
 *
 * PARAMS
 *   lpszType   [I] Content type to get the path for
 *   lpszBuffer [O] Destination for path
 *   dwLen      [I] Length of lpszBuffer
 *
 * RETURNS
 *   Success: TRUE. lpszBuffer contains the full path.
 *   Failure: FALSE.
 *
 * NOTES
 *   The base path for the key is "MIME\Database\Content Type\"
 */
1935
BOOL WINAPI GetMIMETypeSubKeyA(LPCSTR lpszType, LPSTR lpszBuffer, DWORD dwLen)
1936
{
1937
  TRACE("(%s,%p,%d)\n", debugstr_a(lpszType), lpszBuffer, dwLen);
1938 1939 1940

  if (dwLen > dwLenMimeDbContent && lpszType && lpszBuffer)
  {
Jon Griffiths's avatar
Jon Griffiths committed
1941
    size_t dwStrLen = strlen(lpszType);
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955

    if (dwStrLen < dwLen - dwLenMimeDbContent)
    {
      memcpy(lpszBuffer, szMimeDbContentA, dwLenMimeDbContent);
      memcpy(lpszBuffer + dwLenMimeDbContent, lpszType, dwStrLen + 1);
      return TRUE;
    }
  }
  return FALSE;
}

/*************************************************************************
 * @   [SHLWAPI.329]
 *
1956
 * Unicode version of GetMIMETypeSubKeyA.
1957
 */
1958
BOOL WINAPI GetMIMETypeSubKeyW(LPCWSTR lpszType, LPWSTR lpszBuffer, DWORD dwLen)
1959
{
1960
  TRACE("(%s,%p,%d)\n", debugstr_w(lpszType), lpszBuffer, dwLen);
1961 1962 1963 1964 1965 1966 1967

  if (dwLen > dwLenMimeDbContent && lpszType && lpszBuffer)
  {
    DWORD dwStrLen = strlenW(lpszType);

    if (dwStrLen < dwLen - dwLenMimeDbContent)
    {
1968
      memcpy(lpszBuffer, szMimeDbContentW, dwLenMimeDbContent * sizeof(WCHAR));
1969 1970 1971 1972 1973 1974 1975
      memcpy(lpszBuffer + dwLenMimeDbContent, lpszType, (dwStrLen + 1) * sizeof(WCHAR));
      return TRUE;
    }
  }
  return FALSE;
}

1976 1977 1978
/*************************************************************************
 * @   [SHLWAPI.330]
 *
1979
 * Get the file extension for a given Mime type.
1980 1981 1982
 *
 * PARAMS
 *  lpszType [I] Mime type to get the file extension for
1983 1984
 *  lpExt    [O] Destination for the resulting extension
 *  iLen     [I] Length of lpExt in characters
1985 1986 1987
 *
 * RETURNS
 *  Success: TRUE. lpExt contains the file extension.
1988
 *  Failure: FALSE, if any parameter is invalid or the extension cannot be
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
 *           retrieved. If iLen > 0, lpExt is set to an empty string.
 *
 * NOTES
 *  - The extension returned in lpExt always has a leading '.' character, even
 *  if the registry Mime database entry does not.
 *  - iLen must be long enough for the file extension for this function to succeed.
 */
BOOL WINAPI MIME_GetExtensionA(LPCSTR lpszType, LPSTR lpExt, INT iLen)
{
  char szSubKey[MAX_PATH];
  DWORD dwlen = iLen - 1, dwType;
  BOOL bRet = FALSE;

  if (iLen > 0 && lpExt)
    *lpExt = '\0';

  if (lpszType && lpExt && iLen > 2 &&
      GetMIMETypeSubKeyA(lpszType, szSubKey, MAX_PATH) &&
      !SHGetValueA(HKEY_CLASSES_ROOT, szSubKey, szExtensionA, &dwType, lpExt + 1, &dwlen) &&
      lpExt[1])
  {
    if (lpExt[1] == '.')
      memmove(lpExt, lpExt + 1, strlen(lpExt + 1) + 1);
    else
      *lpExt = '.'; /* Supply a '.' */
    bRet = TRUE;
  }
  return bRet;
}

/*************************************************************************
 * @   [SHLWAPI.331]
 *
 * Unicode version of MIME_GetExtensionA.
 */
BOOL WINAPI MIME_GetExtensionW(LPCWSTR lpszType, LPWSTR lpExt, INT iLen)
{
  WCHAR szSubKey[MAX_PATH];
  DWORD dwlen = iLen - 1, dwType;
  BOOL bRet = FALSE;

  if (iLen > 0 && lpExt)
    *lpExt = '\0';

  if (lpszType && lpExt && iLen > 2 &&
      GetMIMETypeSubKeyW(lpszType, szSubKey, MAX_PATH) &&
      !SHGetValueW(HKEY_CLASSES_ROOT, szSubKey, szExtensionW, &dwType, lpExt + 1, &dwlen) &&
      lpExt[1])
  {
    if (lpExt[1] == '.')
      memmove(lpExt, lpExt + 1, (strlenW(lpExt + 1) + 1) * sizeof(WCHAR));
    else
      *lpExt = '.'; /* Supply a '.' */
    bRet = TRUE;
  }
  return bRet;
}

2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
/*************************************************************************
 * @   [SHLWAPI.324]
 *
 * Set the file extension for a MIME content key.
 *
 * PARAMS
 *   lpszExt  [I] File extension to set
 *   lpszType [I] Content type to set the extension for
 *
 * RETURNS
 *   Success: TRUE. The file extension is set in the registry.
 *   Failure: FALSE.
 */
2060
BOOL WINAPI RegisterExtensionForMIMETypeA(LPCSTR lpszExt, LPCSTR lpszType)
2061 2062 2063 2064 2065 2066
{
  DWORD dwLen;
  char szKey[MAX_PATH];

  TRACE("(%s,%s)\n", debugstr_a(lpszExt), debugstr_a(lpszType));

2067
  if (!GetMIMETypeSubKeyA(lpszType, szKey, MAX_PATH)) /* Get full path to the key */
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
    return FALSE;

  dwLen = strlen(lpszExt) + 1;

  if (SHSetValueA(HKEY_CLASSES_ROOT, szKey, szExtensionA, REG_SZ, lpszExt, dwLen))
    return FALSE;
  return TRUE;
}

/*************************************************************************
 * @   [SHLWAPI.325]
 *
2080
 * Unicode version of RegisterExtensionForMIMETypeA.
2081
 */
2082
BOOL WINAPI RegisterExtensionForMIMETypeW(LPCWSTR lpszExt, LPCWSTR lpszType)
2083 2084 2085 2086 2087 2088 2089
{
  DWORD dwLen;
  WCHAR szKey[MAX_PATH];

  TRACE("(%s,%s)\n", debugstr_w(lpszExt), debugstr_w(lpszType));

  /* Get the full path to the key */
2090
  if (!GetMIMETypeSubKeyW(lpszType, szKey, MAX_PATH)) /* Get full path to the key */
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
    return FALSE;

  dwLen = (lstrlenW(lpszExt) + 1) * sizeof(WCHAR);

  if (SHSetValueW(HKEY_CLASSES_ROOT, szKey, szExtensionW, REG_SZ, lpszExt, dwLen))
    return FALSE;
  return TRUE;
}

/*************************************************************************
 * @   [SHLWAPI.326]
 *
 * Delete a file extension from a MIME content type.
 *
 * PARAMS
 *   lpszType [I] Content type to delete the extension for
 *
 * RETURNS
 *   Success: TRUE. The file extension is deleted from the registry.
 *   Failure: FALSE. The extension may have been removed but the key remains.
 *
 * NOTES
 *   If deleting the extension leaves an orphan key, the key is removed also.
 */
2115
BOOL WINAPI UnregisterExtensionForMIMETypeA(LPCSTR lpszType)
2116 2117 2118 2119 2120
{
  char szKey[MAX_PATH];

  TRACE("(%s)\n", debugstr_a(lpszType));

2121
  if (!GetMIMETypeSubKeyA(lpszType, szKey, MAX_PATH)) /* Get full path to the key */
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
    return FALSE;

  if (!SHDeleteValueA(HKEY_CLASSES_ROOT, szKey, szExtensionA))
    return FALSE;

  if (!SHDeleteOrphanKeyA(HKEY_CLASSES_ROOT, szKey))
    return FALSE;
  return TRUE;
}

/*************************************************************************
 * @   [SHLWAPI.327]
 *
2135
 * Unicode version of UnregisterExtensionForMIMETypeA.
2136
 */
2137
BOOL WINAPI UnregisterExtensionForMIMETypeW(LPCWSTR lpszType)
2138 2139 2140 2141 2142
{
  WCHAR szKey[MAX_PATH];

  TRACE("(%s)\n", debugstr_w(lpszType));

2143
  if (!GetMIMETypeSubKeyW(lpszType, szKey, MAX_PATH)) /* Get full path to the key */
2144 2145 2146 2147 2148 2149 2150 2151 2152
    return FALSE;

  if (!SHDeleteValueW(HKEY_CLASSES_ROOT, szKey, szExtensionW))
    return FALSE;

  if (!SHDeleteOrphanKeyW(HKEY_CLASSES_ROOT, szKey))
    return FALSE;
  return TRUE;
}
2153 2154 2155

/*************************************************************************
 * SHRegDuplicateHKey   [SHLWAPI.@]
Jon Griffiths's avatar
Jon Griffiths committed
2156 2157 2158 2159 2160 2161 2162 2163
 *
 * Create a duplicate of a registry handle.
 *
 * PARAMS
 *  hKey [I] key to duplicate.
 *
 * RETURNS
 *  A new handle pointing to the same key as hKey.
2164 2165 2166 2167 2168 2169
 */
HKEY WINAPI SHRegDuplicateHKey(HKEY hKey)
{
    HKEY newKey = 0;

    RegOpenKeyExA(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
2170
    TRACE("new key is %p\n", newKey);
2171 2172
    return newKey;
}
2173 2174 2175 2176 2177 2178 2179 2180 2181


/*************************************************************************
 * SHCopyKeyA	[SHLWAPI.@]
 *
 * Copy a key and its values/sub keys to another location.
 *
 * PARAMS
 *  hKeySrc    [I] Source key to copy from
2182
 *  lpszSrcSubKey [I] Sub key under hKeySrc, or NULL to use hKeySrc directly
2183
 *  hKeyDst    [I] Destination key
2184 2185 2186 2187 2188 2189 2190 2191
 *  dwReserved [I] Reserved, must be 0
 *
 * RETURNS
 *  Success: ERROR_SUCCESS. The key is copied to the destination key.
 *  Failure: A standard windows error code.
 *
 * NOTES
 *  If hKeyDst is a key under hKeySrc, this function will misbehave
Jon Griffiths's avatar
Jon Griffiths committed
2192 2193
 *  (It will loop until out of stack, or the registry is full). This
 *  bug is present in Win32 also.
2194
 */
2195
DWORD WINAPI SHCopyKeyA(HKEY hKeySrc, LPCSTR lpszSrcSubKey, HKEY hKeyDst, DWORD dwReserved)
2196 2197 2198
{
  WCHAR szSubKeyW[MAX_PATH];

2199
  TRACE("(hkey=%p,%s,%p08x,%d)\n", hKeySrc, debugstr_a(lpszSrcSubKey), hKeyDst, dwReserved);
2200

2201 2202
  if (lpszSrcSubKey)
    MultiByteToWideChar(0, 0, lpszSrcSubKey, -1, szSubKeyW, MAX_PATH);
2203

2204
  return SHCopyKeyW(hKeySrc, lpszSrcSubKey ? szSubKeyW : NULL, hKeyDst, dwReserved);
2205 2206 2207 2208 2209 2210 2211
}

/*************************************************************************
 * SHCopyKeyW	[SHLWAPI.@]
 *
 * See SHCopyKeyA.
 */
2212
DWORD WINAPI SHCopyKeyW(HKEY hKeySrc, LPCWSTR lpszSrcSubKey, HKEY hKeyDst, DWORD dwReserved)
2213 2214 2215 2216
{
  DWORD dwKeyCount = 0, dwValueCount = 0, dwMaxKeyLen = 0;
  DWORD  dwMaxValueLen = 0, dwMaxDataLen = 0, i;
  BYTE buff[1024];
2217
  LPVOID lpBuff = buff;
2218 2219 2220
  WCHAR szName[MAX_PATH], *lpszName = szName;
  DWORD dwRet = S_OK;

2221
  TRACE("hkey=%p,%s,%p08x,%d)\n", hKeySrc, debugstr_w(lpszSrcSubKey), hKeyDst, dwReserved);
2222 2223 2224 2225 2226

  if(!hKeyDst || !hKeySrc)
    dwRet = ERROR_INVALID_PARAMETER;
  else
  {
2227 2228 2229
    /* Open source key */
    if(lpszSrcSubKey)
      dwRet = RegOpenKeyExW(hKeySrc, lpszSrcSubKey, 0, KEY_ALL_ACCESS, &hKeySrc);
2230 2231

    if(dwRet)
2232
      hKeyDst = NULL; /* Don't close this key since we didn't open it */
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
    else
    {
      /* Get details about sub keys and values */
      dwRet = RegQueryInfoKeyW(hKeySrc, NULL, NULL, NULL, &dwKeyCount, &dwMaxKeyLen,
                               NULL, &dwValueCount, &dwMaxValueLen, &dwMaxDataLen,
                               NULL, NULL);
      if(!dwRet)
      {
        if (dwMaxValueLen > dwMaxKeyLen)
          dwMaxKeyLen = dwMaxValueLen; /* Get max size for key/value names */

        if (dwMaxKeyLen++ > MAX_PATH - 1)
          lpszName = HeapAlloc(GetProcessHeap(), 0, dwMaxKeyLen * sizeof(WCHAR));

        if (dwMaxDataLen > sizeof(buff))
          lpBuff = HeapAlloc(GetProcessHeap(), 0, dwMaxDataLen);

        if (!lpszName || !lpBuff)
          dwRet = ERROR_NOT_ENOUGH_MEMORY;
      }
    }
  }

  /* Copy all the sub keys */
  for(i = 0; i < dwKeyCount && !dwRet; i++)
  {
    HKEY hSubKeySrc, hSubKeyDst;
    DWORD dwSize = dwMaxKeyLen;

    dwRet = RegEnumKeyExW(hKeySrc, i, lpszName, &dwSize, NULL, NULL, NULL, NULL);

    if(!dwRet)
    {
      /* Open source sub key */
      dwRet = RegOpenKeyExW(hKeySrc, lpszName, 0, KEY_READ, &hSubKeySrc);

      if(!dwRet)
      {
        /* Create destination sub key */
        dwRet = RegCreateKeyW(hKeyDst, lpszName, &hSubKeyDst);

        if(!dwRet)
        {
          /* Recursively copy keys and values from the sub key */
2277
          dwRet = SHCopyKeyW(hSubKeySrc, NULL, hSubKeyDst, 0);
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
          RegCloseKey(hSubKeyDst);
        }
      }
      RegCloseKey(hSubKeySrc);
    }
  }

  /* Copy all the values in this key */
  for (i = 0; i < dwValueCount && !dwRet; i++)
  {
    DWORD dwNameSize = dwMaxKeyLen, dwType, dwLen = dwMaxDataLen;

2290
    dwRet = RegEnumValueW(hKeySrc, i, lpszName, &dwNameSize, NULL, &dwType, lpBuff, &dwLen);
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301

    if (!dwRet)
      dwRet = SHSetValueW(hKeyDst, NULL, lpszName, dwType, lpBuff, dwLen);
  }

  /* Free buffers if allocated */
  if (lpszName != szName)
    HeapFree(GetProcessHeap(), 0, lpszName);
  if (lpBuff != buff)
    HeapFree(GetProcessHeap(), 0, lpBuff);

2302
  if (lpszSrcSubKey && hKeyDst)
2303 2304 2305
    RegCloseKey(hKeyDst);
  return dwRet;
}
Jon Griffiths's avatar
Jon Griffiths committed
2306 2307 2308 2309 2310 2311

/*
 * The following functions are ORDINAL ONLY:
 */

/*************************************************************************
2312
 *      @     [SHLWAPI.280]
Jon Griffiths's avatar
Jon Griffiths committed
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
 *
 * Read an integer value from the registry, falling back to a default.
 *
 * PARAMS
 *  hKey      [I] Registry key to read from
 *  lpszValue [I] Value name to read
 *  iDefault  [I] Default value to return
 *
 * RETURNS
 *  The value contained in the given registry value if present, otherwise
 *  iDefault.
 */
2325
int WINAPI SHRegGetIntW(HKEY hKey, LPCWSTR lpszValue, int iDefault)
Jon Griffiths's avatar
Jon Griffiths committed
2326
{
2327
  TRACE("(%p,%s,%d)\n", hKey, debugstr_w(lpszValue), iDefault);
Jon Griffiths's avatar
Jon Griffiths committed
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342

  if (hKey)
  {
    WCHAR szBuff[32];
    DWORD dwSize = sizeof(szBuff);
    szBuff[0] = '\0';
    SHQueryValueExW(hKey, lpszValue, 0, 0, szBuff, &dwSize);

    if(*szBuff >= '0' && *szBuff <= '9')
      return StrToIntW(szBuff);
  }
  return iDefault;
}

/*************************************************************************
2343
 *      @	[SHLWAPI.343]
Jon Griffiths's avatar
Jon Griffiths committed
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
 *
 * Create or open an explorer ClassId Key.
 *
 * PARAMS
 *  guid      [I] Explorer ClassId key to open
 *  lpszValue [I] Value name under the ClassId Key
 *  bUseHKCU  [I] TRUE=Use HKEY_CURRENT_USER, FALSE=Use HKEY_CLASSES_ROOT
 *  bCreate   [I] TRUE=Create the key if it doesn't exist, FALSE=Don't
 *  phKey     [O] Destination for the resulting key handle
 *
 * RETURNS
 *  Success: S_OK. phKey contains the resulting registry handle.
 *  Failure: An HRESULT error code indicating the problem.
 */
2358
HRESULT WINAPI SHRegGetCLSIDKeyA(REFGUID guid, LPCSTR lpszValue, BOOL bUseHKCU, BOOL bCreate, PHKEY phKey)
Jon Griffiths's avatar
Jon Griffiths committed
2359 2360 2361 2362 2363 2364
{
  WCHAR szValue[MAX_PATH];

  if (lpszValue)
    MultiByteToWideChar(CP_ACP, 0, lpszValue, -1, szValue, sizeof(szValue)/sizeof(WCHAR));

2365
  return SHRegGetCLSIDKeyW(guid, lpszValue ? szValue : NULL, bUseHKCU, bCreate, phKey);
Jon Griffiths's avatar
Jon Griffiths committed
2366 2367 2368
}

/*************************************************************************
2369
 *      @	[SHLWAPI.344]
Jon Griffiths's avatar
Jon Griffiths committed
2370
 *
2371
 * Unicode version of SHRegGetCLSIDKeyA.
Jon Griffiths's avatar
Jon Griffiths committed
2372
 */
2373
HRESULT WINAPI SHRegGetCLSIDKeyW(REFGUID guid, LPCWSTR lpszValue, BOOL bUseHKCU,
Jon Griffiths's avatar
Jon Griffiths committed
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
                           BOOL bCreate, PHKEY phKey)
{
  static const WCHAR szClassIdKey[] = { 'S','o','f','t','w','a','r','e','\\',
    'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
    'E','x','p','l','o','r','e','r','\\','C','L','S','I','D','\\' };
#define szClassIdKeyLen (sizeof(szClassIdKey)/sizeof(WCHAR))
  WCHAR szKey[MAX_PATH];
  DWORD dwRet;
  HKEY hkey;

  /* Create the key string */
  memcpy(szKey, szClassIdKey, sizeof(szClassIdKey));
2387
  SHStringFromGUIDW(guid, szKey + szClassIdKeyLen, 39); /* Append guid */
Jon Griffiths's avatar
Jon Griffiths committed
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403

  if(lpszValue)
  {
    szKey[szClassIdKeyLen + 39] = '\\';
    strcpyW(szKey + szClassIdKeyLen + 40, lpszValue); /* Append value name */
  }

  hkey = bUseHKCU ? HKEY_CURRENT_USER : HKEY_CLASSES_ROOT;

  if(bCreate)
    dwRet = RegCreateKeyW(hkey, szKey, phKey);
  else
    dwRet = RegOpenKeyExW(hkey, szKey, 0, KEY_READ, phKey);

  return dwRet ? HRESULT_FROM_WIN32(dwRet) : S_OK;
}
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434

/*************************************************************************
 * SHRegisterValidateTemplate	[SHLWAPI.@]
 *
 * observed from the ie 5.5 installer:
 *  - allocates a buffer with the size of the given file
 *  - read the file content into the buffer
 *  - creates the key szTemplateKey
 *  - sets "205523652929647911071668590831910975402"=dword:00002e37 at
 *    the key
 *
 * PARAMS
 *  filename [I] An existing file its content is read into an allocated
 *               buffer
 *  unknown  [I]
 *
 * RETURNS
 *  Success: ERROR_SUCCESS.
 */
HRESULT WINAPI SHRegisterValidateTemplate(LPCWSTR filename, BOOL unknown)
{
/* static const WCHAR szTemplateKey[] = { 'S','o','f','t','w','a','r','e','\\',
 *  'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
 *  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
 *  'E','x','p','l','o','r','e','r','\\',
 *  'T','e','m','p','l','a','t','e','R','e','g','i','s','t','r','y',0 };
 */
  FIXME("stub: %s, %08x\n", debugstr_w(filename), unknown);

  return S_OK;
}