PDA

View Full Version : Does not work when there is a space in the path



xLeaves
12-07-2019, 09:06
The OS_GetCommand command has a serious bug. This bug even affects ThinBasic.exe and ThinBasicc.exe.

Programs generated using bundles will also be affected by this bug.

When a bug-affected program works in a directory with spaces, the OS_GetCommand function becomes confusing, the content obtained by the first command line is truncated, and the content obtained by the second command line becomes the first command. The second half of the line content.


MsgBox OS_GetCommand(1)
MsgBox OS_GetCommand(2)
MsgBox OS_GetCommand(3)
MsgBox OS_GetCommand(4)

Generate a program and then pass the command line with a space directory to reproduce the problem.

Because OS_Shell and Shell can't work properly under such circumstances.

So I tried to read the command line from the back to the front and it didn't solve the problem.

For example:


USES "FILE"



Dim sBinPath As String = Replace$(OS_GetCommand(CommandCount - 2), """", "")
Dim sBotPath As String = OS_GetCommand(CommandCount - 1)
Dim sScrPath As String = OS_GetCommand(CommandCount)



APP_SetReturnCode(Shell(sBinPath, sBotPath, true))



File_Kill(Replace$(sBotPath, """", ""))
File_Kill(Replace$(sScrPath, """", ""))
File_Kill(Replace$(sBotPath & ".LastRunTimeError.ini", """", ""))

But dragging the tbasic file to thinbasic.exe is fine.

xLeaves
12-07-2019, 09:07
CommandCount = OS_GetCommands

xLeaves
12-07-2019, 09:42
a complete recurring example:


Script1:

MsgBox OS_GetCommand(1)
MsgBox OS_GetCommand(2)
MsgBox OS_GetCommand(3)
MsgBox OS_GetCommand(4)

Script2:

Shell("bot.exe", """c:\123 456\789"" ""c:\qwe asd\zxc""")


Both scripts generate EXE files,

Run script 1 with script 2 (the file name of script 1 is bot.exe, and the two files must be in the same directory)

When there is no space in the directory, the command line sequence pops up completely different from the space, and is not affected by double quotes. Only the first command line will be troubled by this problem.

The command line we passed with double quotes can be obtained normally, but the first one, the default command line will be split into two, in fact, I use the task manager to view the command line, this parameter is also with double Quotation marks should not be split.

Petr Schreiber
14-07-2019, 22:30
Thanks for reporting the issue - I think I know where it comes from. I will have a look and contact Eros with the fix.


Petr

ErosOlmi
15-07-2019, 20:32
:confused:
mmm ... I do not see the problem
If parameters are between single double quotes " than spaces are not creating any problem.

Below picture if from \thinBasic\SampleScripts\OS\OS_SampleCommandLine.tbasic script executed with the following parameters in command line
"c:\123 456\789" "c:\qwe asd\zxc"

And they seems parsed as expected
9963


Here same script loaded from command line passing parameters
9964


Of course if you pass 2 consecutive double quotes ... it will create a problem because they will interpreted as empty string and the rest will be parsed as parameters so space will considered a parameter change.

I'm I wrong?

xLeaves
15-07-2019, 20:54
The path to the executable contains spaces to reproduce the problem.

ErosOlmi
15-07-2019, 21:33
OK sorry now I got it. I was concentrated on parameters

Yes, it's a bug. I will check soon
9965

ErosOlmi
15-07-2019, 21:55
Please find attached thinBasic_OS.dll module
Can you please download it and substitute the one you have into thinBasic\Lib\ directory
Let me know if this fix the problem

Thanks
Eros

xLeaves
16-07-2019, 09:29
The problem is solved, but the OS_Shell and Shell functions cannot run applications with a space path. This problem still exists.

ErosOlmi
16-07-2019, 21:08
Dear xLeaves,

Regarding thinBasic Shell command:
it is quite easy to solve the problem just adding a quotes string as ApplicationName.

I do not think it is thinBasic responsibility to add double quotes because thinBasic could add unwanted behaves. It is instead programmer responsibility

Shell command is mainly a wrapper of CreateProcessA API function https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa
As stated in that page ... "The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:
c:\program.exe c:\program files\sub.exe c:\program files\sub dir\program.exe c:\program files\sub dir\program name.exe If the executable module is a 16-bit application, lpApplicationName should be NULL, and the string pointed to by lpCommandLine should specify the executable module as well as its arguments.

To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file."

Also stated into thinBasic help: https://www.thinbasic.com/public/products/thinBasic/help/html/index.html?shell.htm

Regarding OS_Shell
Same or similar situation for OS_Shell. Command line can contain not only the executable name and it path but also any required arguments or command-line switches.

If thinBasic would quote the full command it could introduce unwanted behaves.
So again it is programmer responsibility to check if main command needs to be double quotes in order not to let the system to interpret the command in the wrong way


Ciao
Eros

xLeaves
17-07-2019, 19:24
Dear ErosOlmi:

In fact, I added double quotes. In the absence of the command line, they work fine. Once the command line is added, it will not work correctly. This code:


USES "FILE"


Dim sBinPath As String = OS_GetCommand(2)
Dim sBotPath As String = OS_GetCommand(3)


msgbox sBinPath & " " & sBotPath
APP_SetReturnCode(RunApp(sBinPath & " " & sBotPath, %OS_WNDSTYLE_NORMAL, %OS_SHELL_SYNC))
msgbox sBinPath, , sBotPath
sBinPath = Replace$(sBinPath, """", "")
APP_SetReturnCode(Shell(sBinPath, sBotPath, %OS_SHELL_SYNC))

9970

9971

The above image is an image of their runtime, indicating that the parameters are passed correctly, and double quotes are added, but the program still does not start.

Petr Schreiber
18-07-2019, 19:34
Hi xLeaves,

what kind of binary do you launch, please?

I tried to prepare minimal example, and it seems to work nicely:


uses "OS"


string exe_file = """my cool exe\minimal.exe"""
string params = """ciao signore"""


msgbox 0, exe_file + $CRLF + params


os_shell(exe_file + " " + params, %OS_WNDSTYLE_NORMAL, %OS_SHELL_SYNC)


Could you please try to:
- extract the ZIP from attachement
- run the "try_it.tbasic"
- let us know what it did?


Petr

xLeaves
19-07-2019, 15:03
Thank you, ErosOlmi, Petr Schreiber

I found the cause of the problem, because the previous command line parsing problem caused ThinBasic.exe to be deleted, but I did not find this in subsequent tests and thought there were other problems.

Now it works very well, thank you for your help.