Is it possable to do this

This is a discussion on Is it possable to do this within the C++ Programming forums, part of the General Programming Boards category; Code: if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" >> Skey ), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS) Skey is a variable, is there any ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    20

    Is it possable to do this

    Code:
    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" >> Skey),
    0,
    KEY_READ,
    &hTestKey) == ERROR_SUCCESS)
    Skey is a variable, is there any way to do that?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That's a syntax error. What are you trying to do?
    My best code is written with the delete key.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Looks like he is trying to load the reg file: SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Unin stall\\
    into the var Skey.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    I guess from my understanding of what ive been reading in order to read the values in a regestry key you have to open the exact key first.
    The uninstall key has various subkeys, the Skey variable is gonna be the name of one f the subkeys.. the code above will be looped for each subkey in the uninstall key.

    im trying to open each one so I can grab the values in each like "display name" and uninstall directory, and the version of the software.

    I grabed the code off of the MSDN site and it almost does what I need.. but not quite, which is what im trying to get it to do, it creates a list of subkeys for the uninstall key, but it dosn't grab the values in each of the subkeys, the code to grab the values is there, but I need to open the subkeys first to get it.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    naw this line here

    TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion \\Uninstall\\" >> Skey),

    thats like a pointer kinda... it hooks onto this HKEY_LOCAL_MACHINE

    points to that key in the regestry.. Skey is supposed to be one key below eg: uninstall\\Doom3

    hehe I used the >> like you would use cin hoping it would add it to that.. seeing as Skey is a string

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    ::RegQueryValueEx for your registry querying needs. I'm sure you could make a nifty little operator overload for this, with a RegistryKey class and a buffer as the params of your overloaded >>. Even so, it wouldn't work how you described it, >> is a purely istream thing until you extend it.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    can't figure out how to use regqueryvalue.... heck I barley understand what this is doin so far LOL, all Is what i mention in the code below.

    here is what I have, all except for a few changes was copyed from here:
    http://msdn.microsoft.com/library/de...mputername.asp

    ok here is the code:
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <apstring.h>
    #include <tchar.h>
    #include <fstream.h>
    
    ifstream fin;
    ofstream fout;
    
    
    
    
    #define MAX_KEY_LENGTH 255
    #define MAX_VALUE_NAME 16383
     
    apstring QueryKey(HKEY hKey) 
    { 
    	fout.open("value.txt"); <=== I open this to store the results	int K = 1;
    
        TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
        DWORD    cbName;                   // size of name string 
        TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
        DWORD    cchClassName = MAX_PATH;  // size of class string 
        DWORD    cSubKeys=0;               // number of subkeys 
        DWORD    cbMaxSubKey;              // longest subkey size 
        DWORD    cchMaxClass;              // longest class string 
        DWORD    cValues;              // number of values for key 
        DWORD    cchMaxValue;          // longest value name 
        DWORD    cbMaxValueData;       // longest value data 
        DWORD    cbSecurityDescriptor; // size of security descriptor 
        FILETIME ftLastWriteTime;      // last write time 
     
        DWORD i, retCode; 
     
        TCHAR  achValue[MAX_VALUE_NAME]; 
        DWORD cchValue = MAX_VALUE_NAME; 
     
        // Get the class name and the value count. 
        retCode = RegQueryInfoKey(
            hKey,                    // key handle 
            achClass,                // buffer for class name 
            &cchClassName,           // size of class string 
            NULL,                    // reserved 
            &cSubKeys,               // number of subkeys 
            &cbMaxSubKey,            // longest subkey size 
            &cchMaxClass,            // longest class string 
            &cValues,                // number of values for this key 
            &cchMaxValue,            // longest value name 
            &cbMaxValueData,         // longest value data 
            &cbSecurityDescriptor,   // security descriptor 
            &ftLastWriteTime);       // last write time 
     
        // Enumerate the subkeys, until RegEnumKeyEx fails.
        
        if (cSubKeys)
        {
            printf( "\nNumber of subkeys: %d\n", cSubKeys);
    
            for (i=0; i<cSubKeys; i++) 
            { 
                cbName = MAX_KEY_LENGTH;
                retCode = RegEnumKeyEx(hKey, i,
                         achKey, 
                         &cbName, 
                         NULL, 
                         NULL, 
                         NULL, 
                         &ftLastWriteTime); 
                if (retCode == ERROR_SUCCESS) 
                {
                    _tprintf(TEXT("(%d) %s\n"), i+1, achKey);
    	fout << K << achKey<<  endl;
    	return (achKey);  <=== im retuning achkey because it contains the subkey name that I need to open to retrieve the value
    	K++;
                }
            }
        } 
     
        // Enumerate the key values. 
    
        if (cValues)      <===== problem because the uninstall key has no values.. its the subkeys that have the values I need so after the value of the subkey is returned to the main function it will be put into Skey and this whole thing ill be repeated for the subkey.after I can get it to do that i will figure out how to keep it from just getting the value from the same subkey over and over.
        {
    			
            printf( "\nNumber of values: %d\n", cValues);
    
            for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) 
            { 
                cchValue = MAX_VALUE_NAME; 
                achValue[0] = '\0'; 
                retCode = RegEnumValue(hKey, i, 
                    achValue, 
                    &cchValue, 
                    NULL, 
                    NULL,
                    NULL,
                    NULL);
     
                if (retCode == ERROR_SUCCESS ) 
                { 
                    _tprintf(TEXT("(%d) %s\n"), i+1, achValue); 
    			} 
            }
        }
    }
    
    void _tmain(void)
    {
    
    	apstring Skey = "";
    	HKEY hTestKey;
    
    	if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    		TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" Skey),
    		0,
    		KEY_READ,
    		&hTestKey) == ERROR_SUCCESS)
    	{			
    	Skey = QueryKey(hTestKey);
    	}
    }
    Ok, I know its not done at the bottom yet.. i need to get it to accept Skey first.. then i'll worry about looping it.. but I noted in red where the problem is

    hope this at least explains what im tryin to do.. searching this spot in the registry is the only way I can think of of obtaining a list of all installed software on the machine.
    Last edited by Sephiroth222; 08-28-2005 at 09:54 AM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Well, from the .h's up top, in particular the stdio.h, is this C or C++? :P

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    In your example, I don't know what an apstring is, or why we'd be using it, but you were returning an HKEY anyways so your QueryKey should have been defined to return one.

    >> Well, from the .h's up top, in particular the stdio.h, is this C or C++? :P

    Meh, if you're using the Win32 API and aren't going to write all your own wrappers, a lot of it will look like regular C anyways. Anyways, to get all the installed programs is going to be somewhat awkward but it's a cool idea. What you might do is, like in your example, is use RegQueryInfoKey in order to get the number of subkeys (cSubKeys) in the already open SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal l key. Then you will loop through each subkey, and, using the key you obtain from the enumeration, you will open it and RegQueryValue[Ex] or RegGetValue the DisplayName or QuietDisplayName values, and then print them out!

    Want to give that a try and report back? Be sure to just look at the MSDN documentation on the functions you need

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Well fstream is only found in C++, but he's using outdated C++. FYI, the new standard dictates that the following style be used:

    - Header files from the C library should be preceded by a 'c'
    - Standard files do not need the '.h'
    - Namespaces must be used

    like so

    Code:
    #include <cstdio> // A standard C header file
    #include <windows.h> // A non-standard file
    #include <apstring.h> // Same as above, I assume
    #include <tchar.h> // Same as above, I assume
    #include <fstream> // A standard C++ header file
    
    using namespace std;
    That 'using' statement is the simplest way to just say that you're using the standard namespace. Nothing else in your code needs to change. There are other styles, which might make it easier if you're working with other namespaces, but since you're just using the standard namespace, I recommend you stick with this for now.

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    this is what i tried and it worked,

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    	HKEY hk;
    	if ( RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\ILV", &hk) == ERROR_SUCCESS)
    	{
    		RegSetValueEx(hk, "hello", 0, REG_EXPAND_SZ, (LPBYTE)"world", 6);
    	}
    	cin.get();
    	return 0;
    }

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> ...obtaining a list of all installed software on the machine. (Sephiroth)
    >> this is what i tried and it worked [code]... (ILoveVectors)

    Was there any relevence with that ILoveVectors?

Popular pages Recent additions subscribe to a feed

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