Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

test if an application is running and return ERRLEVEL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • test if an application is running and return ERRLEVEL

    this program was written out a need to test if a certain program or batch file or another instance of the same program is running
    use the program consname.bas to name your window caption

    in using this program for a batch file
    you would test for a application's(console running batch file) caption
    the if the ERRLEVEL returns a 1 then the application(batch file) is already running(such as a backup routine)then usually abort the program because you do notwant another instance of the program running.

    if the ERRLEVEL returns a 0 or otherwise, place the name in the caption using consname then continue with your statments in the batch file.

    consname.exe and consname.bas are located in the attached zip file

    Code:
    REM ISITRUNG,BAS
    REM compiled with pbcc4.4 powerbasic
    
    
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    
    GLOBAL scompare$
    GLOBAL foundrunning AS LONG
    
    FUNCTION EnumCallback (BYVAL hWnd AS LONG, lParam AS LONG) AS LONG
    DIM szCaption AS ASCIIZ * %MAX_PATH, Result AS LONG
    Result = GetWindowText(hWnd, szCaption, SIZEOF(szCaption))
    IF TRIM$(szCaption) <> "" THEN
      Result = FindWindow ("", szCaption)
    
       IF scompare$="" THEN
         REM PRINT "hWnd: " & FORMAT$(Result) TAB(16) szCaption
         PRINT szCaption
         WAITKEY$
         ELSE
            TEMP$=TRIM$(UCASE$(SZCAPTION))
            DO WHILE INSTR(TEMP$,"  ")
            REPLACE "  " WITH " " IN TEMP$
            LOOP
            TEMP$=TRIM$(TEMP$)
            IF TEMP$=UCASE$(scompare$) THEN
               foundrunning=1&
            END IF
       END IF
    END IF
    FUNCTION = 1
    END FUNCTION
    
    FUNCTION PBMAIN AS LONG
    LOCAL textsearched AS STRING
    scompare$=COMMAND$
    scompare$=TRIM$(scompare$)
    textsearched=scompare$
    
    DO WHILE INSTR(scompare$,"  ")
    REPLACE "  " WITH " " IN scompare$
    LOOP
    scompare$=TRIM$(scompare$)
    
    IF LEN(scompare$)=0 THEN PRINT:PRINT "list of applications running":PRINT STRING$(78,196)
    
    EnumWindows CODEPTR(EnumCallback), 0&
    
    IF LEN(scompare$)<> 0 AND foundrunning=0& THEN
        scompare$="command prompt - "+scompare$
        EnumWindows CODEPTR(EnumCallback), 0&
        END IF
    
    IF LEN(scompare$)=0 THEN
         PRINT STRING$(78,196)
         PRINT "Check if application running? the name goes on the command tail"
         PRINT "ERRLEVEL 1 is returned if true, otherwise ERRLEVEL 0 is returned"
         ELSE
         IF foundrunning& THEN
            PRINT "*** "+textsearched+" *** application found running"
            PRINT "ERRLEVEL 1 returned"
            ELSE
            PRINT "*** "+textsearched+" *** did not find this application"
            PRINT "ERRLEVEL 0 returned"
         END IF
    END IF
    
    
    FUNCTION=foundrunning&
    'WAITKEY$
    END FUNCTION
    ex of batch file handling
    Code:
    CLS
    ECHO OFF
    isitrung test me
    IF ERRORLEVEL = 1 GOTO ABORT
    CONSNAME TEST ME
    CLS
    ECHO "program was not already running continue statements"
    PAUSE
    GOTO STOP
    :ABORT
    ECHO " this batch file was already running"
    pause
    :STOP
    exit
    Attached Files
    Last edited by Paul Purvis; 5 Jul 2008, 02:27 PM.
    p purvis

  • #2
    well it ain't pretty and not much tested but it should fly as well as pigs
    after some discussion i created a monster.
    this program will try a little better to not let another instance of a batch file run if a person try runs the batch file as second time very near the first time.

    i do not use the function createfile very often, but hopefully in using it, this program will find if there is another instance of a batch file running.
    the temporary file is just for this program to work its best, not for creating a file to be used as flag for the entire batch processing.
    a temporary file should automatically be deleted at the close of this program and this program will also change the name of the application(batch file caption) if there is not a batch file already running with this captions name.

    so basically you can test for the existence of a batch job running and name the one this program is located in, within one program all together.
    this should reduce the probability of the same batch job running multiple instances.


    the word isitrnit is a terrible name and not very descriptive and it makes you think you are running linux program, ha ha ha.
    the word is suppose to stand for IS IT RUNNING NAME IT.
    maybe somebody else can give it a better 8 character name.


    because this program creates a file in the current directory, i would assume it would fail it the current directory was not set to allow files with create or write or delete permissions.
    i do not know about the rest of the world but all my batch files, or at least most of them start the same


    echo off
    cls
    c:
    md c:\temp
    cd \temp
    cls
    .....rest of batch file commands

    Code:
    REM ISITRNIT.BAS
    REM compiled with pbcc4.4 powerbasic
    
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    GLOBAL scompare AS STRING
    GLOBAL foundrunning AS LONG
    
    FUNCTION EnumCallback (BYVAL hWnd AS LONG, lParam AS LONG) AS LONG
    DIM szCaption AS ASCIIZ * %MAX_PATH
    LOCAL Result AS LONG
    LOCAL stemp AS STRING
    Result = GetWindowText(hWnd, szCaption, SIZEOF(szCaption))
    IF TRIM$(szCaption) <> "" THEN
      Result = FindWindow ("", szCaption)
    
       IF scompare="" THEN
         PRINT szCaption
         ELSE
            stemp=TRIM$(UCASE$(SZCAPTION))
            DO WHILE INSTR(stemp,"  ")
            REPLACE "  " WITH " " IN stemp
            LOOP
            stemp=TRIM$(stemp)
            IF stemp=UCASE$(scompare) THEN
               foundrunning=1&
            END IF
       END IF
    END IF
    FUNCTION = 1
    END FUNCTION
    
    FUNCTION PBMAIN AS LONG
    LOCAL textsearched AS STRING
    LOCAL stempfilename AS ASCIIZ * %MAX_PATH
    LOCAL hfile AS LONG
    scompare=COMMAND$
    
    DO WHILE INSTR(scompare,"  ")
    REPLACE "  " WITH " " IN scompare
    LOOP
    scompare=TRIM$(scompare)
    textsearched=scompare
    
    
    IF LEN(scompare)=0 THEN
        PRINT
        PRINT "list of applications running"
        PRINT STRING$(78,196)
        ELSE
        stempfilename=scompare+".isitrnit.tmp"
        REPLACE " " WITH "_" IN stempfilename
        KILL stempfilename
        hFile = CreateFile(stempfilename,0,0,BYVAL 0&,%CREATE_NEW,%FILE_FLAG_DELETE_ON_CLOSE,BYVAL 0&)
        IF hFile = -1 THEN
            PRINT "*** "+textsearched+" *** application appears to be running"
            PRINT "ERRLEVEL 1 returned"
            FUNCTION = 1
            EXIT FUNCTION
        END IF
    END IF
    
    EnumWindows CODEPTR(EnumCallback), 0&
    
    IF LEN(scompare)<> 0 AND foundrunning=0& THEN
        scompare="command prompt - "+scompare
        EnumWindows CODEPTR(EnumCallback), 0&
        END IF
    
    IF LEN(scompare)=0 THEN
         PRINT STRING$(78,196)
         PRINT "Checks if an application is running?"
         PRINT "The application name goes on the command tail."
         PRINT "If the application is not found running."
         PRINT "This program will name the caption to the program name searched."
         PRINT "This program was intended for script programming and batch files."
         PRINT "ERRLEVEL 1 is returned if true, otherwise ERRLEVEL 0 is returned."
         ELSE
         IF foundrunning THEN
            PRINT "*** "+textsearched+" *** application found running"
            PRINT "ERRLEVEL 1 returned"
            ELSE
            CONSOLE NAME textsearched
         END IF
    END IF
    
    
    FUNCTION=foundrunning
    END FUNCTION
    Attached Files
    Last edited by Paul Purvis; 8 Jul 2008, 12:06 PM.
    p purvis

    Comment


    • #3
      the program had an error
      program test ok as far as i can see
      isitrnit program is mostly for scripts such as batch file processing.
      tested on win2k pro
      issues with existing programs. In particular, %INVALID_HANDLE_VALUE is
      &HFFFFFFFF??? rather than -1& now.

      hfile returned a -1 when not being able to create a temporary file
      not the %INVALID_HANDLE_VALUE value
      code now fixed to look for that value of -1&
      p purvis

      Comment


      • #4
        hfile returned a -1 when not being able to create a temporary file
        not the %INVALID_HANDLE_VALUE value
        code now fixed to look for that value of -1&
        Code:
        LOCAL hfile AS [B]LONG[/B]
        &hFFFFFFFF??? will equal -1& when cast as a LONG

        try
        Code:
        MACRO eq32 (a, b) =   BITS???(A) = BITS???(B) 
        ..
        IF eq32(%INVALID_HANDLE_VALUE, hFile) THEN 
              handle is invalid
        ...
        (Or use PB/DLL 5 or 6, which does unsigned comparisons of LONG/DWORD)
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          i made a change to hopefully recover from a very remote situation of a failure of INITRNIT due to a computer failure.
          the program now deletes the temporary file being used as flag if it exist before it creates the same file. this change should guarantee INITRNIT runs every time correctly even after a computer failure.
          p purvis

          Comment


          • #6
            for processes running

            I tend to use this for determining if an app is already running


            FUNCTION GetAppName AS STRING
            ' return the name of the application
            LOCAL sName AS STRING, zPath AS ASCIIZ * %MAX_PATH
            GetModuleFileName GetModuleHandle(""), zPath , %MAX_PATH
            sName = FileNam(zPath)
            FUNCTION = sName
            END FUNCTION

            IF funProcessCount(GetAppName)>1 THEN
            ' ' more than one version of this running ?
            ' put code here to handle what you want to do
            END IF
            Attached Files

            Comment


            • #7
              Thanks Graham
              that is helpful

              the reason for the way i handled it was to compress any double spaces down to only one space.
              it removes the opportunity for an error in typing the process name with the command and having too many spaces and if there where ever any double spaces in a process name running.
              because of the way i am using the temporary file also in blocking another program from trying to run. i do not believe there will be a chance for another instance to start up before the process gets named. but we have all been surprised as to what can happen.

              i am planning on putting a sleep statement in the program and also an option on the command line for adding a sleep period after the program runs.
              the reason is for that is where a program may rename the window caption(title) and a second instance starting up.
              it would be better for the console program not to place a title in the window caption.
              a user may have a lazy finger and hit the icon on the desktop slowly and this program is designed first in mind to keep a user from starting a second instance of a program.
              if a batch file is being used now with this program, i strongly suggest you place a the program i wrote called SLEEP.EXE right behind this program with a few moments of sleep.
              my sleep program will acknowledge fractions of a seconds, so you could use SLEEP 1 or SLEEP 1.50 or SLEEP 1.75 or SLEEP 2, this might help a users that are older or with disabilities and does not have the full control over their hands or even a child or a teenager who rapes on it like it was a toy.
              Last edited by Paul Purvis; 20 Jul 2008, 01:27 PM.
              p purvis

              Comment

              Working...
              X