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
Thanks,
-John
C:\RUN>myProgram.exe RUNFROMCOMMANDLINE
#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 '_____________________________________________________________________________ '
#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
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment