It is currently Mon Nov 19, 2018 12:29 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Detecting app started with "Start"
PostPosted: Thu Oct 25, 2018 3:09 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
Any ideas on a way to detect if the app is run via:

Code:
start myapp.exe

or (actually more important)
Code:
start /w myapp.exe


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Thu Oct 25, 2018 6:48 am 
Offline
Addict
Addict
User avatar

Joined: Tue Oct 09, 2007 2:15 am
Posts: 999
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 5.46 LTS / 5.62 (Windows x86/x64) | Windows10 Pro x64 | Z370 Extreme4 | i7 8770k | 32GB RAM | iChill GeForce GTX 980 X4 Ultra | HAF XF Evo​​
English is not my native language... (I often use DeepL to translate my texts.)


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Thu Oct 25, 2018 4:19 pm 
Offline
New User
New User
User avatar

Joined: Wed Jun 27, 2018 5:12 am
Posts: 8
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Thu Oct 25, 2018 6:11 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Fri Oct 26, 2018 8:48 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 487
Location: Linz, Austria
jassing wrote:
Any ideas on a way to detect if the app is run via:
Code:
start myapp.exe

or (actually more important)
Code:
start /w myapp.exe
Like this?!
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Fri Oct 26, 2018 11:24 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
Nice!!!!

Thank you Chi!


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 12:38 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
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:
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") 


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 4:16 am 
Offline
New User
New User
User avatar

Joined: Wed Jun 27, 2018 5:12 am
Posts: 8
Another one including GetStartupInfo()

Code:
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 5:11 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 2:22 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 487
Location: Linz, Austria
Code:
/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... ;)


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 2:27 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
chi wrote:
Code:
/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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 3:10 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 857
Location: Canada
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Sat Oct 27, 2018 11:24 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 487
Location: Linz, Austria
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting app started with "Start"
PostPosted: Fri Nov 09, 2018 4:32 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1293
Location: (Embarrassed to say country)
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye