Help needed with a function problem I really cant solve. Why is it that RegOpenKey() returns error success even though it fails. First it fails to return a handle with a valid Registry key/address filling the key paramater with nothing 0 but returns 0 aswell indicating an error_success right? because when I format the error it says "The operation succeeded". Whats really odd is that I tried the same thing and no matter what the functions always returns error_success even when there is an invalid KEY or details supplied to it. If I am not mistaken the getLastError discards the error it pulls out, I dont know perhaps it does. But anyway I always keep getting 0 even the function has clearly failed(from teh function and the Error Message queue. Even though I call RegReadkey afterwards with an invalid handle it succeeds and does not supply the size nor read the value into a buffer. I am confused does anyone know why all these functions are failing and succeeding(according to Format Message). infact in the if statement for regopenkey, it would return error success yet was != to it in the condition I finally had to use a variable in the condition and have it compared to not equal to 0 to get it to work. Is this a problem with Registry virtualization ? I am opening HKEY_LOCAL_MACHINE and a key in the SYSTEM part of it. the code come compiles fine but this happens does this just happen when you compile a program from Visual Studio, is it due to come CLR related thing?

Note I tried to call RegOpenKeyEx same problem any ideas of what might be going on?

 DWORD Err = 42;
        long int iErri=9;
        char * msg = 0; if(iErri != 161 || 2) {
            cout<<"An error occurred while attempting to open your path for writing this may be due"<<endl<<" to your system's settings or some other problem pertaining to the values please check your system settings and try again. Remember to run this program as an adminster for full access to the values we need to change(inserting a new path requires adminster privileges)"<<endl;
                Err = GetLastError(); // no matter how many times THAT i get it is always error_succcess
                Err = GetLastError();
                Err = GetLastError();
                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, Err, (LPTSTR) &msg, 0, NULL);
                cout<<"System: "<<Err<<endl<<endl;
                printf("%S\n System<Message may contain irregular messeges or be shortened", msg);
                    RegCloseKey(key);
                    return 0;

                 DWORD Err = 42;
        long int iErri=9;
        char * msg = 0; 
        va_list * Dts =  0;
        HKEY key;
        Err = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\hggjk"), &key);
        Err = GetLastError();
        cout<<key<<endl; //key is always zero and i set the if to always succeed for debugging purposes
        if(iErri != 161 || 2) {
        cout<<"An error occured while opening the resources needed to complete the specified task unfortunately the program will need to resolve to recover"<<endl;
            Err = GetLastError(); // no matter how many times THAT i get it is always error_succcess
            Err = GetLastError();
            Err = GetLastError();
             FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, Err, (LPTSTR) &msg, 0, NULL);
            cout<<"System: "<<Err<<endl<<endl;
            printf("%S\n System<Message may contain irregular messeges or be shortened", msg);
                RegCloseKey(key);
                return 0;


        }  DWORD Err = 42;
        long int iErri=9;
        char * msg = 0; 
        va_list * Dts =  0;
        HKEY key;
        Err = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\hggjk"), &key);
        Err = GetLastError();
        cout<<key<<endl; //key is always zero and i set the if to always succeed for debugging purposes
        if(iErri != 161 || 2) {
        cout<<"An error occured while opening the resources needed to complete the specified task unfortunately the program will need to resolve to recover"<<endl;
            Err = GetLastError(); // no matter how many times THAT i get it is always error_succcess
            Err = GetLastError();
            Err = GetLastError();
             FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, Err, (LPTSTR) &msg, 0, NULL);
            cout<<"System: "<<Err<<endl<<endl;
            printf("%S\n System<Message may contain irregular messeges or be shortened", msg);
                RegCloseKey(key);
                return 0;


        } DWORD Err = 42;
        long int iErri=9;
        char * msg = 0; 
        va_list * Dts =  0;
        HKEY key;
        Err = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\hggjk"), &key);
        Err = GetLastError();
        cout<<key<<endl; //key is always zero and i set the if to always succeed for debugging purposes
        if(iErri != 161 || 2) {
        cout<<"An error occured while opening the resources needed to complete the specified task unfortunately the program will need to resolve to recover"<<endl;
            Err = GetLastError(); // no matter how many times THAT i get it is always error_succcess
            Err = GetLastError();
            Err = GetLastError();
             FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, Err, (LPTSTR) &msg, 0, NULL);
            cout<<"System: "<<Err<<endl<<endl;
            printf("%S\n System<Message may contain irregular messeges or be shortened", msg);
                RegCloseKey(key);
                return 0;

        }

Calling registry functions does not cause GetLastError() to return any error codes. They are rather returned by the registry function you called.

Example:

DWORD Err;
HKEY key;
char * msg;
Err = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\hggjk"), &key);
if( Err!=ERROR_SUCCESS )
{
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, Err, (LPTSTR) &msg, 0, NULL);
    cout<<"System: "<<Err<<endl<<endl;
    printf("%S\n System<Message may contain irregular messeges or be shortened", msg);
    LocalFree(msg); // Always call this API to free the memory allocated by the call to FormatMessage
}
else
    RegCloseKey(key);

If RegOpenKey fails, the error code is returned in the Err variable.

Additionally, calling GetLastError() for the first time clears the system's internal error flag thus succeeding calls to GetLastError() will always return 0.

oh sorry this is a really late reply. I guess the error is non zero then but it still isnt working I was misinformed I guess I saw example code on the MSDN library where someone used it like that oh wait maybe that was S.O. I dont know I should change the title or post again before I continue. I think I kept gettting the error 87 or 131 when I used it without the getlasterror function. Thats right I knew it was odd that it returned an Error code by its return type specifier and yet still would post it to the system, no functions I know of do that I should have took note of that. In any condition its stil not succeeding but there should be a different post for that. I appreciate the response unfortunately I could not reply because I was in a hotel where I could not get the WIFI to work for a bit of time.

This article has been dead for over six months. Start a new discussion instead.