Thread: Registry functions, not there?

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Registry functions, not there?

    I got the "new" dev-cpp 4.9.9.1 and when I got to compile this function...

    Code:
    RegGetValue(
    HKEY_LOCAL_MACHINE,
    "HARDWARE\DESCRIPTION\System\CentralProcessor\0",
    "ProcessorNameString",
    NULL,
    NULL,
    var,
    sizeof(var)
    );
    It says that thsi function doesn't exist.

    here are my header files...

    Code:
    #include <iostream.h>
    #include <Windows.h>
    #include <Winreg.h>
    Winreg, I know shouldn't be there, but I had to try it, and it still didn't work.

    I don't ge this!

  2. #2
    Hello,

    I'm not familiar with RegGetValue(). Though, I am familiar with RegOpenKeyEx, RegQueryValueEx, RegCloseKey, and so forth.

    I did a Google search on RegGetValue, and this is what turned up: MSDN. At the bottom it says: Requires Windows Server 2003 SP1. This may answer your question.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok so now... I got anouther problem....

    Code:
    #include <iostream.h>
    #include <Windows.h>
    
    int main()
    {
        BYTE* var;
        
        RegQueryValueEx(
        HKEY_LOCAL_MACHINE,
        "HARDWARE\DESCRIPTION\System\CentralProcessor\0\ProcessorNameString",
        NULL,
        NULL,
        var,
        sizeof(var)
        );
        
        cout<<var;
    }
    it says invalad converstion from unsigned in to DWORD.

    I must have forgoten alot about c++, but what is wrong?

  4. #4
    Hello,

    Simply, parameter 5 is the address to the data of your buffer and parameter 6 is the address of data buffer size.

    I don't make a habit of posting code, though I think it may be beneficial. I apologize for using printf() and other C-style functions in the C++ board. So, here is an example I wrote on how to query a registry key. It may help gear you in the right direction:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int checkKey(HKEY tree, const char *folder, char *key) {
    	long lRet;
    	HKEY hKey;
    	char temp[150];
    	DWORD dwBufLen;
    
    	// Open location
    	lRet = RegOpenKeyEx( tree, folder, 0, KEY_QUERY_VALUE, &hKey );
    	if (lRet != ERROR_SUCCESS)
    		return 0;
    
    	// Get key
    	dwBufLen = sizeof(temp);
    	lRet = RegQueryValueEx( hKey, key, NULL, NULL, (BYTE*)&temp, &dwBufLen );
    	if (lRet != ERROR_SUCCESS)
    		return 0;
    
    	printf("Key value: %s\n", temp);
    
    	// Close key
    	lRet = RegCloseKey( hKey );
    	if (lRet != ERROR_SUCCESS)
    		return 0;
    
    	// Got this far, then key exists
    	return 1;
    }
    
    int main() {
    	printf("Checking for key: HKEY_CURRENT_USER -> Control Panel -> Desktop -> Wallpaper\n");
    
    	if (checkKey(HKEY_CURRENT_USER, "Control Panel\\Desktop", "Wallpaper"))
    		printf("Key Exists.\n");
    	else
    		printf("Key Does Not Exist.\n");
    
    	return 0;
    }
    RegQueryValueEx()
    The RegQueryValueEx function retrieves the type and data for a specified value name associated with an open registry key.
    Code:
    LONG RegQueryValueEx(
        HKEY hKey,		// handle of key to query
    
        LPTSTR lpValueName,	// address of name of value to query
    
        LPDWORD lpReserved,	// reserved
    
        LPDWORD lpType,	// address of buffer for value type
    
        LPBYTE lpData,	// address of data buffer
    
        LPDWORD lpcbData	// address of data buffer size
    );
    Or learn more about it here.

    There are other functions, though simply doing an MSDN/Google search may pull up further documentation on such functions within the windows header file.

    If you have any further questions please feel free to ask.


    - Stack Overflow
    Last edited by Stack Overflow; 12-29-2004 at 01:30 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As a shortcut, you can use SHGetValue. Whether you use SHGetValue or RegQueryValueEx, you must make sure that a retrieved string is nul terminated before trying to use it. You should be aware that, unless documented, keys in the registry may change from one platform to another (or even when a hotfix is released).
    Code:
    #include <windows.h>
    #include <shlwapi.h>
    #include <stdio.h>
    #pragma comment(lib, "shlwapi.lib")
    
    int main(void)
    {
        TCHAR szName[500];
        DWORD dwType;
        DWORD cbSize = sizeof(szName) - sizeof(TCHAR); /* Leave room for nul terminator. */
    
        if (SHGetValue(HKEY_LOCAL_MACHINE,
                       TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
                       TEXT("ProcessorNameString"),
                       &dwType,
                       szName,
                       &cbSize) == ERROR_SUCCESS)
        {
            /* IMPORTANT: Make sure string is nul terminated. */
            szName[cbSize / sizeof(TCHAR)] = TEXT('\0');
    
            printf("Name is %s.\n", szName);
        }
        else
        {
            printf("Failed to retrieve value.\n");
        }
    
        getchar();
        return 0;
    }
    Last edited by anonytmouse; 12-29-2004 at 01:49 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> BYTE* var;

    that doesn't point to anything.

    >> sizeof(var)

    the parameter should be a pointer to the size of the buffer - on return it will contain the number of bytes copied.

    >> I must have forgoten alot about c++, but what is wrong?

    ever heard of documentation? it's a nice thing to have when you can't remember function parameters. here's borland's WinAPI help file: download.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM