RegQueryValueEx Doesn't return data

This is a discussion on RegQueryValueEx Doesn't return data within the Windows Programming forums, part of the Platform Specific Boards category; Hello.. I'm writing a Euchre program because I'm bored >.< and I let the player name the teams which exist ...

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    RegQueryValueEx Doesn't return data

    Hello.. I'm writing a Euchre program because I'm bored >.< and I let the player name the teams which exist so its easier to display the scores. So when the program starts, the user inputs two team names and they're stored in the registry. Everything up to here works fine. If the user has already set team names, I want to read them in from the registry and display them on the controls. I have no idea why, but this part isn't work. I tried having two separate values Team1 and Team2 and tried condensing them into one value called Teams. As two separate values, Team2 is read in and displayed, but Team1 is null. As one value, nothing is read in. Here's the code I'm using..

    Each team with a separate value:
    Code:
    void set_names(HWND hwnd)
    {
         HINSTANCE hInst = GetModuleHandle(NULL);
         int i = 0;
         char *buf;
         int len = GetWindowTextLength(GetDlgItem(hwnd, ID_STAT_PLAYER3)), teams = 0;
         char pname[256] = {0}, t1name[256] = {0}, t2name[256] = {0};
         LONG lRet, lRetStatus, lRetTeam;
         HKEY hKey;
         DWORD dwLength = 100, dwType = REG_SZ, dwSize = 255;
         
         lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\ShibbyInc", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
         
         lRetStatus = RegQueryValueEx(hKey, "Name", NULL, &dwType, (LPBYTE)&pname, &dwSize);
         if (lRetStatus == ERROR_SUCCESS)
         {
             SetDlgItemText(hwnd, ID_STAT_PLAYER3, pname);
         }
         else
         {    
             RegCloseKey(hKey);
             DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
        }
        
        lRetTeam = RegQueryValueEx(hKey, "Team1", NULL, &dwType, (LPBYTE)&t1name, &dwSize);
        ErrorFunc("RegQueryValueEx for Team 1");
        if (lRetTeam == ERROR_SUCCESS)
        {
            MessageBox(0, t1name, "Team 1", MB_OK);
            for (i = ID_STAT1_TEAM1; i <= ID_STAT3_TEAM1; i++)
            {
                if (SetDlgItemText(g_hwnd, i, t1name) == 0)
    		       ErrorFunc("SetDlgItemText");
            }
        }
        else
        {
            ErrorFunc("RegQueryValueEx");
            MessageBox(0, t1name, "Team 1", MB_OK);
            teams = 1;
        }
        
        lRetTeam = RegQueryValueEx(hKey, "Team2", NULL, &dwType, (LPBYTE)&t2name, &dwSize);
        if (lRetTeam == ERROR_SUCCESS)
        {
            for (i = ID_STAT1_TEAM2; i <= ID_STAT3_TEAM2; i++)
            {
                if (SetDlgItemText(g_hwnd, i, t2name) == 0)
    		       ErrorFunc("SetDlgItemText");
            }
        }
        else
            teams = 1;
            
        if (teams == 1)
        {
            RegCloseKey(hKey);
            DialogBox(hInst, MAKEINTRESOURCE(IDD_TNAME), NULL, TDlgProc);
        }
    }
    Both team names in one value:
    Code:
    void set_names(HWND hwnd)
    {
         HINSTANCE hInst = GetModuleHandle(NULL);
         int i = 0, x = 0;
         char *buf;
         int len = GetWindowTextLength(GetDlgItem(hwnd, ID_STAT_PLAYER3));
         char pname[256] = {0}, teams[256] = {0}, t1name[128] = {0}, t2name[128] = {0};
         LONG lRet, lRetStatus, lRetTeam;
         HKEY hKey;
         DWORD dwLength = 100, dwType = REG_SZ, dwSize = 255;
         
         lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\ShibbyInc", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
         
         lRetStatus = RegQueryValueEx(hKey, "Name", NULL, &dwType, (LPBYTE)&pname, &dwSize);
         if (lRetStatus == ERROR_SUCCESS)
         {
             SetDlgItemText(hwnd, ID_STAT_PLAYER3, pname);
         }
         else
         {    
             RegCloseKey(hKey);
             DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
        }
        RegCloseKey(hKey);
        lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\ShibbyInc", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
    /* I added this code to close and reopen the key thinking that may be a problem
     * but it wasn't and it still doesn't work :[
     */
         
        lRetTeam = RegQueryValueEx(hKey, "Teams", NULL, &dwType, (LPBYTE)&teams, &dwSize);
        MessageBox(0, teams, "Teams", MB_OK);
        if (lRetTeam == ERROR_SUCCESS)
        {
            while (teams[i] != ' ')
            {
                t1name[x] = teams[i];
                ++i;
                ++x;
            }
            ++i;
            x = 0;
            while (teams[i] != '\0')
            {
                t2name[x] = teams[i];
                ++i;
                ++x;
            }
            for (i = ID_STAT1_TEAM1; i <= ID_STAT3_TEAM1; i++)
            {
                if (SetDlgItemText(g_hwnd, i, t1name) == 0)
    		       ErrorFunc("SetDlgItemText");
            }
            for (i = ID_STAT1_TEAM2; i <= ID_STAT3_TEAM2; i++)
            {
                if (SetDlgItemText(g_hwnd, i, t2name) == 0)
    		       ErrorFunc("SetDlgItemText");
            }
        }
        else
        {
            RegCloseKey(hKey);
            DialogBox(hInst, MAKEINTRESOURCE(IDD_TNAME), NULL, TDlgProc);
        }
    }
    (this also shows the code I'm using for the players name which works fine every time)

    any ideas about what's going wrong? I'd greatly appreciate any help :] thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I don't see where you're actually calling RegSetValueEx to add the team names to the registry.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int set_names(void)
    {
        char pname[] = {"Shibby"}, t1name[] = {"Team1"}, t2name[] = {"Team2"};
        HKEY hKey = NULL;
        LPTSTR  lpTeam1 = NULL;
        LPTSTR  lpTeam2 = NULL;
        DWORD dwType = 0, dwSize = 0, dwBufSize = 0, lRet = 0;
    
        lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\ShibbyInc", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
        if(lRet == ERROR_SUCCESS)
        {
            if(RegSetValueEx(hKey, "Name", 0, REG_SZ, (LPBYTE) pname, strlen(pname)+ 1))
            {
                printf("Could not set Name."); 
                RegCloseKey(hKey); 
                return -1;
            }
            if(RegSetValueEx(hKey, "Team1", 0, REG_SZ, (LPBYTE) t1name, strlen(t1name)+ 1))
            {
                printf("Could not set Team1."); 
                RegCloseKey(hKey); 
                return -1;
            }       
            if(RegSetValueEx(hKey, "Team2", 0, REG_SZ, (LPBYTE) t2name, strlen(t2name)+ 1))
            {
                printf("Could not set Team2."); 
                RegCloseKey(hKey); 
                return -1;
            }   
        }
        // dwSize contains the length of the string in bytes
        lRet = RegQueryValueEx(hKey, t1name, 0, &dwType, NULL, &dwSize );
        if (lRet != ERROR_SUCCESS) return -1;
        // Allocate memory for string including null terminator
        dwBufSize = dwSize + (1 * sizeof(char));
        lpTeam1 = (LPTSTR )malloc(dwBufSize);
        if (lpTeam1 == NULL) return -1;
        // Retrieve the string form the registry key
        lRet = RegQueryValueEx(hKey, t1name, 0, &dwType, (LPBYTE) lpTeam1, &dwSize );
        if (lRet != ERROR_SUCCESS)
        { 
            free(lpTeam1);
            return -1 ;
        }
        // At this point, you should check the return string to be sure it has a 
        // null terminator.  I'll leave that up to you 
        printf("%s\n", lpTeam1);
        return 0;
    }
    
    int main(void)
    {
        set_names();
        return 0;
    }

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    It's not in this function :] but like I said, that part of the code works. I can run the program, enter some text in the dialog box, open up regedit and voila - they're there. If you'd really like to see the code for that, I'll post it below but that's not the part giving me a problem as I've confirmed every time that the values existed in the registry :]

    This is the code for the case where both names exist in one value.
    Code:
    BOOL CALLBACK TDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case IDCANCEL:
                         if (MessageBox(0, "Are you sure you want to close the team name dialog?", "Confirm", MB_OKCANCEL) == IDOK)
                          SendMessage(hwnd, WM_CLOSE, 0, 0);
                    break;
                    case IDC_ADD:
    				{
                        HINSTANCE hInst = GetModuleHandle(NULL);
                        char *buf;
                        char pname[256], teams[256] = {0};
                        LONG lRet, lRetStatus;
                        HKEY hKey;
                        DWORD dwLength = 100, dwType = REG_SZ, dwSize = 255;
    				    int i = 0, len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT1)), len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT2));
         
                        lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\ShibbyInc", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
    
                        if(len > 0 && len2 > 0)
                        {  					
                            buf = malloc((len + 1) * sizeof(char));
    						GetDlgItemText(hwnd, IDC_TEXT1, buf, len + 1);						
    						for (i = ID_STAT1_TEAM1; i <= ID_STAT3_TEAM1; i++)
                            {
                                if (SetDlgItemText(g_hwnd, i, buf) == 0)
    						       ErrorFunc("SetDlgItemText");
                            }
                            
                            strcpy(teams, buf);
    						free(buf);
    						
    						buf = malloc((len2 + 1) * sizeof(char));
    						GetDlgItemText(hwnd, IDC_TEXT2, buf, len2 + 1);						
    						for (i = ID_STAT1_TEAM2; i <= ID_STAT3_TEAM2; i++)
                            {
                                if (SetDlgItemText(g_hwnd, i, buf) == 0)
    						       ErrorFunc("SetDlgItemText");
                            }
                            strcat(teams, " ");
                            strcat(teams, buf);
                            len = strlen(teams);
    			            if (RegSetValueEx(hKey, "Teams", 0, dwType, (LPBYTE)teams, (DWORD)len) != ERROR_SUCCESS)
    			               ErrorFunc("RegSetValueEx");
    						free(buf);
    						
    						SendMessage(hwnd, WM_CLOSE, 0, 0);
                       }
                       else 
                       {
    						MessageBox(0, "Please enter a team name in both boxes provided.", "Warning", MB_OK);
                       }
                       
                       RegCloseKey(hKey);
                    }
                    break;
            }
            break;
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    We just don't know whether or not we'll get a null terminated string from the registry. So, I would suggest that you null terminate the input string before calling SetDlgItemText which requires a null terminated string. Your RegQueryValueEx functions do work given the code that you provided.

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    bah.. that stinks. i changed the code for the strcat to this:
    Code:
    strcat(teams, " ");
    strcat(teams, buf);
    len = strlen(teams);
    teams[len] = 0;
    ++len;
    and it still does the same thing. this makes no sense.
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Registry data is wide-chars, isn't it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Registry data is wide-chars, isn't it?
    The data stored in the registry can be ANSI or UNICODE depending upon whether you're using ANSI or UNICODE functions.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I've used your registry functions from the above code exactly "as is" in a console app to determine whether or not RegQueryValueEx returns data. In my console app, the calls to RegQueryValueEx *ALWAYS* return data. I cannot recreate your RegQueryValueEx issue with a basic console app. Thus, I would assume your issue lies with the GUI not with your registry functions.

  9. #9
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    well this is rather...screwy. i modified the part of the code that reads in the team names from the registry to this:
    Code:
             lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\ShibbyInc", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
         while (teams[0] == 0)
         {
             lRetTeam = RegQueryValueEx(hKey, "Teams", NULL, &dwType, (LPBYTE)&teams, &dwSize);
             MessageBox(0, teams, "Teams", MB_OK);
         }
    the first time the prompt appears, there is no text as the variable is null which has been the problem. so it goes through the case again and the second time, it prompts with the data from the registry key. this is the same thing that was happening when there was 2 values, 1 for each team: it wouldn't read in the first but the second reads in fine. ??(O_o)?? i dont get it. lol
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21