GetLastError always returns zero

This is a discussion on GetLastError always returns zero within the Windows Programming forums, part of the Platform Specific Boards category; Hi All, I tried to read a key and its value from windows registry. I succeeded in it. But i ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    GetLastError always returns zero

    Hi All,
    I tried to read a key and its value from windows registry. I succeeded in it. But i want to know the error code returned by those functions and hence made the function to fail (by asking program to get value for a non-existing key). GetLastError i used. But it always returns zero as the error code.
    Can anyone help me in this regard.
    Thanks in advance.
    Saravanan.T.S.
    Beginner.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    What version of Windows are you running? The 16bit cored systems, (95,98,ME), do not fully implement GetLastError() and frequently return zero even if there is an error.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Hi all,
    I am using win2k os. Code is very simple. Here is this:
    Code:
    #include<windows.h>
    #include<stdio.h>
    void printerror(char*);
    int main()
    {
    	HKEY hkey;
    	//LPCTSTR lpsubkey;	
    	int err,sizekeyvalue=30;
    	char keyvalue[30];	
    	err=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Sarav",0,KEY_READ,&hkey);
    	if(err==ERROR_SUCCESS)
    	{
    		printf("\nRegistry value read successfully\n");
    	}
    	else
    	{
    		printerror("RegOpenKeyEx");		
    	}
    	err=RegQueryValueEx(hkey,"Protocol",0,0,(LPBYTE)keyvalue,&sizekeyvalue);
    	if(err==ERROR_SUCCESS)
    	{
    		printf("\nThe string is %s\n",keyvalue);
    	}
    	else
    	{
    		printerror("RegQueryValue");
    	}
    	RegCloseKey(hkey);
    }
    void printerror(char *msg)
    {
    	DWORD dwErrno;
    	char errmsg[30];
    	dwErrno=GetLastError();
    	sprintf(errmsg,"%s Failed:Error No is %u",msg,dwErrno);
    	printf("\n%s\n",errmsg);
    }
    Saravanan.T.S.
    Beginner.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Neither of the functions you are using use the GetLastError() mechanism, rather, they return the winerror.h value directly. From MSDN...

    >>>
    Return Values
    If the function succeeds, the return value is ERROR_SUCCESS.

    If the function fails, the return value is a nonzero error code defined in WINERROR.H. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
    <<<

    ... routines that require you to call GetLastError() mention it in this section of the documentation.

    As an aside, it is always a good idea to call GetLastError() immeadiately after a failing call. All kinds of things can reset this value.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Thanks adrianxw,
    I made variable=RegOpenKeyEx() to get fail and the error value is present in "variable" itself. Now i am able to get error value correclty.But why GetLastError() fails to give correct error value?
    Saravanan.T.S.
    Beginner.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> I made variable=RegOpenKeyEx() to get fail and the error value is present in "variable" itself.

    Err, you had that already, (err = )...

    >>> But why GetLastError() fails to give correct error value?

    ... as I said...

    >>> Neither of the functions you are using use the GetLastError() mechanism
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Yes salem,
    I found that(buffer overflow) and I corrected it already..Thanks.
    Saravanan.T.S.
    Beginner.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Hi adrianxw,
    I just mentioned "variable" in generic sense for clarity.. I used only "err".
    Saravanan.T.S.
    Beginner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21