Detecting app started with "Start"

Windows specific forum
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Detecting app started with "Start"

Post by jassing »

Any ideas on a way to detect if the app is run via:

Code: Select all

start myapp.exe
or (actually more important)

Code: Select all

start /w myapp.exe
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Detecting app started with "Start"

Post by Bisonte »

What do you mean ?

If you need the parameter, use ProgramParameter(). This works in a cmd : my.exe /w ,
or with the desktop icon....

I don't know what you mean with : "start" :?:
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Pierre Bellisle
User
User
Posts: 35
Joined: Wed Jun 27, 2018 5:12 am

Re: Detecting app started with "Start"

Post by Pierre Bellisle »

Bisonte, "start /w" belong to a batch file.

-

Jassing,
If you are familiar with APIs, this one might be of help for you.
GetStartupInfo(STARTUPINFO). Yours to try...

Might also help for: Console window only if run from a console window.

I do not have time to write a demo but I will send you a PM with code
made in another language that can be easilly adapted.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Detecting app started with "Start"

Post by jassing »

Start not only is in batch files, but can also be used from the command line.
I will look at the API/Code this evening. The forum link is mine -- which is actually what I'm trying to refine.
The code (at bottom of page) works great -- except when run with "start /w test.exe"... So if I can detect I've been started with the start command, and not simply the command line, I can code around that situation.
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Detecting app started with "Start"

Post by chi »

jassing wrote:Any ideas on a way to detect if the app is run via:

Code: Select all

start myapp.exe
or (actually more important)

Code: Select all

start /w myapp.exe
Like this?!

Code: Select all

Import "kernel32.lib"
  AttachConsole(PID)
EndImport
#ATTACH_PARENT_PROCESS = -1

