RunProgram - using variables

Just starting out? Need help? Post your questions and find answers here.
calypso
User
User
Posts: 14
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

RunProgram - using variables

Post by calypso »

This is my first post. I have been using PureBasic for a few small projects.
Sometimes I run into strange (to me) problems and sometimes I can resolve them.
But this one seems to be beyond my 72 year old brain. Can anyone tell me what I am not doing correctly?

I want to run an external program VNC from within a PureBasic program. I have some examples working but when I use a string variable for the program path it no longer seems to work. I have a file list of computers with their IP addresses and the first line of the file is a path to VNCviewer.exe. If I read the first line of the file and get the VNC path into a variable and confirm it is correct, the test program does not seem to launch VNC and attempt to connect to the test IP address. When I put the actual path in the command line as shown in my examples it works fine. Why can I not use variables read from a file?
For something so trivial I have spent hours trying different things to make it work with no success. I appreciate any help on this one.
Thank you
Stephen

I am going to post some examples of my dilemma. lines commented out allowed me run the tests. I just comment/uncomment where needed.

Code: Select all

;=== This Works to launch VNC and pass on the IP address to connect to ===
IPaddr$ = "192.168.23.15"         ;any address will do
VNC$ = "C:\Program Files (x86)\RealVNC\VNCviewer.exe"
RunProgram(VNC$, IPaddr$, "")
End

;=== This does not work === WHY ?
; vnc-list.txt first line contains: C:\Program Files (x86)\RealVNC\VNCviewer.exe
;IPaddr$ = "192.168.23.15"                ;any address will do
;ReadFile(0, "vnc-list.txt")
;VNC$ = ReadString(0)                     ;gets the correct vnc path to VNC$
;CloseFile(0)
;RunProgram(VNC$, IPaddr$, "")            ;does not appear to work ??? 
;End


;=== And This works ===
;IPaddr$ = "192.160.23.15"
;ShellExecute_(0, "open", "C:\Program Files (x86)\RealVNC\VNCviewer.exe", IPaddr$, "", #SW_NORMAL)


;=== And this works ===
;IPaddr$ = "192.160.23.15"
;VNC$ = "C:\Program Files (x86)\RealVNC\VNCviewer.exe"
;ShellExecute_(0, "open", VNC$, IPaddr$, "", #SW_NORMAL)


;=== This does not work === WHY ?
; vnc-list.txt first line contains: C:\Program Files (x86)\RealVNC\VNCviewer.exe
IPaddr$ = "192.168.23.15"                ;any address will do
ReadFile(0, "vnc-list.txt")
VNC$ = ReadString(0)                     ;gets the correct vnc path to VNC$
CloseFile(0)
ShellExecute_(0, "open", VNC$, IPaddr$, "", #SW_NORMAL)
End
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RunProgram - using variables

Post by infratec »

In general it looks Ok, have you ever debuuged your variable:

Code: Select all

Debug "-" + VNC$ + "-"
You also did no check if ReadFile() was successful.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: RunProgram - using variables

Post by BarryG »

Don't forget the file's path is usually needed, and use #SW_SHOW instead of #SW_NORMAL (or #SW_HIDE to not see the app):

Code: Select all

ShellExecute_(0, "open", VNC$, IPaddr$, GetPathPart(VNC$), #SW_SHOW)
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: RunProgram - using variables

Post by luis »

Hi there.

You wrote "If I read the first line of the file and get the VNC path into a variable and confirm it is correct"

But, even if weirder things happened, I have an hard time believing it is correct because the two examples

Code: Select all

;=== This Works to launch VNC and pass on the IP address to connect to ===
IPaddr$ = "192.168.23.15"         ;any address will do
VNC$ = "C:\Program Files (x86)\RealVNC\VNCviewer.exe"
RunProgram(VNC$, IPaddr$, "")
End

;=== This does not work === WHY ?
; vnc-list.txt first line contains: C:\Program Files (x86)\RealVNC\VNCviewer.exe
;IPaddr$ = "192.168.23.15"                ;any address will do
;ReadFile(0, "vnc-list.txt")
;VNC$ = ReadString(0)                     ;gets the correct vnc path to VNC$
;CloseFile(0)
;RunProgram(VNC$, IPaddr$, "")            ;does not appear to work ??? 
;End
would be perfectly equivalent. Maybe the two strings VNC$ just look the same.

Supposing the test conditions are identical and they are not the issue, and the order of the tests do not alter something, compare the memory contents of the variable read from file and the variable manually set in code. You can use ShowMemory() for that.
Worst case scenario you can post a zip with the code and the original text file you are reading from.
Also please mention if you are using the latest version of PB or another.
"Have you tried turning it off and on again ?"
A little PureBasic review
calypso
User
User
Posts: 14
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

Re: RunProgram - using variables

Post by calypso »

I use the debugger to view variables.
The input file is easy to make as the text is in my posted code samples. It's just the one line.
I have even copied the variable VNC$ that is populated to another variable and tried to use the 2nd variable with no change.
I didn't check for a good file open because there is more than just the snippets i posted.
I previously read from the file, and I'm running with the debugger so I know the variable VNC$ is getting populated.

I'm about to trace through the assembly code to find out where it is different and why it is not launching vnc.
There is something wrong somewhere, it's just beyond me. I need to get some sleep now, Thanks for looking into it.
Just try it out yourself.

I have the latest PB Version 6.01 LTS x64.
Thank you,
Stephen
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: RunProgram - using variables

Post by RASHAD »

Hi luis
Glad to see you participate not too much but it's OK :)
Try the next [I didn't test it]

Code: Select all

IPaddr$ = "192.168.23.15"                ;any address will do
ReadFile(0, "vnc-list.txt")
VNC$ = ReadString(0)                     ;gets the correct vnc path to VNC$
CloseFile(0)
RunProgram(Chr(34)+VNC$+Chr(34), IPaddr$, "")            ;does not appear to work ??? 
End
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: RunProgram - using variables

Post by mk-soft »

You don't need to check the assembler.

It is certainly the path to the file you want to read. So always check whether the file is also open and always specify the path to the file or set the path beforehand with SetCurrentDirectory. The current path can be different at the beginning. Depending on the call environment of the programme.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: RunProgram - using variables

Post by luis »

Hi Rashad, thanks, nice to see you :)
mk-soft wrote: Sun Mar 26, 2023 12:21 pm It is certainly the path to the file you want to read.
He said he checked the value read from the file.
calypso wrote: Sun Mar 26, 2023 12:10 pm The input file is easy to make as the text is in my posted code samples. It's just the one line.
Yes, but the file I make may be not the same you are using, for encoding reasons, because it contains an invisible character, etc.

Anyway, I have real vnc installed so I gave it a go with that one.

Windows 10 x64, PB 6.01 x64 ASM backend.
Even used the same name for the file.

Code: Select all

IPaddr$ = "192.168.23.15"        
VNC$ = "C:\Program Files\UltraVNC\vncviewer.exe"
RunProgram(VNC$, IPaddr$, "")
End

Code: Select all

; vnc-list.txt first line contains: C:\Program Files\UltraVNC\vncviewer.exe
IPaddr$ = "192.168.23.15"                
ReadFile(0, "vnc-list.txt")
VNC$ = ReadString(0)              
CloseFile(0)
RunProgram(VNC$, IPaddr$, "")
End
They both work.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: RunProgram - using variables

Post by mk-soft »

Test it!

Code: Select all

EnableExplicit

; !!! Set Compiler option -> Compile/Run -> Create temporay executable in the source directory

Global IPaddr$, ProgramPath$, FileName$, VNC$, r1


; vnc-list.txt first line contains: C:\Program Files (x86)\RealVNC\VNCviewer.exe
IPaddr$ = "192.168.23.15"                ;any address will do

ProgramPath$ = GetPathPart(ProgramFilename())
FileName$ = ProgramPath$ + "vnc-List.txt"
If ReadFile(0, FileName$)
  Debug "Read file " + FileName$
  VNC$ = ReadString(0)
  CloseFile(0)
Else
  Debug "File not found! " + FileName$
EndIf

If VNC$
  Debug "VNC path " + VNC$
  r1 = ShellExecute_(0, "open", VNC$, IPaddr$, "", #SW_NORMAL)
  ;r1 = RunProgram(VNC$, IPaddr$, "", #PB_Program_Open)
  Debug "r1 = " + r1
EndIf

End
P.S.
Welcome

Whether I still want to learn a new prog language at 72, I don't know.
Will support you in any case ;)

How did you start programming?
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: RunProgram - using variables

Post by Jeromyal »

Open file handles can be the devil sometimes.
To not worry if you left your text editor open on your file perhaps use the #PB_File_SharedRead flag in the ReadFile command.
Also I have had trouble in the past with some things not being in a block like if endif so Mk-soft's example above probably wins the show.
calypso
User
User
Posts: 14
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

Re: RunProgram - using variables

Post by calypso »

@Luis,
This second one that you tried and said it works does not work on my PC, I do not know why.
I need to use variables in the RunProgram line I don't have a choice as the users name in the path will vary depending on whose PC it is being run on.

Another issue I have with my PureBasic and I don't know if it is related or if others have the same problem is..
If i make a change in code where I change the value of a variable I have to save the pb file,exit PB and restart PB. If I do not, PB keeps the value of the variable from the last run before the change. File | Reload does not help, have to exit completely and restart PB. Is this normal

I'm now going to look at the last updated in this thread.
-stephen
calypso
User
User
Posts: 14
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

Re: RunProgram - using variables

Post by calypso »

@ Jeromyal
I had no open file handles. Even tried to run the exe. no change.
It is strange.
-stephen
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: RunProgram - using variables

Post by RASHAD »

Hi
ShellExecute_() uses pointers not strings
Try

Code: Select all

;=== This does not work === WHY ?
; vnc-list.txt first line contains: C:\Program Files (x86)\RealVNC\VNCviewer.exe
IPaddr$ = "192.168.23.15"                ;any address will do
ReadFile(0, "vnc-list.txt")
VNC$ = ReadString(0)                     ;gets the correct vnc path to VNC$
Debug VNC$                                  ;To se if you got the correct string
CloseFile(0)
ShellExecute_(0, @"open", @VNC$, @IPaddr$, @"", #SW_NORMAL)
Egypt my love
calypso
User
User
Posts: 14
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

Re: RunProgram - using variables

Post by calypso »

@RASHAD
for your example using pointers did you try it? I tried and it did not work.
I'm still trying to figure out why.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: RunProgram - using variables

Post by BarryG »

I don't use pointers for ShellExecute_() and it works. But I think I read that PureBasic converts them to pointers behind the scenes, for our convenience.
Post Reply