i know return 0 means making eax = 0 before return. i see a lot of moduls have return 0 at the end. what is it for ?
currently i don't have any of that in my program but it seems to be working without problem.
The specification for many Windows messages when used is to return 0. You can do it manually with xor eax, eax, mov eax, 0 or use the macro that picks out if its returning zero or not.
sorry, what i meant is not "how to use return 0" . but "why should i use return 0" :lol
can i cancel the "xor eax,eax" before ret ? - i mean, in the modules written by myself.
i certainly will not modify the modules written by other people.
supercoollee the purpose is a easy way to signify if the function worked correctly, for example 1 = everythings fine, 0 = it failed. It can be any number just 1 and 0 are common.
which means, if i don't need to check whether the function fails, i don't need to rewrite eax , right?
that's good for me.
No, actually read what has been said, some Windows mesages REQUIRE that 0 is returned in EAX. Some functions that you write can return anything you like in EAX including ZERO. Learn how a system works and you will understand WHY you can return 0.
Most common use is in subclassing procedures; Windows wants to know, for example, if it should erase a button's background, or if you have done it already yourself.
WM_ERASEBKGND Message (http://msdn.microsoft.com/en-us/library/ms648055%28VS.85%29.aspx)
QuoteThe WM_ERASEBKGND message is sent when the window background must be erased (for example, when a window is resized). The message is sent to prepare an invalidated portion of a window for painting.
...
Return Value
An application should return nonzero if it erases the background; otherwise, it should return zero.
after learning how the API functions work, i tend to write routines much the same way
nearly all API functions return a value in EAX
for some, a zero return value indicates failure - a non-zero value is the result of the operation
one example of this might be a function that returns a handle in EAX - if the call fails, EAX will be zero, otherwise it is the handle
for some other API calls, a zero value indicates success
in these cases, a non-zero value may indicate the error code - or you may have to call GetLastError to get the error code
i try to emulate these behaviors when i write my own routines
notice, in ASM, you can also return values in ECX and EDX - i might use ECX for a string length and EDX for the pointer, for example
not sure how those registers are used in different compilers, though
http://users.aber.ac.uk/auj/voidmain.shtml
the system uses the return value after your program exits. different return values signify different things. on windows, you generally return 0 for success and anything else for non-success.
another relevant link :
http://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success
OK, i see :lol
Quote from: Slugsnack on May 10, 2010, 02:19:20 PM
on windows, you generally return 0 for success and anything else for non-success.
Careful with that statement. For most API calls, it is actually the other way round.
Then, there are some silly outliers, such as the FileOpen/CreateFile/FindFirstFile family that uses HFILE_ERROR and INVALID_HANDLE_VALUE, both of which are -1. Which implies that a return value of 0 would be a valid handle ::)
GetKeyState is also a nice example: A key is pressed
.if sword ptr ax<0, i.e. according to the documentation, eax might be positive but ax must be negative.
The best advice is: Read the documentation individually. Don't rely on your intuition.
For the procedures I write, I almost always return zero for success and non-zero for an error.
You should set eax to zero for success at the end of the program too, for ERRORLEVEL and it's just good practice to do so.
Quote from: Greg Lyon on May 10, 2010, 04:23:29 PM
For the procedures I write, I almost always return zero for success and non-zero for an error.
It could be declared a matter of taste. However, in many cases you want to return something useful in eax, such as a handle or a pointer. That is easier done if you declare eax!=0 a success.
just like the API functions - it is neither "set in stone" nor "a matter of taste"
it simply depends on the requirement of the function
jj,
Yes, I said "almost always". :lol
Returning zero for success is a C convention, but it is not "set in stone" either.
You acxtually need to understand what a return value is from a function and why it is that way. A function that returns OFFSET 0 in a file is not returning an error but a file pointer OFFSET. Its mainly boolean returns that are used for errors but that can be risky as well, the best way is to test for the error return value and find out WHY it failed.
I tend to use 0 for failure or error in a function unless zero is a valid return value for the function. There is no blanket usage that is always right and the need to actually use the function reference material will always be with us.
invoke Function,args etc ....
test eax, eax
jz failure
I believe the reason 0 is for success is so you can specify which error happened if it is non-zero. But when you first start programing Win32 returning 0 for WM_NCCREATE may cause you to debug your application for a few hours.... It requires 1 for success... MSDN will be your friend before you learn all of them.
The reason hutch said above is also what i have noticed too... If it is suppose to return a handle or pointer, it will generally be NULL for failure. and you will need to call GetLastError