Page 1 of 2

How to set exit code with errorlevel() ?

Posted: Fri Apr 28, 2017 7:41 pm
by kristalist
Hi All!
Can anybody help me with function errorlevel() ?
I tryed to set errorlevel in HMG programs ( errorlevel(anynumber)) , but after quiting I see error exit code == 0 ...
I need this functionality, but can't understand how to set errorlevel ( to change exit error code) before quiting HMG programs.
I have tested exit code of programs with this script ( you can save in "C:\hmg.3.4.3\SAMPLES\Applications\CONTACTOS\testexit.vbs" and try to set errorlevel(33) in "contactos project" before quiting ( before windows.release) .)

Code: Select all

dim path, WshShell
path = "C:\hmg.3.4.3\SAMPLES\Applications\CONTACTOS\contactos.exe"

Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(path)) Then
   msgBox path & " exists."
Else
   msgBox path & " doesn't exist."
End If


Set WshShell = WScript.CreateObject("WScript.Shell")
Do
If fso.FileExists(path) Then

  			RetVal = WshShell.Run( path, 1, True)
	Else
  			RetVal = 255
	End If
Loop Until True '(RetVal = 0 )
WScript.echo "Errorlevel on return was:", RetVal
MsgBox "How to change errorlevel on HMG-programs exit?" & vbCrLf & "using errorleve(errnum) ( as in pure harbour) do not work on my tests :-("

Re: How to set exit code with errorlevel() ?

Posted: Fri Apr 28, 2017 8:57 pm
by edk
In my opinion the ERRORLEVEL function works fine.
View sample: run test.bat
ErrorLev.7z
(554.78 KiB) Downloaded 247 times
You can also read the ErrorLevel running application from Harbour via WaitRun().
Run ErrorLev.exe
ErrorLev_and_waitrun.7z
(1.24 MiB) Downloaded 246 times

Re: How to set exit code with errorlevel() ?

Posted: Fri Apr 28, 2017 10:15 pm
by kristalist
Thanks edk for answer. Yes, I have not any problems to set errorlevel() in pure consol harbours programm and get this value by VBS script. But , if I trying to add function errorlevel() to HMG GUI project ( for example to hmg.3.4.3\SAMPLES\Applications\CONTACTOS\contactos.exe ), then I can't change returning exit code by this function and used it in my VBS script. When I told HMG programs as opposite HARBOUR programs I mean GUI specific programs with non-harbours linked librires. Sorry, I am not specialist in good definitions.

Re: How to set exit code with errorlevel() ?

Posted: Fri Apr 28, 2017 10:30 pm
by Pablo César
Seems iteresting.
You can also read the ErrorLevel running application from Harbour via WaitRun().
Nice, to know that. ErrorLevel can be read by OS inbatch files in looping.

Re: How to set exit code with errorlevel() ?

Posted: Fri Apr 28, 2017 11:00 pm
by srvet_claudio
Try with:
ExitProcess( nErrorLevel )

Re: How to set exit code with errorlevel() ?

Posted: Mon May 01, 2017 9:57 pm
by kristalist
Thank You Dr. Claudio! It's works !

Re: How to set exit code with errorlevel() ?

Posted: Sat Sep 16, 2017 9:11 am
by BeGeS
Hi.

I want to upload this thread because I am trying to work with ErrorLevel() and I can not do it.

And this function: ExitProcess(nErrorLevel), I don't know what it does.

What I want is the following:

- aaa.BAT calls bbb.EXE

- bbb.EXE returns a value (from 11 to 20, for example) to aaa.BAT

- Depending on that value, aaa.BAT will call ccc.EXE or ddd.EXE

With HARBOUR works perfectly, but with HMG I do not know how to do it.

Thank you.

P.S .: I've already tried WAITRUN() and HB_RUN(), but it's not what I'm looking for.

Re: How to set exit code with errorlevel() ?

Posted: Sat Sep 16, 2017 8:46 pm
by edk
Hi, this sample works like a charm for me ;) :

Code: Select all

#include <hmg.ch>

Function Main

