Code:
int environmentValueAdd(wchar_t* pathToAdd){
HKEY root=HKEY_LOCAL_MACHINE;
wchar_t directorio[]=L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
//wchar_t Path[]=L"Path";
wchar_t Path[]=L"Pate";//---copy of the real path to not break the real.
unsigned long tipoValue= REG_EXPAND_SZ;
HKEY environment;
unsigned long size=1;//asigno un valor no nulo
//i can't use RegGetValue becouse minimus OS is XP 64bits or vista
long returnStatus =RegOpenKeyEx(root,directorio,0,KEY_ALL_ACCESS, &environment);
if (returnStatus != ERROR_SUCCESS){
return 1;
}
returnStatus=RegQueryValueEx(environment , Path , NULL, &tipoValue ,NULL,(LPDWORD)&size);
//pone en size el tamaņo del dato en bytes
if (returnStatus != ERROR_SUCCESS){
RegCloseKey(environment);
return 2;
}
wchar_t* data=(wchar_t*)malloc((unsigned int)size);
if(!data)return 5;
returnStatus =RegQueryValueEx(environment , Path , NULL, &tipoValue ,(LPBYTE)data,(LPDWORD)&size);//datos...
if (returnStatus != ERROR_SUCCESS){
RegCloseKey(environment);
free(data);
return 3;
}
MessageBox(NULL,data,L"REG_EXPAND_SZ",MB_OK);
wchar_t* remplaced=(wchar_t *)malloc((wcslen(data)+ wcslen(pathToAdd) + 3) * sizeof(wchar_t));//uno demas
if(!remplaced)return 6;
wcscpy(remplaced,data);
wcscat(remplaced,L";");
wcscat(remplaced,pathToAdd);
size=(wcslen(remplaced) + 1) * sizeof(wchar_t);
returnStatus =RegSetValueEx(environment,Path,0,tipoValue,(LPBYTE)data,size);
if (returnStatus != ERROR_SUCCESS){
RegCloseKey(environment);
free(data);
return 4;
}
RegFlushKey(environment);
SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM) "Environment", SMTO_ABORTIFHUNG,
5000,(PDWORD_PTR)&returnStatus);
free(data);
returnStatus=RegQueryValueEx(environment , Path , NULL, &tipoValue ,NULL,(LPDWORD)&size);
//pone en size el tamaņo del dato en bytes
if (returnStatus != ERROR_SUCCESS){
RegCloseKey(environment);
return 2;
}
data=(wchar_t*)malloc((unsigned int)size);
if(!data)return 5;
returnStatus =RegQueryValueEx(environment , Path , NULL, &tipoValue ,(LPBYTE)data,(LPDWORD)&size);//datos...
if (returnStatus != ERROR_SUCCESS){
RegCloseKey(environment);
free(data);
return 3;
}
MessageBox(NULL,data,L"REG_EXPAND_SZ",MB_OK);
RegCloseKey(environment);
free(data);
free(remplaced);
return 0;//exito
}
thanks