Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem in opening the windows registery

Problem in opening the windows registery

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neeraj Sinha
    wrote on last edited by
    #1

    Hi I have to retrieve the registery value through RegQueryValueEx().Before doing this I am using RegOpenKeyEx() to open the registery.But the return value of RegOpenKeyEx() I am getting is 998 i.e the function is not able to successfully open the windows registery.Although,I have tried to map the error code of getlasterror() with the help of FormatMessage(),the return value of getlasterror() is 0 i.e. "operation completed successfully". Here is the code: PHKEY m_htKey; const char* lpSubKey = "SOFTWARE\\MyProduct\\N K software\\1.5"; long* lpcbValue = NULL; DWORD valueType = 0; DWORD dwBufLen = 0; const char* valueName = "NKSDir"; long res = RegOpenKeyEx(HKEY_CURRENT_USER ,lpSubKey,0,KEY_ALL_ACCESS,m_htKey); DWORD dw = GetLastError(); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf,0,NULL); //Display the string. MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); // Free the buffer. LocalFree( lpMsgBuf ); if (res != ERROR_SUCCESS) return TRUE; else { printf("hi"); res = RegQueryValueEx((HKEY)m_htKey,valueName,NULL,&valueType,NULL,&dwBufLen); } Now in the above code, the if condition is getting true and it's returning true.The control is not going into the else condition. Can you please suggest me what could be the reason behind it that RegOpenKeyEx() is returning non zero value?

    With Regards Neeraj Sinha

    C J J H 4 Replies Last reply
    0
    • N Neeraj Sinha

      Hi I have to retrieve the registery value through RegQueryValueEx().Before doing this I am using RegOpenKeyEx() to open the registery.But the return value of RegOpenKeyEx() I am getting is 998 i.e the function is not able to successfully open the windows registery.Although,I have tried to map the error code of getlasterror() with the help of FormatMessage(),the return value of getlasterror() is 0 i.e. "operation completed successfully". Here is the code: PHKEY m_htKey; const char* lpSubKey = "SOFTWARE\\MyProduct\\N K software\\1.5"; long* lpcbValue = NULL; DWORD valueType = 0; DWORD dwBufLen = 0; const char* valueName = "NKSDir"; long res = RegOpenKeyEx(HKEY_CURRENT_USER ,lpSubKey,0,KEY_ALL_ACCESS,m_htKey); DWORD dw = GetLastError(); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf,0,NULL); //Display the string. MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); // Free the buffer. LocalFree( lpMsgBuf ); if (res != ERROR_SUCCESS) return TRUE; else { printf("hi"); res = RegQueryValueEx((HKEY)m_htKey,valueName,NULL,&valueType,NULL,&dwBufLen); } Now in the above code, the if condition is getting true and it's returning true.The control is not going into the else condition. Can you please suggest me what could be the reason behind it that RegOpenKeyEx() is returning non zero value?

      With Regards Neeraj Sinha

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      I don't know the solution to your problem (I never worked with the registry) but shouldn't you pass the return value of RegOpenKeyEx to your FormatMessage function instead of GetLastError ? Even if you get an error in RegOpenKeyEx, GetLastError will still return 0.


      Cédric Moonen Software developer
      Charting control [v1.2]

      N 1 Reply Last reply
      0
      • N Neeraj Sinha

        Hi I have to retrieve the registery value through RegQueryValueEx().Before doing this I am using RegOpenKeyEx() to open the registery.But the return value of RegOpenKeyEx() I am getting is 998 i.e the function is not able to successfully open the windows registery.Although,I have tried to map the error code of getlasterror() with the help of FormatMessage(),the return value of getlasterror() is 0 i.e. "operation completed successfully". Here is the code: PHKEY m_htKey; const char* lpSubKey = "SOFTWARE\\MyProduct\\N K software\\1.5"; long* lpcbValue = NULL; DWORD valueType = 0; DWORD dwBufLen = 0; const char* valueName = "NKSDir"; long res = RegOpenKeyEx(HKEY_CURRENT_USER ,lpSubKey,0,KEY_ALL_ACCESS,m_htKey); DWORD dw = GetLastError(); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf,0,NULL); //Display the string. MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); // Free the buffer. LocalFree( lpMsgBuf ); if (res != ERROR_SUCCESS) return TRUE; else { printf("hi"); res = RegQueryValueEx((HKEY)m_htKey,valueName,NULL,&valueType,NULL,&dwBufLen); } Now in the above code, the if condition is getting true and it's returning true.The control is not going into the else condition. Can you please suggest me what could be the reason behind it that RegOpenKeyEx() is returning non zero value?

        With Regards Neeraj Sinha

        J Offline
        J Offline
        Jonathan Darka
        wrote on last edited by
        #3

        Hi, Cedric's comment is correct, you need to pass the value returned from RegQueryValueEx() to FormatMessage() and then tell us the error you are getting before we can help.


        Jonathan Wilkes Darka [Xanya.net]

        1 Reply Last reply
        0
        • N Neeraj Sinha

          Hi I have to retrieve the registery value through RegQueryValueEx().Before doing this I am using RegOpenKeyEx() to open the registery.But the return value of RegOpenKeyEx() I am getting is 998 i.e the function is not able to successfully open the windows registery.Although,I have tried to map the error code of getlasterror() with the help of FormatMessage(),the return value of getlasterror() is 0 i.e. "operation completed successfully". Here is the code: PHKEY m_htKey; const char* lpSubKey = "SOFTWARE\\MyProduct\\N K software\\1.5"; long* lpcbValue = NULL; DWORD valueType = 0; DWORD dwBufLen = 0; const char* valueName = "NKSDir"; long res = RegOpenKeyEx(HKEY_CURRENT_USER ,lpSubKey,0,KEY_ALL_ACCESS,m_htKey); DWORD dw = GetLastError(); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf,0,NULL); //Display the string. MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); // Free the buffer. LocalFree( lpMsgBuf ); if (res != ERROR_SUCCESS) return TRUE; else { printf("hi"); res = RegQueryValueEx((HKEY)m_htKey,valueName,NULL,&valueType,NULL,&dwBufLen); } Now in the above code, the if condition is getting true and it's returning true.The control is not going into the else condition. Can you please suggest me what could be the reason behind it that RegOpenKeyEx() is returning non zero value?

          With Regards Neeraj Sinha

          J Offline
          J Offline
          Jhony george
          wrote on last edited by
          #4

          LONG regResult = ERROR_SUCCESS; HKEY hKey; regResult=RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\MyProduct\\N K software\\1.5" ,0,KEY_ALL_ACCESS,&hKey); if(regResult == ERROR_SUCCESS) { unsigned long type=REG_SZ, size=1024; char res[1024]=""; char res2[1024]=""; long err=RegQueryValueEx(hKey,"Reference",NULL,&type,(LPBYTE)&res[0],&size); RegCloseKey(hKey); if(err==ERROR_SUCCESS) { //....do necessary code here } else //...do code here } Hope this will help you!!!!!!!!!!1

          N 1 Reply Last reply
          0
          • C Cedric Moonen

            I don't know the solution to your problem (I never worked with the registry) but shouldn't you pass the return value of RegOpenKeyEx to your FormatMessage function instead of GetLastError ? Even if you get an error in RegOpenKeyEx, GetLastError will still return 0.


            Cédric Moonen Software developer
            Charting control [v1.2]

            N Offline
            N Offline
            Neeraj Sinha
            wrote on last edited by
            #5

            Yes I agree.Thanks for your help to remove my confusion.Now I am getting the correct message of error code 998 through FormatMessage, i.e."Invalid access to memory location". Can anybody please solve this problem?What could be the possible reason behind this message?

            With Regards Neeraj Sinha

            1 Reply Last reply
            0
            • J Jhony george

              LONG regResult = ERROR_SUCCESS; HKEY hKey; regResult=RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\MyProduct\\N K software\\1.5" ,0,KEY_ALL_ACCESS,&hKey); if(regResult == ERROR_SUCCESS) { unsigned long type=REG_SZ, size=1024; char res[1024]=""; char res2[1024]=""; long err=RegQueryValueEx(hKey,"Reference",NULL,&type,(LPBYTE)&res[0],&size); RegCloseKey(hKey); if(err==ERROR_SUCCESS) { //....do necessary code here } else //...do code here } Hope this will help you!!!!!!!!!!1

              N Offline
              N Offline
              Neeraj Sinha
              wrote on last edited by
              #6

              Hi Cedric,Jonathan and D.Mani Thanks a lot for your quick replies. I got my problem completely solved now.

              With Regards Neeraj Sinha

              1 Reply Last reply
              0
              • N Neeraj Sinha

                Hi I have to retrieve the registery value through RegQueryValueEx().Before doing this I am using RegOpenKeyEx() to open the registery.But the return value of RegOpenKeyEx() I am getting is 998 i.e the function is not able to successfully open the windows registery.Although,I have tried to map the error code of getlasterror() with the help of FormatMessage(),the return value of getlasterror() is 0 i.e. "operation completed successfully". Here is the code: PHKEY m_htKey; const char* lpSubKey = "SOFTWARE\\MyProduct\\N K software\\1.5"; long* lpcbValue = NULL; DWORD valueType = 0; DWORD dwBufLen = 0; const char* valueName = "NKSDir"; long res = RegOpenKeyEx(HKEY_CURRENT_USER ,lpSubKey,0,KEY_ALL_ACCESS,m_htKey); DWORD dw = GetLastError(); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf,0,NULL); //Display the string. MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); // Free the buffer. LocalFree( lpMsgBuf ); if (res != ERROR_SUCCESS) return TRUE; else { printf("hi"); res = RegQueryValueEx((HKEY)m_htKey,valueName,NULL,&valueType,NULL,&dwBufLen); } Now in the above code, the if condition is getting true and it's returning true.The control is not going into the else condition. Can you please suggest me what could be the reason behind it that RegOpenKeyEx() is returning non zero value?

                With Regards Neeraj Sinha

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                And I want to suggest see this article. A Registry Class [^]


                WhiteSky


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups