Beispiel #1
0
int
write_w32_registry_string(const char *root, const char *dir, 
                          const char *name, const char *value)
{
    HKEY root_key, reg_key;
	
    if ( !(root_key = get_root_key(root) ) )
	return -1;

    if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key ) 
         != ERROR_SUCCESS )
	return -1;
	
    if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value, 
                        strlen( value ) ) != ERROR_SUCCESS ) {
        if ( RegCreateKey( root_key, name, &reg_key ) != ERROR_SUCCESS ) {
            RegCloseKey(reg_key);
            return -1;
        }
        if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
                            strlen( value ) ) != ERROR_SUCCESS ) {
            RegCloseKey(reg_key);
            return -1;
        }
    }

    RegCloseKey( reg_key );
	
    return 0;
}
Beispiel #2
0
/****************
 * Return a string from the Win32 Registry or NULL in case of
 * error.  Caller must release the return value.   A NULL for root
 * is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
 * NOTE: The value is allocated with a plain malloc() - use free() and not
 * the usual xfree()!!!
 */
char *
read_w32_registry_string( const char *root, const char *dir, const char *name )
{
    HKEY root_key, key_handle;
    DWORD n1, nbytes, type;
    char *result = NULL;

    if ( !(root_key = get_root_key(root) ) )
	return NULL;

    if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
      {
        if (root)
          return NULL; /* no need for a RegClose, so return direct */
        /* It seems to be common practise to fall back to HKLM. */
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
          return NULL; /* still no need for a RegClose, so return direct */
      }

    nbytes = 1;
    if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) ) {
        if (root)
            goto leave;
        /* Try to fallback to HKLM also vor a missing value.  */
        RegCloseKey (key_handle);
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
            return NULL; /* Nope.  */
        if (RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes))
            goto leave;
    }
    result = malloc( (n1=nbytes+1) );
    if( !result )
	goto leave;
    if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
	free(result); result = NULL;
	goto leave;
    }
    result[nbytes] = 0; /* make sure it is really a string  */
    if (type == REG_EXPAND_SZ && strchr (result, '%')) {
        char *tmp;
        
        n1 += 1000;
        tmp = malloc (n1+1);
        if (!tmp)
            goto leave;
        nbytes = ExpandEnvironmentStrings (result, tmp, n1);
        if (nbytes && nbytes > n1) {
            free (tmp);
            n1 = nbytes;
            tmp = malloc (n1 + 1);
            if (!tmp)
                goto leave;
            nbytes = ExpandEnvironmentStrings (result, tmp, n1);
            if (nbytes && nbytes > n1) {
                free (tmp); /* oops - truncated, better don't expand at all */
                goto leave;
            }
            tmp[nbytes] = 0;
            free (result);
            result = tmp;
        }
        else if (nbytes) { /* okay, reduce the length */
            tmp[nbytes] = 0;
            free (result);
            result = malloc (strlen (tmp)+1);
            if (!result)
                result = tmp;
            else {
                strcpy (result, tmp);
                free (tmp);
            }
        }
        else {  /* error - don't expand */
            free (tmp);
        }
    }

  leave:
    RegCloseKey( key_handle );
    return result;
}
Beispiel #3
0
Datei: w32reg.c Projekt: gpg/gpa
/* Return a string from the Win32 Registry or NULL in case of error.
   Caller must release the return value with g_free ().  A NULL for
   root is an alias for HKEY_CURRENT_USER with a fallback for
   HKEY_LOCAL_MACHINE.  */
char *
read_w32_registry_string (const char *root, const char *dir, const char *name)
{
    HKEY root_key;
    HKEY key_handle;
    DWORD n1;
    DWORD nbytes;
    DWORD type;
    char *result = NULL;

    root_key = get_root_key (root);
    if (! root_key)
        return NULL;

    if (RegOpenKeyEx (root_key, dir, 0, KEY_READ, &key_handle))
    {
        if (root)
            /* No need for a RegClose, so return directly.  */
            return NULL;

        /* It seems to be common practise to fall back to HKLM.  */
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
            /* Still no need for a RegClose, so return directly.  */
            return NULL;
    }

    nbytes = 1;
    if (RegQueryValueEx (key_handle, name, 0, NULL, NULL, &nbytes))
    {
        if (root)
            goto leave;

        /* Try to fallback to HKLM also for a missing value.  */
        RegCloseKey (key_handle);
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
            return NULL;

        if (RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes))
            goto leave;
    }
    n1 = nbytes + 1;
    result = g_malloc (n1);
    if (RegQueryValueEx (key_handle, name, 0, &type, result, &n1))
    {
        g_free (result);
        result = NULL;
        goto leave;
    }

    /* Make sure it is really a string.  */
    result[nbytes] = 0;
    if (type == REG_EXPAND_SZ && strchr (result, '%'))
    {
        char *tmp;

        n1 += 1000;
        tmp = g_malloc (n1 + 1);
        nbytes = ExpandEnvironmentStrings (result, tmp, n1);
        if (nbytes && nbytes > n1)
        {
            free (tmp);
            n1 = nbytes;
            tmp = g_malloc (n1 + 1);
            nbytes = ExpandEnvironmentStrings (result, tmp, n1);
            if (nbytes && nbytes > n1)
            {
                /* Oops: truncated, better don't expand at all.  */
                free (tmp);
                goto leave;
            }
            tmp[nbytes] = 0;
            g_free (result);
            result = tmp;
        }
        else if (nbytes)
        {
            /* Okay, reduce the length.  */
            tmp[nbytes] = 0;
            free (result);
            result = g_malloc (strlen (tmp) + 1);
            strcpy (result, tmp);
            g_free (tmp);
        }
        else
        {
            /* Error - don't expand.  */
            g_free (tmp);
        }
    }

leave:
    RegCloseKey (key_handle);
    return result;
}