Thread: CRegKey

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    CRegKey

    Hi, I'm wondering about something here. How would you detect if a user has a key value in the registry, and if it's not there, create one? The code (at end of post) a friend of mine and me are using doesn't do this. What it does is crash if the m_hKey is NULL.

    Code:
    		m_regApp.Create(HKEY_CURRENT_USER, "Software\\TriFaze\\Jam MP3\\");
    
    		if(m_regApp.m_hKey == NULL){
    			TRACE("Unable to set m_hKey!\n");
    		} else{
    			TRACE("m_hKey set!\n");
    		}
    
    		// Write the new registry settings
    	    m_regApp.Open(HKEY_CURRENT_USER, "Software\\TriFaze\\Jam MP3\\");   // Get last saved directory
    		m_regApp.SetValue(myMusic, "LastDir");    // Set new directory
    		m_regApp.SetValue("255", "LastVolume");    // Set new volume level
    		
    		if(!m_regApp.QueryValue(drive, "SelectDrive", (DWORD*)&m_dwRead)){
    			if(ccd.DoModal() == ID_OK){
    				m_regApp.QueryValue(drive, "SelectDrive", (DWORD*)&m_dwRead);
    			}
    		}
    
    		m_regApp.Close();     // Close the key
    (PS: Thanks in advance for any help)

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try

    "Software\\TriFaze\\Jam MP3"

    and

    Is the handle NULL before the call to Create() ? If not why not?

    then

    test the return from the Create() == ERROR_SUCCESS and if not call GetLastError() to find out why.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by novacain
    try

    "Software\\TriFaze\\Jam MP3"
    Either way works just the same.

    Quote Originally Posted by novacain
    and

    Is the handle NULL before the call to Create() ? If not why not?
    It seems to be before the call.

    Quote Originally Posted by novacain
    then

    test the return from the Create() == ERROR_SUCCESS and if not call GetLastError() to find out why.
    It does return ERROR_SUCCESS.

    ** EDIT: Added results **
    Last edited by Quantrizi; 07-06-2004 at 07:03 AM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try an Open(). If that fails then use a Create().

    It may be the reg key exists and so the m_hkey is not attatched automatically by the Create() (but the Create() still returns a success because the reg key is there)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by novacain
    Try an Open(). If that fails then use a Create().

    It may be the reg key exists and so the m_hkey is not attatched automatically by the Create() (but the Create() still returns a success because the reg key is there)
    I forgot to add that in to the code I posted above, but it was there.

    Anyways though, I decided to do store them in a file, but a new problem arises. When I try to save the directory that was last used, it fails to right the directory (If the directory is C:\Program Files\Pie Is Good\, it'd only save C:\P). Below is the code for it:

    Code:
    #pragma once
    
    #include <stdio.h>
    #include <stdlib.h>
    
    static FILE *fp;
    
    static DWORD dwBytes;
    
    static CString fn;
    
    static BOOL bRW = FALSE;
    static BOOL SaveSetting(char *filename, char *text);
    static BOOL ReadSetting(char *filename, char *store);
    
    BOOL SaveSetting(char *filename, char *text){
    	fn.Empty();
    	fn.Format("%s.jp3", filename);
    
    	fp = fopen(fn, "wt");
    
    	if(fp != NULL){
    		if(fwrite(text, sizeof(char*), strlen(text), fp) != 1){
    			TRACE("fwrite() did not write the file properly!\n");
    			AfxMessageBox("fwrite() failed!");
    
    			return FALSE;
    		} else{
    			TRACE("fwrite() did write the file properly!\n");
    			
    			return TRUE;
    		}
    
    		return TRUE;
    	} else{
    		TRACE("fp is empty.  No file was opened nor created.\n");
    		AfxMessageBox("fp is empty.  No file was opened nor created.");
    
    		return FALSE;
    	}
    
    	return TRUE;
    }

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You should probably modify filename (or fn) to replace '\', which are escape characters, with "\\". For example C:\Program Files\Pie Is Good\ becomes C:\\Program Files\\Pie Is Good\\
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Ken Fitlike
    You should probably modify filename (or fn) to replace '\', which are escape characters, with "\\". For example C:\Program Files\Pie Is Good\ becomes C:\\Program Files\\Pie Is Good\\
    Well, the way the code works is that the file is created in the same directory as the exe. So, I don't think that's the problem, because the other files are written perfectly.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try sizeof(char) instead of sizeof(char*) in your fwrite call:
    Code:
    fwrite(text, sizeof(char), strlen(text), fp)
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Ken Fitlike
    Try sizeof(char) instead of sizeof(char*) in your fwrite call:
    Code:
    fwrite(text, sizeof(char), strlen(text), fp)
    Thank you

    Why would that make a difference though (it worked though)?

    One other thing, now my reading code is causing trouble:

    Code:
    BOOL ReadSetting(char *filename, char *store){
    	fn.Empty();
    	fn.Format("%s.jp3", filename);
    
    	fp = fopen(fn, "rt");
    
    	if(fp != NULL){
    		if(fread(store, sizeof(char), strlen(store), fp) != 1){
    			TRACE("fread() did not read the file properly!\n");
    			AfxMessageBox("fread() failed!");
    
    			return FALSE;
    		} else{
    			TRACE("fread() did read the file properly!\n");
    			
    			return TRUE;
    		}
    
    		return TRUE;
    	} else{
    		TRACE("fp is empty.  No file was opened nor read.\n");
    		AfxMessageBox("fp is empty.  No file was opened nor read.");
    	
    		return FALSE;
    	}
    
    	return TRUE;
    }
    The problem is that I get the "fread() failed!" whenever I try to read the file. Maybe it has to deal w/ the "!= 1" if statement?
    Last edited by Quantrizi; 07-09-2004 at 02:39 PM.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Why would that make a difference though (it worked though)?

    sizeof(char) != sizeof(char*)


    >>if(fread(store, sizeof(char), strlen(store), fp) != 1){


    strlen(store) == the number of characters already in store

    so if store empty == 0

    you want the size of the char array - 1
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atl's cregkey?
    By Johnk in forum Windows Programming
    Replies: 0
    Last Post: 07-10-2002, 07:19 PM