Announcement

Collapse
No announcement yet.

Can my program tell if it's running under control of a .BAT file?

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

    Can my program tell if it's running under control of a .BAT file?

    Is there a mechanism that I can use to enable my PBCC EXE to tell if it is being run from within a .BAT file, versus normal invocation (commandline, desktop shortcut, etc.)?

    Thanks,
    -John

    #2
    I solved this one time by using a different command-line option when the program was invoked.

    Actually, I did this by adding a param when I called it using CreateProcess() and supplied nothing when I invoked the program from the command line.

    e.g,
    Code:
    C:\RUN>myProgram.exe   RUNFROMCOMMANDLINE
    You could also use an environment variable, since your "child" process run from the CMD file will inherit the environment variables of its caller (in this case CMD.EXE).
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


      #3
      Hey John, you may use the old CURSORX/Y trick...

      Code:
      #COMPILE EXE
      #DIM ALL
      #INCLUDE "Win32Api.inc"
      '_____________________________________________________________________________
      
      FUNCTION PBMAIN() AS LONG
      
       'See also GetStartupInfo(StartupInformation) to know if program was started from a shortcut or from Windows File Explorer
      
       IF CURSORX * CURSORY = 1 THEN 'Caret at row 1, col 1
         'If you type on the keyboard at the command prompt to start this program,
         'the caret won't be at row 1, col 1.
         PRINT "Probably started from Windows File Explorer or by shelling"
       ELSE
         PRINT "High probability of being started from a command prompt or a batch file"
       END IF
      
       WAITKEY$
      
      END FUNCTION
      '_____________________________________________________________________________
      '​

      Comment


        #4

        Comment


          #5
          Thanks all, clever ideas!
          Michael and Pierre, I'll keep these in the reference file, as I think Mike's reference to Wayne's code is exactly the thing I need. I'll check it out this week.

          Thanks!!!
          -John

          Comment


            #6
            John, hi, I know I'm a little late to the party here. I read your post and thought about it for a while. It occurred to me WMIC has all kinds of functions that extract details about every process, including its parent, if any. So, I wrote this code below. It probably looks a little clunky, but all it really does is a series of WMIC calls, basically getting the child process ID, the parent process ID, and finally, the name of the parent. If the parent's name is "cmd.exe", that means it's being run inside a batch file.

            This probably isn't bulletproof software. But quick-and-dirty and does the job.

            Code:
            #INCLUDE "win32api.inc"
            
            
            FUNCTION SpawnedByBatch AS LONG
            
                LOCAL temp AS STRING
                LOCAL comspec AS STRING
                LOCAL LocalAppName AS STRING
                LOCAL hFile AS DWORD
                LOCAL ProcessID AS STRING
                LOCAL ParentProcessID AS STRING
            
                LocalAppName = EXE.NAMEX$                            ' the PowerBasic EXE name
                comspec = ENVIRON$("COMSPEC") + " /C "
                temp = BUILD$("wmic process where ", $DQ, _          ' use wmic to get the
                              "name like '", LocalAppName, "'", _    ' Process ID
                              $DQ, " get processid > $_temp_1")
                SHELL BUILD$(comspec, temp), 0
                SHELL BUILD$(comspec, "more $_temp_1 > $_temp_2"), 0 ' poor man's UNICODE converter
                hFile = FREEFILE                                     ' pick thru the output
                OPEN "$_temp_2" FOR BINARY AS #hFile                 ' to get the Process ID
                GET$ #hFile, LOF(#hFile), temp                       ' of the PowerBasic
                CLOSE #hFile                                         ' program that's
                REPLACE ANY $CRLF WITH ",," IN temp                  ' currently running
                temp = TRIM$(temp, ANY BUILD$($NUL, ","))
                ProcessID = PARSE$(temp, PARSECOUNT(temp))           ' here is the Process ID
            
                temp = BUILD$("wmic process where processid=", _
                               ProcessID, _
                               " get parentprocessid > $_temp_3")
                SHELL BUILD$(comspec, temp), 0
            
                SHELL BUILD$(comspec, "more $_temp_3 > $_temp_4"), 0 ' poor man's UNICODE converter
                hFile = FREEFILE                                     ' pick thru the output
                OPEN "$_temp_4" FOR BINARY AS #hFile                 ' to get the Process ID
                GET$ #hFile, LOF(#hFile), temp                       ' of the PB's parent
                CLOSE #hFile
                REPLACE ANY $CRLF WITH ",," IN temp
                temp = TRIM$(temp, ANY BUILD$($NUL, ","))
                ParentProcessID = PARSE$(temp, PARSECOUNT(temp))     ' here is the Parent Process ID;
                                                                     ' now we need to get the name
                                                                     ' of that parent process
            
                temp = BUILD$("wmic process where processid=", _
                               ParentProcessID, _
                               " get ExecutablePath > $_temp_5")
                SHELL BUILD$(comspec, temp), 0
                SHELL BUILD$(comspec, "more $_temp_5 > $_temp_6"), 0 ' poor man's UNICODE converter
                hFile = FREEFILE                                     ' pick thru the output
                OPEN "$_temp_6" FOR BINARY AS #hFile                 ' to get the name of the
                GET$ #hFile, LOF(#hFile), temp                       ' parent process
                CLOSE #hFile
            
                KILL "$_temp_*"                                    ' clean up the temporary files
            
                IF INSTR(LCASE$(temp), "cmd.exe") > 0 THEN
                    ' if here, this means the PB's parent process had "cmd.exe"
                    ' in its name, indicating it was started from a batch file
                    FUNCTION = %TRUE
                ELSE
                    FUNCTION = %FALSE
                END IF
            
            END FUNCTION
            
            
            FUNCTION PBMAIN () AS LONG
            
                IF ISTRUE SpawnedByBatch THEN
                    PRINT "this was spawned by a batch file."
                ELSE
                    PRINT "this was not spawned by a batch file."
                END IF
            
                WAITKEY$
            
            END FUNCTION​
            Christopher P. Becker
            signal engineer in the defense industry
            Abu Dhabi, United Arab Emirates

            Comment


              #7
              Mr. Becker, The Windows API function GetCurrentProcessID() might simplify your WMIC code in Post 6....
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