AskParam: Asks for command-line parameters via GUI dialog

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: Hacker, petermad, Stefan2, white

Post Reply
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

crconner wrote:But I am asking if you could provide environment variables to the PARENT process.
Unfortunately it is impossible. Environment may only be inherited by child processes, but there is no legal way to pass something back to parent process (it would be a security hole).

When you set envvars within a batch, it happens within same process (cmd.exe) so you can access these vars later. But if you try setting vars from child process (e.g. another batch started using start command), you won't be able to see them from parent batch.

Nested batch call look a bit complex but it isn't hard to do, and since AskParam allows hiding child console windows with /s0, it won't be noticeable for users.
User avatar
nsp
Power Member
Power Member
Posts: 1856
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Post by *nsp »

MVV wrote:
crconner wrote:But I am asking if you could provide environment variables to the PARENT process.
Unfortunately it is impossible. Environment may only be inherited by child processes, but there is no legal way to pass something back to parent process (it would be a security hole).
The only "safe/easy" trick I know is to not send but get !
For this, you generate/write to the output stream from the gui application.
For multiple values you can use

Code: Select all

@MyGUI Param1 Param2 ..... >%temp%\myvars_%ID%.bat
@CALL %temp%\myvars_%ID%.bat
@ DEL %temp%\myvars_%ID%.bat
Inside you just write multiple @SET myVar1="my value 1"

If you only have one value to get :

Code: Select all

for /F "delims=" %%I in ('@MyGUI Param1 Param2 ..... ') do (SET MyVAR=%%I)
...
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

It is a non-standard thing for GUI (i.e. non-console) applications to write to output stream.

Anyway I think that using multiple entrances would be much simpler and readable than using temporary batch files defining variables:

Code: Select all

@echo off
if not -%1==- goto %1

:label1
AskParam.exe /p"First question:" /s0 %0 label2
goto :EOF

:label2
set q1=%AskParam1%
AskParam.exe /p"Second question:" /s0 %0 label3 & goto :EOF

:label3
set q2=%AskParam1%
AskParam.exe /p"Third question:" /p"Fourth question:" %0 label4 & goto :EOF

:label4
set q3=%AskParam1%
set q4=%AskParam2%
echo Well, first four answers were '%q1%', '%q2%', '%q3%' and '%q4%'. Fine.
pause
goto :EOF
Every phase ends with goto :EOF on a line after executing AskParam or at the same with it but after & character which splits a line to multiple commands. Parameter /s0 hides console window of next phase.
User avatar
nsp
Power Member
Power Member
Posts: 1856
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Post by *nsp »

MVV wrote:I'm still thinking that using multiple entrances would be much simpler and readable than using temporary batch files defining variables:
I can agree that it seems to be simple/readable but in fact you stack multiple call to the command interpreter as it is askparam that call the next step...
_AskParam (L1)=> Batch L2
____AskParam (L2)=> L3
_______AskParam (L3) => L4
_________AskParam (L4)
_________EOF
_______EOF
____EOF

The old fashion with temporary file or with the "for /F" is for me more flexible. If we consider one line for the variable and one for the value, the for /F call is easy and efficient !
batch call
This is a sample

Code: Select all

Setlocal ENABLEEXTENSIONS EnableDelayedExpansion
SET COMMAND=askparmToStdout params ..
SET VAR=
for /F "delims=" %%I in ( '%COMMAND%' ) DO (
 if "!VAR!-"=="-" (
   SET VAR=%%I
  ) else (
    SET !VAR!=%%I
    SET VAR=
 )
)
I can admit that it is less compact than the temporary intermediate batch but askparam is for real batch writers ;)

For your test just try to output:

Code: Select all

Var1
value1
Var2
value2
var3

var4
var3 was erased !
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

My way won't stack calls w/o /w parameters. Anyway AskParam and cmd.exe don't take too much memory so it is not a problem at all. And one of most noticeable benefits of my way - it really works now. :D

You can play with your way by calling cmd.exe from AskParam like that:

Code: Select all

@echo off
set tmp_out="%temp%\$askparam$tmp$%random%$"
AskParam /w /p"First question:" /p"Second question:" /s0 cmd.exe /c "echo set AskParam1=%%1>%tmp_out% & echo set AskParam2=%%2>>%tmp_out%"
type %tmp_out%
AskParam /w /p"First question:" /p"Second question:" /s0 cmd.exe /c "echo %%1>%tmp_out% & echo %%2>>%tmp_out%"
type %tmp_out%
pause
del %tmp_out%
User avatar
crconner
Junior Member
Junior Member
Posts: 23
Joined: 2004-12-25, 22:44 UTC
Location: USA

Post by *crconner »

MVV wrote: Unfortunately it is impossible. Environment may only be inherited by child processes, but there is no legal way to pass something back to parent process (it would be a security hole).
I think I misled you by my use of the word Parent. I used that word because several times you and others have used the word Child. That is, the process that AskParam starts up from <command to execute> was referred to as a Child process. (Some of the usage came from Google Translations of your Russian Forum topic. Let's blame Google Translate :oops: ). In this context, the process AskParam runs in is the Parent process. That's why I use that word.

What I want is that the environment variables should be available to the SAME process that AskParam runs in. So, if one considers only the process that AskParam was invoked from, the user entered variables should be available to the SAME batch file that invoked AskParam. So when AskParam runs, it reads the user data, sets the new environment variables for the batch process, and returns. Then since this is the same process, the new variables are available to the rest of the batch program.

======

I am assuming that the batch process is the same process that AskParam runs in. I am thinking it does something like a CALL to AskParam, and AskParam returns. If I am wrong, and AskParam runs in a separate process than the batch file that invoked it, then you are perfectly right about the parent / child problem. It kind of centers around what happens when AskParam is invoked (separate process or same process). And does AskParam simply exit, or does it do a Return?


======

If it is the case that AskParam runs in a separate process, then I agree with everything you have said. Then, your multiple entry schemes make sense. Also, using a temporary file would make sense.

Since you obviously are light years ahead of me, I'll bet AskParam runs in its own process, and I did not realize that?
My hero, Mr. Ghisler. License #37856 since 1999.
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

What I want is that the environment variables should be available to the SAME process that AskParam runs in. So, if one considers only the process that AskParam was invoked from, the user entered variables should be available to the SAME batch file that invoked AskParam. So when AskParam runs, it reads the user data, sets the new environment variables for the batch process, and returns. Then since this is the same process, the new variables are available to the rest of the batch program.
It is the thing that is impossible. When we're talking about cmd.exe (which processes batch file) and AskParam, cmd.exe is a parent process and AskParam is a child one, and when we're talking about AskParam and something started by it, AskParam is a parent and that something is a child. And Windows doesn't allow to send envvars from child processes to parent ones.
And does AskParam simply exit, or does it do a Return?
It is only possible to return an integer value that AskParam transfers from its child (return code, it may be returned from batch using exit [/b] [<number>]), it isn't possible to return text. Console applications may also write to console stream but AskParam is a GUI application and is not attached to console (otherwise you would see a black window together with GUI dialog).
I'll bet AskParam runs in its own process, and I did not realize that?
Yes, every program runs in a separate process, there's no legal way to run multiple programs in same process; only cmd.exe allows calling another batch files within same process because batch files are not processes, they only contain instructions for cmd.exe.
User avatar
crconner
Junior Member
Junior Member
Posts: 23
Joined: 2004-12-25, 22:44 UTC
Location: USA

Post by *crconner »

Thank you MVV for enlightening me. I appreciate the time you took to answer my questions. And thanks for all the useful utilities you have written and shared.

So, I suppose your multiple entry of the batch file is probably the simplest solution.
My hero, Mr. Ghisler. License #37856 since 1999.
User avatar
nsp
Power Member
Power Member
Posts: 1856
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Post by *nsp »

MVV wrote:My way won't stack calls w/o /w parameters. Anyway AskParam and cmd.exe don't take too much memory so it is not a problem at all. And one of most noticeable benefits of my way - it really works now. :D

You can play with your way by calling cmd.exe from AskParam like that:

Code: Select all

@echo off
set tmp_out="%temp%\$askparam$tmp$%random%$"
AskParam /w /p"First question:" /p"Second question:" /s0 cmd.exe /c "echo set AskParam1=%%1>%tmp_out% & echo set AskParam2=%%2>>%tmp_out%"
Thanks MVV I appreciate a lot your work specially VP and ASkparam ;)

To be clear and summarize, you designed AskParam to be a wrapper in order to get in a very clean way some parameters and pass it to a called proggy/script..
Without this subsequent call the parameters/entries are lost. so It is not intended to be an GUI only callable from batch without re-entrance.

The main usage is to nicely call software from TC UserCmd or in VirtPan Scripts ! You did a very good tool that is a zillion times more efficient than the ? of TC :lol:.

The stdout without child call is another approach that is more related to std batch scripting than TC !
crcorner wrote:So, I suppose your multiple entry of the batch file is probably the simplest solution.
Yes ! As said MVV it works already in the current version ! You will not loose any of the previous mapped variable as you complement your env during successive call...
Last edited by nsp on 2015-02-11, 07:26 UTC, edited 1 time in total.
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

