Problem in opening the windows registery
-
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
-
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
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] -
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
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]
-
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
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
-
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]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
-
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
Hi Cedric,Jonathan and D.Mani Thanks a lot for your quick replies. I got my problem completely solved now.
With Regards 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