If AttachConsole(#ATTACH_PARENT_PROCESS)
  
  GetConsoleScreenBufferInfo_(GetStdHandle_(#STD_OUTPUT_HANDLE), csbi.CONSOLE_SCREEN_BUFFER_INFO)
  
  If csbi\dwCursorPosition\x = 0
    text$ = Chr(13) + "Program was started with 'start /w'. Press 'Enter' To continue..."
  Else
    text$ = Chr(13) + "Program was started from the command line. Press 'Enter' To continue..."
  EndIf
  
  WriteConsole_(GetStdHandle_(#STD_OUTPUT_HANDLE), @text$, Len(text$), 0, 0)
  
  If csbi\dwCursorPosition\x = 0
    ReadConsole_(GetStdHandle_(#STD_INPUT_HANDLE), @null, 0, @null, 0)
  EndIf
  
Else
  
  MessageRequester("", "Program was started the usual way...")
  
EndIf
Et cetera is my worst enemy
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Detecting app started with "Start"

Post by jassing »

Nice!!!!

Thank you Chi!
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Detecting app started with "Start"

Post by jassing »

Not sure how I missed it; but I compiled to "chi.exe" -- when I do start chi.exe -- it works perfectly
but when I do start /w chi.exe - it detects start/w, but it returns immediately.

I incorporated your basic code into my project, that's when I noticed start /w seems to ignore any request for waiting on input; then saw it ignored too for your code.

While your code does what I asked (detecting) any ideas why it seems to ignore code for waiting for input?

--edit--
Compile & run this code, it basically allows two sets of "input" at the readinput. The 1st one it runs it as if at the command prompt normally, the 2nd prompt's input is processed by the program...

Code: Select all

Procedure.s _input_()
  Protected.s inString
  Protected *buffer, buflen=2048
  *buffer = AllocateMemory(buflen)
  If ReadConsole_(GetStdHandle_(#STD_INPUT_HANDLE),*buffer,buflen,@buflen,0)
    OutputDebugString_("Len: "+buflen)
    OutputDebugString_("Success: "+PeekS(*buffer))
    inString = PeekS(*buffer,buflen)
  Else
    OutputDebugString_("Failed read()")
  EndIf
  FreeMemory(*buffer)
  MessageRequester("Input()","Input result: "+inString)
  ProcedureReturn inString
EndProcedure



Import "kernel32.lib"
  AttachConsole(PID)
EndImport
#ATTACH_PARENT_PROCESS = -1

If AttachConsole(#ATTACH_PARENT_PROCESS)
  
  GetConsoleScreenBufferInfo_(GetStdHandle_(#STD_OUTPUT_HANDLE), csbi.CONSOLE_SCREEN_BUFFER_INFO)
  
  If csbi\dwCursorPosition\x = 0
    text$ = Chr(13) + "Program was started with 'start /w'. Press 'Enter' To continue..."
  Else
    text$ = Chr(13) + "Program was started from the command line. Press 'Enter' To continue..."
  EndIf
  
  WriteConsole_(GetStdHandle_(#STD_OUTPUT_HANDLE), @text$, Len(text$), 0, 0)
  
  MessageRequester("Before...","About to get input, Type 'DIR<enter>'"+#CRLF$+"Notice, it displays the directory"+#CRLF$+"Then type 'XYZ<enter> at next prompt'")
  
  s$=_input_()
  
  MessageRequester("Past Input()","Input retrieved:"+s$)

Else
  MessageRequester("", "Program was started the usual way...")
EndIf

MessageRequester("test","All done")  
User avatar
Pierre Bellisle
User
User
Posts: 35
Joined: Wed Jun 27, 2018 5:12 am

Re: Detecting app started with "Start"

Post by Pierre Bellisle »

Another one including GetStartupInfo()

Code: Select all

EnableExplicit
#PB_Compiler_Processor        = #PB_Processor_x64 ;x86 x64
#PB_Compiler_ExecutableFormat = #PB_Compiler_Executable ; #PB_Compiler_Executable #PB_Compiler_Console #PB_Compiler_DLL

 Import "kernel32.lib"
   AttachConsole(PID)
 EndImport
 #ATTACH_PARENT_PROCESS  = -1
 #STARTF_TITLEISLINKNAME = $800 
 Define.CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo 
 Define.STARTUPINFO StartupInformation 
 Define.s sTitle
 Define.s sExtention
 Define.s sMessage
 Define.s sConsoleMessage
 Define.s CrLf

 GetStartupInfo_(StartupInformation)
 sTitle     = PeekS(StartupInformation\lpTitle)
 sExtention = LCase(Right(sTitle, 4))
 CrLf       = Chr(13) + Chr(10)

 If StartupInformation\dwFlags And #STARTF_TITLEISLINKNAME
   If sExtention = ".lnk" 
     sMessage = "This program was started via this shortcut."
   ElseIf sExtention = ".exe" 
     sMessage = "This program was started via Explorer" + CrLf + 
                "or by the Run dialog or by another process." 
   EndIf
   sMessage = sMessage +  CrLf + CrLf + "Exe name is: " + sTitle
 Else
   sMessage = "This program was started from a batch file or command prompt." + CrLf + CrLf +
              "Exe name is:" + CrLf + sTitle
   If AttachConsole(#ATTACH_PARENT_PROCESS)
     GetConsoleScreenBufferInfo_(GetStdHandle_(#STD_OUTPUT_HANDLE), ConsoleScreenBufferInfo)
     If ConsoleScreenBufferInfo\dwCursorPosition\x = 0
       sConsoleMessage = CrLf + "This program was started with [Start /w]." + CrLf
     Else
       sConsoleMessage = CrLf + "This program was started from the command prompt." + CrLf
     EndIf
     WriteConsole_(GetStdHandle_(#STD_OUTPUT_HANDLE), sConsoleMessage, Len(sConsoleMessage), 0, 0)
   EndIf
 EndIf
 MessageBox_(#HWND_DESKTOP, sMessage, "Started from...", #MB_OK | #MB_TOPMOST)
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Detecting app started with "Start"

Post by jassing »

Thanks. I think at this point, we've got the detection down; but getting input to work as expected is my next hurdle. it's an odd one, that makes me think I'm missing something very obvious.
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Detecting app started with "Start"

Post by chi »

Code: Select all

/W or /WAIT  Start application and wait for it to terminate.
It kinda sounds intended... Start and Wait. I don't think you're able to run additional commands with /W
Do you know a program that behaves like you want? I couldn't find one... ;)
Et cetera is my worst enemy
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Detecting app started with "Start"

Post by jassing »

chi wrote:

Code: Select all

/W or /WAIT  Start application and wait for it to terminate.
It kinda sounds intended... Start and Wait. I don't think you're able to run additional commands with /W
Do you know a program that behaves like you want? I couldn't find one... ;)
I think we have a misunderstanding.... the problem is that when used with start/w input fails.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Detecting app started with "Start"

Post by Paul »

It kinda looks like you are blocking yourself when using the /w flag.
With the /w flag, the console is told to wait for your app to finish but your app is waiting for input from the console ;)
Image Image
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Detecting app started with "Start"

Post by chi »

Don't know why I even tried OpenConsole() there, but it works... But I wouldn't dare to publish it like this (without further investigation)

Code: Select all

Import "kernel32.lib"
  AttachConsole(PID)
EndImport
#ATTACH_PARENT_PROCESS = -1

If AttachConsole(#ATTACH_PARENT_PROCESS)
 
  GetConsoleScreenBufferInfo_(GetStdHandle_(#STD_OUTPUT_HANDLE), csbi.CONSOLE_SCREEN_BUFFER_INFO)
 
  If csbi\dwCursorPosition\x = 0
    text$ = Chr(13) + "Program was started with 'start /w'. Press 'Enter' To continue..."
  Else
    text$ = Chr(13) + "Program was started from the command line. Press 'Enter' To continue..."
  EndIf
 
  WriteConsole_(GetStdHandle_(#STD_OUTPUT_HANDLE), @text$, Len(text$), 0, 0)
 
  If csbi\dwCursorPosition\x = 0
    ;ReadConsole_(GetStdHandle_(#STD_INPUT_HANDLE), @null, 0, @null, 0)
    OpenConsole("test")
    PrintN("")
    Print("Input: ")
    a$ = Input()    
    PrintN("Your input: " + a$)
    Input()
    CloseConsole()
  EndIf
 
Else
 
  MessageRequester("", "Program was started the usual way...")
 
EndIf
Et cetera is my worst enemy
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Detecting app started with "Start"

Post by jassing »

Compile to a windows exe.
Open command prompt.
run the exe
when it says "hi 'enter' ..." type "dir" and press 'enter' -- you'll see what I mean.
Post Reply