crconner wrote:So, I suppose your multiple entry of the batch file is probably the simplest solution.
It is really the simplest solution, and it is already available. I think this structure is quite easy to create/edit/understand.
nsp wrote:To be clear and summarize, you designed AskParam to be a wrapper in order to get in a very clean way some parameters and pass it to a called proggy/script..
Without this subsequent call the parameters/entries are lost. so It is not intended to be an GUI only callable from batch without re-entrance.
Perhaps ConPaste was a trigger to create AskParam, or maybe limitations of ? (I don't remember it correctly).
Anyway, reentrance wasn't in mind when AskParam was born, it only required when you can't ask all questions in a single AskParam call (e.g. when some questions or suggested answers depend on previous answers), and it provides much better flexibility.
sgp
Senior Member
Senior Member
Posts: 355
Joined: 2005-01-31, 16:04 UTC

Post by *sgp »

MVV wrote:And Windows doesn't allow to send envvars from child processes to parent ones.
I have a command-line utility that does allow setting an envvar in another process by process id. It works on WinXP SP3 32bit. I wouldn't be surprised if it didn't work on x64 Windows and Windows 7/8. I used to use this utility to retro-set Explorer. Since WinXP SP3 Explorer got smarter and it started self-deleting injected envvars when it found them. So I stopped using this utility.
However - with some limitations - setting envvars across processes is still possible. Windows implements envvars in a multi-stack way (this is my way to view it, it isn't a precise description). The top of the stack is the process memory. Lower in the stack, or in parallel stacks, you find registry envvar keys (User, Volatile, System come to mind). For instance, Windows initializes envvar PATH in the System registry then a process can overwrite PATH value in its process memory. PATH registry value stays unchanged, but the process sees a different value. User and System registry envvar keys survive reboots. Volatile doesn't. So if AskParam was able to set/get System/User/Volatile registry envvars, a parent application could actively read the System/User/Volatile envvar values instead of defaulting to the envvar values in process memory. I have tested this kind of gimmicks up to Win7 and they do work. The need to actively engage in reading values from the registry is limitation #1. This isn't as much of a limitation if you're writing the application that runs askParam. However, for pre-built applications the app needs to refresh its environment in order to see changed registry envvar values, but of course an end-user can't control if/when an app will refresh, if it ever will (most don't). So, in practical terms, using registry envvars can be effective mostly for batch scripts.
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

There is no legal and stable way to pass envvar to parent process.
I have a command-line utility that does allow setting an envvar in another process by process id.
It is possible to write some code to process memory and create remote thread in order to set new envvar value, but such way isn't legal, antiviruses may/should/will treat it suspicious.
So if AskParam was able to set/get System/User/Volatile registry envvars
This may require elevation, and also not all programs are able to refresh their envvars w/o restarting (perhaps it was meant by your actively reading; actually there is a broadcast message WM_SETTINGCHANGE that informs about updates, but not all programs process it, especially console ones that have no windows to which message is sent).
sgp
Senior Member
Senior Member
Posts: 355
Joined: 2005-01-31, 16:04 UTC

Post by *sgp »

MVV wrote: This may require elevation, and also not all programs are able to refresh their envvars w/o restarting (perhaps it was meant by your actively reading; actually there is a broadcast message WM_SETTINGCHANGE that informs about updates, but not all programs process it, especially console ones that have no windows to which message is sent).
Correct. Your explanations confirm my observations.
User avatar
MVV
Power Member
Power Member
Posts: 8703
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

AmandaPratt126,
Thank you!
Sorry, there is no complex documentation with tutorials. I've tried to add a Readme but it is very poor yet. However I think you can read entire topic in order to get more examples.
Of course, feel free to ask if you need something special. You only need to provide desired scenario.
User avatar
xmeron
Junior Member
Junior Member
Posts: 90
Joined: 2009-06-17, 19:43 UTC

Post by *xmeron »

AskParam.exe is a good tool, and it seems need a readme.txt file, such as phrase "cut hints", I checked in dict and still don't know the exact meaning. Other problems are like:
8 modes of show mode only list out 4 of them.
Tried but don't know the difference of /rd and /rl.
The only one example is in help dialog window, but this example cannot be copied, I have to type letters one by one then run it, and Windows(not by TC) gave me a message below:
Windows cannot find 'OMMANDER_PATHTOTALCMD.exe'. Make sure you typed the name correctly, and then try again.
Seems because you added %COMMANDER_PATH% to system environment variables, so you can get the right result?

For the parameter /v, when the dialog width is not default, it is not in the middle of screen, I think centering this dialog should be better.

And, I wanna use AskParam.exe to implement the function: Renaming single file with dialog (Because some filenames are too long to inline-rename conveniently). I tried several styles for AskParam, still cannot get right renaming result, maybe please also add this frequently used example to readme.txt file? Thanks.
Post Reply