Craig
Craig

Reputation: 177

How do I get the exit code via %errorlevel% , its not working anymore with assembly program

I want to get the exit code of my tutorial assembly program (using masm32 and link). It was working fine, I would type echo %errorlevel% and it would display my exit code that I typed in after invoke ExitProcess. Now it doesn't work anymore. I'm using VirtualBox on an OpenSuse 12.1 host and Windows Vista Home Premium as the guest. I've searched for answers but have come up short. Most complaints are about using a batch file, which is not what I'm trying to do. Here is the simple program

hello_world.asm

.586
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

.data
HelloWorld  db  "Hello World!", 0
.code
start:
invoke MessageBox, NULL, addr HelloWorld, addr HelloWorld, MB_OK
invoke ExitProcess, 2

end start

I expect it to return 2, but echo %errorlevel% returns 0. Is there something I'm missing? Thanks, and I apologize this question has been answered to death. I just can't find my answer.

Edit: Actually, I found part of my answer. It only works if I link using /SUBSYSTEM:CONSOLE. Using /SUBSYSTEM:WINDOWS always returns 0. I don't know what to make of this. where is the exit code with a windows program? any info greatly appreciated.

Upvotes: 0

Views: 931

Answers (2)

Harry Johnston
Harry Johnston

Reputation: 36328

Launch the process like this:

start /wait helloworld

That will make the command shell wait until the process has finished, so that it can retrieve the exit code.

(You don't need to do this if you are using a batch file.)

Upvotes: 0

Raymond Chen
Raymond Chen

Reputation: 45172

If your subsystem is Windows, then the command processor returns to the command prompt immediately without waiting for the program to exit. (Try it with notepad for example.) Since time travel has not yet been invented, it cannot tell you what the exit code of the program is, since the program hasn't exited yet.

Upvotes: 5

Related Questions