How to set exit code with errorlevel() ?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

kristalist
Posts: 21
Joined: Thu Apr 23, 2015 2:30 pm
DBs Used: DBF

How to set exit code with errorlevel() ?

Post 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 :-("
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

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

Post by edk »

In my opinion the ERRORLEVEL function works fine.
View sample: run test.bat
ErrorLev.7z
(554.78 KiB) Downloaded 244 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
kristalist
Posts: 21
Joined: Thu Apr 23, 2015 2:30 pm
DBs Used: DBF

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

Post 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.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

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

Post 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.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

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

Post by srvet_claudio »

Try with:
ExitProcess( nErrorLevel )
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
kristalist
Posts: 21
Joined: Thu Apr 23, 2015 2:30 pm
DBs Used: DBF

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

Post by kristalist »

Thank You Dr. Claudio! It's works !
User avatar
BeGeS
Posts: 125
Joined: Fri Jul 14, 2017 10:45 am
DBs Used: DBF
Location: La Mancha, Spain

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

Post 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.
I get by with a little help from my friends
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

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

Post 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
User avatar
BeGeS
Posts: 125
Joined: Fri Jul 14, 2017 10:45 am
DBs Used: DBF
Location: La Mancha, Spain

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

Post 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.

;)
I get by with a little help from my friends
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

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

Post 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.
Attachments
ExitProcess.7z
(1.05 MiB) Downloaded 204 times
Post Reply