f



Error code when there is no error??

In my source I have:

     wndclass.hIcon = LoadIcon (hInstance, "test") ;

test.ico is a valid icon. file. Everythig works ok.
But when I test for an error like this:

     ShowLastError();   // no problems here
     wndclass.hIcon = LoadIcon (hInstance, "test") ;
     ShowLastError();   // I get "The system cannot find the file specidied"

wndclass.hIcon get a valid handle.





Here is my ShowLastError program:

VOID ShowLastError(void)
{
    LPVOID lpMsgBuf;
    DWORD error_code;
    int status;
    char sa1[1024];
    error_code = GetLastError ();
    if (error_code!=0)
       {
       status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
       if (!status)
          {
          wsprintf(sa1,"%s: Cannot display message for error %ld, status
%ld","Program", error_code, GetLastError());
          MessageBox(NULL, sa1,"", MB_OK | MB_ICONERROR);
          goto hytrt;   
          }
          MessageBox(NULL, lpMsgBuf,"", MB_OK | MB_ICONERROR);
       LocalFree((HLOCAL)lpMsgBuf);
       hytrt:   
       SetLastError(0);
       }
return;    
}
0
John
8/13/2009 8:20:38 AM
comp.os.programmer.win32 14523 articles. 0 followers. Post Follow

8 Replies
947 Views

Similar Articles

[PageSpeed] 54

John Smith wrote:
> In my source I have:
> 
>      wndclass.hIcon = LoadIcon (hInstance, "test") ;
> 
> test.ico is a valid icon. file. Everythig works ok.
> But when I test for an error like this:
> 
>      ShowLastError();   // no problems here
>      wndclass.hIcon = LoadIcon (hInstance, "test") ;
>      ShowLastError();   // I get "The system cannot find the file
>      specidied"
> 
> wndclass.hIcon get a valid handle.

You generally call GetLastError() only if something failed, otherwise its
value is meaningless. How exactly to determine errors depends on the called
function, see its documentation (http://msdn.microsoft.com).

Uli


-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

0
Ulrich
8/13/2009 8:55:21 AM
On 13/08/2009 09:20, John Smith wrote:
> In my source I have:
>
>       wndclass.hIcon = LoadIcon (hInstance, "test") ;
>
> test.ico is a valid icon. file. Everythig works ok.
> But when I test for an error like this:

Are you trying to load the "test.ico" file or the "test" named resource?

-- 
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems
0
Dee
8/13/2009 9:26:41 AM
Dee Earley <dee.earley@icode.co.uk> wrote:

>On 13/08/2009 09:20, John Smith wrote:
>> In my source I have:
>>
>>       wndclass.hIcon = LoadIcon (hInstance, "test") ;
>>
>> test.ico is a valid icon. file. Everythig works ok.
>> But when I test for an error like this:
>
>Are you trying to load the "test.ico" file or the "test" named resource?

in my RC file there is a line:

test ICON "testabc.ico"
0
John
8/13/2009 10:04:25 AM
Ulrich Eckhardt <eckhardt@satorlaser.com> wrote:

>John Smith wrote:
>> In my source I have:
>> 
>>      wndclass.hIcon = LoadIcon (hInstance, "test") ;
>> 
>> test.ico is a valid icon. file. Everythig works ok.
>> But when I test for an error like this:
>> 
>>      ShowLastError();   // no problems here
>>      wndclass.hIcon = LoadIcon (hInstance, "test") ;
>>      ShowLastError();   // I get "The system cannot find the file
>>      specidied"
>> 
>> wndclass.hIcon get a valid handle.
>
>You generally call GetLastError() only if something failed, otherwise its
>value is meaningless. How exactly to determine errors depends on the called
>function, see its documentation (http://msdn.microsoft.com).
>
>Uli


Please note that the first ShowLastError() set's the error number to to zero
using SetLastError(0). So thet loadicon line generates an error. Why, I dunno.

ShowLastError();   //error count it set to zero
wndclass.hIcon = LoadIcon (hInstance, "test") ;
ShowLastError();   
0
John
8/13/2009 10:08:33 AM
John Smith wrote:
> Please note that the first ShowLastError() set's the error number to to
> zero using SetLastError(0). So thet loadicon line generates an error. Why,
> I dunno.
> 
> ShowLastError();   //error count it set to zero
> wndclass.hIcon = LoadIcon (hInstance, "test") ;
> ShowLastError();

John, really, there is no error. Only if LoadIcon() returns NULL is the
value of GetLastError() meaningful, as is documented. If it doesn't fail,
the value of GetLastError() is meaningless.

  icon = LoadIcon(...);
  if(icon==NULL) {
     // save error value as first action
     int e = GetLastError();
     ... // handle error (log and return or throw)
  }

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

0
Ulrich
8/13/2009 10:44:24 AM
John Smith wrote:

> Ulrich Eckhardt <eckhardt@satorlaser.com> wrote:
> 
> > John Smith wrote:
> >> In my source I have:
> >> 
> >>      wndclass.hIcon = LoadIcon (hInstance, "test") ;
> >> 
> >> test.ico is a valid icon. file. Everythig works ok.
> >> But when I test for an error like this:
> >> 
> >>      ShowLastError();   // no problems here
> >>      wndclass.hIcon = LoadIcon (hInstance, "test") ;
> >>      ShowLastError();   // I get "The system cannot find the file
> >>      specidied"
> >> 
> >> wndclass.hIcon get a valid handle.
> > 
> > You generally call GetLastError() only if something failed,
> > otherwise its value is meaningless. How exactly to determine errors
> > depends on the called function, see its documentation
> > (http://msdn.microsoft.com).
> > 
> > Uli
> 
> 
> Please note that the first ShowLastError() set's the error number to
> to zero using SetLastError(0). So thet loadicon line generates an
> error. Why, I dunno.
> 
> ShowLastError();   //error count it set to zero
> wndclass.hIcon = LoadIcon (hInstance, "test") ;
> ShowLastError();   

Hi John,

Not 100% sure of this, but GetLastError (which I assume ShowLastError
is based on) just reports the last set error.  That can be _the_ error
you're interested in, or any other error that appeared earlier.  That
you reset the error before you call LoadIcon doesn't mean LoadIcon
itself can't generate an error internally in it's own code; if it then
handles that error succesfully and returns a valid icon handle, nothing
bad has happened but GetLastError still reports the error that occured
in LoadIcon (I assume LoadIcon doesn't reset the error when it's
successful?).

So, to re-iterate Ulrich Eckhardt, only call GetLastError /
ShowLastError when an error occured, and forget about it otherwise.
What happens here probably involves LoadIcon's implementation trying to
load the resource from file or such thing, failing at that, and then
loads it correctly from resource?

-- 
Kind regards,
Carl Colijn

TwoLogs - IT Services and Product Development; a natural choice!
http://www.twologs.com
TimeTraces: the powerful and versatile time registration system!
http://www.twologs.com/TimeTraces
0
Carl
8/13/2009 10:59:24 AM
Ulrich Eckhardt <eckhardt@satorlaser.com> wrote:

>John Smith wrote:
>> Please note that the first ShowLastError() set's the error number to to
>> zero using SetLastError(0). So thet loadicon line generates an error. Why,
>> I dunno.
>> 
>> ShowLastError();   //error count it set to zero
>> wndclass.hIcon = LoadIcon (hInstance, "test") ;
>> ShowLastError();
>
>John, really, there is no error. Only if LoadIcon() returns NULL is the
>value of GetLastError() meaningful, as is documented. If it doesn't fail,
>the value of GetLastError() is meaningless.
>
>  icon = LoadIcon(...);
>  if(icon==NULL) {
>     // save error value as first action
>     int e = GetLastError();
>     ... // handle error (log and return or throw)
>  }
>
>Uli



For some reason LoadIcon sets the error value returned by subsequent
GetLastError(). The value points to an error allthough no error has occurred.

This does not happen in most other functions. I do get a similar error in a
CreateIC() function although the function is successfull and returns a valid
value which works well.

My point in all this is simple bug tracking: throw GetLastError()'s to try to
smoke out bugs.

From my experiece GetLastError() retains the last error from a previous function
call that did not succeed. Subsequent other function calls that do not result in
error do not overwrite said previous error. Only a new error situation renews the
GetLastError() value.

0
John
8/13/2009 12:14:54 PM
John Smith wrote: 
> For some reason LoadIcon sets the error value returned by subsequent
> GetLastError(). The value points to an error allthough no error has
> occurred.

You don't know what LoadIcon() might have tried internally before succeeding
overall, so your claim that no error has occurred doesn't hold. LoadIcon()
as a whole didn't fail, as you can see from the handle it returned.

> From my experiece GetLastError() retains the last error from a previous
> function call that did not succeed. Subsequent other function calls that
> do not result in error do not overwrite said previous error. Only a new
> error situation renews the GetLastError() value.

Sorry but no. As you see yourself, using GetLastError() can /not/ be used to
determine whether an error needs to be handled, not even if you used
SetLastError(0) before the call. Also, that behaviour is not in conflict
with the documentation, which says that GetLastError() can be used to
retrieve further information /in case of failure/, which is signalled by
the returnvalue. Otherwise, no guarantees about the value of GetLastError()
are made.

Don't get me wrong, I understand your wish for GetLastError() to
consistently signal whether a function succeeded or not (though I'd rather
have an explicit returnvalue tell me that), but reality differs from both
our desires.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

0
Ulrich
8/13/2009 12:46:54 PM
Reply: