Thread: Registry

  1. #1
    Registered User DutchStud's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Question Registry

    Can someone help me with getting data from the registry. I use these functions. The first works, but the second doesn't. I don't even know if i'm using the right function for the second one.

    PHP Code:
    RegOpenKey(HKEY_CURRENT_USER"Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Explorer\\\\Shell Folders", &key);
    RegQueryValue(key"History"buffer, &ret); 
    By the way, there is that Registry entry, so the error isn't that it doesn't exist. Thanks in advance.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    This gives me my history path -

    Code:
    	HKEY hkey;
    	TCHAR buffer[255];
    	TCHAR sk[] = "Software\\Microsoft\\Windows\\
    		CurrentVersion\\Explorer\\Shell Folders";
    	DWORD a;
    	DWORD b;
     	RegOpenKeyEx(HKEY_CURRENT_USER,sk,0,
    		KEY_QUERY_VALUE ,&hkey);
    	RegQueryValueEx(hkey,"History",0,&a,(BYTE*)buffer,&b);
    	MessageBox(0,buffer,0,0);

  3. #3
    Registered User ExDigit's Avatar
    Join Date
    Jan 2002
    Posts
    31
    Hmm, that code doesn't seem to work Sorensen.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Sorry I broke it up to fit within a window width. Hopefully this is better -

    Code:
    	HKEY hkey;
    	TCHAR buffer[255];
    	TCHAR sk[] = "Software\\Microsoft\\Windows\\"
    		"CurrentVersion\\Explorer\\Shell Folders";
    	DWORD a;
    	DWORD b;
     	RegOpenKeyEx(HKEY_CURRENT_USER,sk,0,
    		KEY_QUERY_VALUE ,&hkey);
    	RegQueryValueEx(hkey,"History",0,&a,(BYTE*)buffer,&b);
    	
    	MessageBox(0,buffer,0,0);

  5. #5
    Registered User ExDigit's Avatar
    Join Date
    Jan 2002
    Posts
    31
    Oh, yeah I realised that, but it just displays an empty message box for me.. does it work on your box?

  6. #6
    Registered User ExDigit's Avatar
    Join Date
    Jan 2002
    Posts
    31
    Okay, I changed the main function into a WinMain function, but now it displays a message box full of garbage. This is the code:
    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLne, int nCmdShow)
    {
    	HKEY hkey;
    	TCHAR buffer[255];
    	TCHAR sk[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
    	DWORD a;
    	DWORD b;
     	RegOpenKeyEx(HKEY_CURRENT_USER,sk,0,KEY_QUERY_VALUE ,&hkey);
    	RegQueryValueEx(hkey,"History",0,&a,(BYTE*)buffer,&b);
    	MessageBox(0,buffer,0,0);
        return 0;
    }
    And it displays the following:
    http://www.codehackers.org/~cdavies/...est2-Error.gif

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Do you have that registry key in your Windows version? If you're coding for potentially different versions of an o/s you'd have to do more thorough error checking than in my code above. Check the return value of RegOpenKeyEx().

    I can't check right now but I'm guessing that the history path is not in the same place in the 9x registry.

  8. #8
    Registered User ExDigit's Avatar
    Join Date
    Jan 2002
    Posts
    31
    Yeah, I have that registry entry and it contains data.. (Windows ME)

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I'm not sure then. Is the space before "Software..." in your code a typo? What is the return value from RegOpenKeyEx()?

  10. #10
    Registered User ExDigit's Avatar
    Join Date
    Jan 2002
    Posts
    31

    It works!

    Hehe, I got it working! I looked in the Win32 API Reference and it said that the first DWORD must be the type of data.. Seeing as we want to receive a zero terminated string, I used REG_SZ. And the second DWORD had to be the buffer size.. you declared the buffer as being able to hold 255 bytes, so that's what I set it to.. Here's the code:
    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLne, int nCmdShow)
    {
    	HKEY hkey;
    	TCHAR buffer[255];
    	TCHAR sk[] = "Software\\Microsoft\\Windows\\CurrentVersion\\"
                     "Explorer\\Shell Folders";
    	DWORD a = REG_SZ; //  <-- Zero terminated string
    	DWORD b = 255;    //  <-- Buffer size
    
     	RegOpenKeyEx(HKEY_CURRENT_USER,
                     sk,
                     0,
                     KEY_QUERY_VALUE,
                     &hkey);
    
        RegQueryValueEx(hkey,
                        "History",
                        0,
                        &a,
                        (BYTE*)buffer,
                        &b);
    
    	MessageBox(0,buffer,0,0);
        return 0;
    }
    Hey, I learnt something too!

  11. #11
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Hey, I learnt something too!
    So did I.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Registry, Regedit
    By franse in forum C++ Programming
    Replies: 21
    Last Post: 01-29-2009, 09:57 AM
  2. Registry HowTo
    By xxxrugby in forum C Programming
    Replies: 2
    Last Post: 04-10-2005, 10:44 AM
  3. Efficient registry use?
    By bennyandthejets in forum Windows Programming
    Replies: 6
    Last Post: 09-28-2003, 06:28 PM
  4. Registry
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 07-30-2003, 04:02 PM
  5. Registry Access
    By ExDigit in forum Windows Programming
    Replies: 3
    Last Post: 01-04-2002, 04:02 AM