Archive: GetLastError always returns 126


GetLastError always returns 126
Hello,

Consider following script

FileWrite $0 "abc"
IfErrors +1 +2
Call CheckError
Push "Stop"

Function CheckError

System::Call "Kernel32::GetLastError() i() .r1"
MessageBox MB_ICONINFORMATION|MB_OK "Error code: $1 "

FunctionEnd

The error code returned is 126. From MSDN: ERROR_MOD_NOT_FOUND 126 The specified module could not be found. Now I always get this error code as 126, what ever erroneous instruction i use in first line. Like i tried removing windows directory, 126. I gave an invalid source path in CopyFiles, 126. I am expecting different and meaningful error codes but I always get 126. What could possibly going wrong here?

Basically I am trying to write a function to detect and log details of an error using GetLastError() and FormatMessage(). I will call this function on IfErrors. Im using windows XP to develop the installer.

Thanks in anticipation.


You can't get error codes with nsis, you can only tell if something failed. If you really need error codes, you can call the api directly with the system plugin (kernel32::WriteFile or whatever) and use the ?e option so the system plugin will put the win32 error code on top of the stack


I think I got what you are saying.

the call System::Call "Kernel32::GetLastError() i() .r1" will only be valid if i make a call (kernel32::WriteFile or whatever) first, but not valid after the instruction FileWrite $0 "abc", correct?


No. That call will never be valid as the System plug-in affects the last error flag in so many ways before it even calls GetLastError(). However, the System plug-in can get the last error code for you for calls it made by itself. For that, you need to append "?e" to the call.

System::Call kernel32::WriteFile(...)?e
The error code will wait on the stack.

Anders, Chik

Thanks guys! :up:

I guess I am one of those most people who always look and ask for help and point out lackings. I wish I can do some contribution soon :)