Cute
Cute

Reputation: 14011

How to open the registry and get the specific value in c++

Ineed to open this key" HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\"

and get the "VerSpecificRootDir" value using c++ ....How can i do this

I have no knowledge abt it can any one help me in this regard..

After Getting All The Support I did it as

 unsigned long type=REG_SZ, size=1024;
 char res[1024]="";
 HKEY hk;

long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\Microsoft\\Microsoft SQL Server\\100"),
                  0,KEY_QUERY_VALUE, &hk );
if ( n == ERROR_SUCCESS ) 
{
    printf("OK \n");
    RegQueryValueEx(hk,L"VerSpecificRootDir",NULL,&type,(LPBYTE)&res[0],&size);
    RegCloseKey(hk);

}

But in this i am not getting the value of "VerSpecificDirectory" what i have missed let me know?? what is wrong in this code....

Upvotes: 1

Views: 4468

Answers (5)

Jordan Miner
Jordan Miner

Reputation: 2054

You can use the Windows function SHRegGetValue like this:

TCHAR buffer[260];
DWORD bufferSize = sizeof(buffer);
SHRegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Microsoft SQL Server\\100", "VerSpecificRootDir", SRRF_RT_REG_SZ, NULL, buffer, &bufferSize);

After calling the function, buffer will contain a null-terminated string of the directory. Might want to check the return value for errors too.

Upvotes: 3

Indy9000
Indy9000

Reputation: 8851

Registry API was available (for ~15 years) from Windows 95 onwards. It's all well documented on MSDN ; and if you care to Google so many examples.

Upvotes: 0

Ivan Zlatanov
Ivan Zlatanov

Reputation: 5226

You can sure use ATL::CRegKey. It has all the functionality you need.

http://msdn.microsoft.com/en-us/library/xka57xy4(VS.80).aspx

Upvotes: 1

Zed
Zed

Reputation: 57648

    #include <windows.h>

    HKEY hKey;
    int buffersize = 1024;
    char *buffer = new char[buffersize];

    RegOpenKeyEx (HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Microsoft SQL Server\\100",NULL,KEY_READ,&hKey);
    RegQueryValueEx(hKey,"VerSpecificRootDir",NULL,NULL,(LPBYTE) buffer, buffersize);

    std::cout << buffer;

    RegCloseKey (hKey);

Upvotes: 2

3DH
3DH

Reputation: 1471

I currently only know how to do that by using the Qt framework:

QSettings settings("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\");
QString dir = settings.value("VerSpecificRootDir");

Qt is free and allows you to use simple and very good documented c++ APIs instead of the mixed and up Windows APIs. Sorry - that sounds for advertising... but I formerly struggled with the very bad designed Windows API and than found Qt, which allows me to develop faster and (a cool benefit) for multiple platforms without having to adapt my code.

Regards, Chris

Upvotes: 1

Related Questions