Local nErrorLevel:=0

    DEFINE WINDOW main AT 144 , 426 WIDTH 163 HEIGHT 242 VIRTUAL WIDTH Nil VIRTUAL HEIGHT Nil TITLE "" ICON NIL MAIN CURSOR NIL ON INIT Nil ;
		ON RELEASE (nErrorLevel:=main.RadioGroup_1.value - 1, msgbox( 'nErrorLevel:='+hb_ValToSTR(nErrorLevel), 'On Release') , ExitProcess ( nErrorLevel )) ;
		ON INTERACTIVECLOSE Nil ON MOUSECLICK Nil ON MOUSEDRAG Nil ON MOUSEMOVE Nil ON SIZE Nil ON MAXIMIZE Nil ON MINIMIZE Nil ON PAINT Nil BACKCOLOR Nil NOTIFYICON NIL NOTIFYTOOLTIP NIL ON NOTIFYCLICK Nil ON GOTFOCUS Nil ON LOSTFOCUS Nil ON SCROLLUP Nil ON SCROLLDOWN Nil ON SCROLLLEFT Nil ON SCROLLRIGHT Nil ON HSCROLLBOX Nil ON VSCROLLBOX Nil

    DEFINE RADIOGROUP RadioGroup_1
        ROW    20
        COL    30
        WIDTH  120
        HEIGHT 100
		OPTIONS { 'ErrorLevel 0', 'ErrorLevel 1', 'ErrorLevel 2' , 'ErrorLevel 3'}
        VALUE 1
        FONTNAME "Arial"
        FONTSIZE 9
        TOOLTIP ""
        ONCHANGE Nil
        FONTBOLD .F.
        FONTITALIC .F.
        FONTUNDERLINE .F.
        FONTSTRIKEOUT .F.
        HELPID Nil
        TABSTOP .T.
        VISIBLE .T.
        TRANSPARENT .F.
        SPACING 25
        BACKCOLOR Nil
        FONTCOLOR Nil
        READONLY Nil
        HORIZONTAL .F. 
    END RADIOGROUP

    DEFINE BUTTON Button_1
        ROW    140
        COL    20
        WIDTH  100
        HEIGHT 28
        ACTION ThisWindow.Release
        CAPTION "Release"
        FONTNAME "Arial"
        FONTSIZE 9
        TOOLTIP ""
        FONTBOLD .F.
        FONTITALIC .F.
        FONTUNDERLINE .F.
        FONTSTRIKEOUT .F.
        ONGOTFOCUS Nil
        ONLOSTFOCUS Nil
        HELPID Nil
        FLAT .F.
        TABSTOP .T.
        VISIBLE .T.
        TRANSPARENT .F.
        MULTILINE .F.
        PICTURE Nil
        PICTALIGNMENT TOP
    END BUTTON

END WINDOW

Main.Center
Main.Activate

Return
and the batch file:

Code: Select all

@ECHO OFF
call demo.exe
ECHO ERROLEVEL=%ERRORLEVEL%
PAUSE

IF ERRORLEVEL 3 GOTO Err3
IF ERRORLEVEL 2 GOTO Err2
IF ERRORLEVEL 1 GOTO Err1
IF ERRORLEVEL 0 GOTO Err0
GOTO END

:Err0
ECHO Run for ErrorLevel=0
GOTO END

:Err1
ECHO Run for ErrorLevel=1
GOTO END

:Err2
ECHO Run for ErrorLevel=2
GOTO END

:Err3
ECHO Run for ErrorLevel=3
GOTO END

:END
ECHO End Of Batch
PAUSE

Re: How to set exit code with errorlevel() ?

Posted: Sat Sep 16, 2017 10:08 pm
by BeGeS
I greatly appreciate the annoyance you have taken, edk.

But... it does not work. :cry:

The .bat file always receives errorlevel = 0.

I am doing some tests and among them was this subject, but I will manage in another way. I mean, it's not a problem that I have to solve necessarily.

However, I reiterate my gratitude.

And add that the program is perfectly adjusted to what I have proposed.

;)

Re: How to set exit code with errorlevel() ?

Posted: Sun Sep 17, 2017 8:41 am
by edk
BeGeS wrote: Sat Sep 16, 2017 10:08 pm But... it does not work. :cry:

The .bat file always receives errorlevel = 0
What OS? Which version of HMG?
I tested on HMG 3.4.4 with WinXP 32 and both Win10 32bit/64bit, works fine.