2009-11-28 11 views

Respuesta

8

que tienen un código muy antiguo, pero debe darle una buena idea:

/** 
* @param location The location of the registry key. For example "Software\\Bethesda Softworks\\Morrowind" 
* @param name the name of the registry key, for example "Installed Path" 
* @return the value of the key or an empty string if an error occured. 
*/ 
std::string getRegKey(const std::string& location, const std::string& name){ 
    HKEY key; 
    TCHAR value[1024]; 
    DWORD bufLen = 1024*sizeof(TCHAR); 
    long ret; 
    ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, location.c_str(), 0, KEY_QUERY_VALUE, &key); 
    if(ret != ERROR_SUCCESS){ 
     return std::string(); 
    } 
    ret = RegQueryValueExA(key, name.c_str(), 0, 0, (LPBYTE) value, &bufLen); 
    RegCloseKey(key); 
    if ((ret != ERROR_SUCCESS) || (bufLen > 1024*sizeof(TCHAR))){ 
     return std::string(); 
    } 
    std::string stringValue = std::string(value, (size_t)bufLen - 1); 
    size_t i = stringValue.length(); 
    while(i > 0 && stringValue[i-1] == '\0'){ 
     --i; 
    } 
    return stringValue.substr(0,i); 
} 
+0

awesomeness, muchas gracias – Maciek

+0

cómo es la cadena pos compuestos por el camino? – Maciek

+7

Este código es potencialmente inseguro. La documentación de RegQueryValueEx (http://msdn.microsoft.com/en-us/library/ms724911(VS.85).aspx) dice (en la sección Comentarios) que la cadena recuperada podría no tener terminación nula. Además, si es una "cadena múltiple" tendrá un doble terminador nulo que también será mal manejado por este código. –

Cuestiones relacionadas