Thread: i can't edit a key with RegSetValue

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Argentina
    Posts
    3

    Unhappy i can't edit a key with RegSetValue

    the next code is write by me, first read the size of the string ,then take the string, edit the string and try to modify the keywith RegSetValue, but never change and not return error the function RegSetValue.
    What am I doing wrong?
    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
    Last edited by dreams_eater; 07-31-2009 at 04:36 PM. Reason: highlighted the important words

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    This code is not very correct.
    See MSDN samples (KB, SDK, doc) or on WIN32 ng for more advanced code
    (and you set data instead of environment...)
    Last edited by Alex31; 08-01-2009 at 07:16 AM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Argentina
    Posts
    3
    thanks Alex31.
    yesterday at 24:30 discoverred this error.
    i so stupid.
    now the code works!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an Edit Box (Subclassing)
    By csonx_p in forum Windows Programming
    Replies: 9
    Last Post: 05-05-2008, 06:36 AM
  2. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 07:58 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM