TCL shell fron CPP

hello gents,

I would like to open TCL shell from CPP progrem.
I have no idea how can it be done,
do spmeone have any idea or some examples that can give me some help ?


thanks
opening it is easy, you can do that with any tool that invokes another program. There are a bunch of these, like shellexecute family, spawn family, system() which is frowned upon, and more. If you need to talk to it with your program, rather than just spawn it and let the user do whatever, that is more compex and you would probably need a library / sdk for that (its probably also doable for very basic stuff without it using pipes or something, depending on how complex you need the interface to be).
Hi,

my goal is to open a TCL shell from CPP program,
the tcl will run a tcl script.
for now, the VS do not find any tcl library which include to command to open the shell.
Isn't the TCL shell an executable you could start from your executable or are we talking about sth. different here?
yes it is.
but my test(CPP test) include some other function that have to run before the tcl test.
I would like to run my main script(cpp) and this one will open tcl shell that will run some pre-define tcl test, this is way I'm looking for tcl shell.

Then it's best to follow jonnin's advice and try this code:
1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#include <shellapi.h>

#pragma comment(lib, "Shell32.lib")

int main()
{
  // your code
  ShellExecuteA(nullptr, "open", "PATH TO THE SHELL", "parameters", nullptr, SW_SHOW);
}

https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
can you call tcl spawn with the input to run the code you want?
What I mean is that calling tcl.exe or whatever it is will start a command window, on par with spawning a dos/cmd prompt window, which then sits there like a lump until the user types things into it. But some programs support calling them with things, eg system("g++ myprogram.cpp") would work but system ("cmd a.exe") would not; cmd does not accept a command this way.

you need to test that out.
can you invoke tcl to do what you want it to do, from the command line only, with parameters?
If the answer to that is yes, then my approach will do it for you.
if the answer is no, you need something a little stronger.
If the user is expected to interact with the TCL program and make things happen, my approach is also still valid.
Last edited on
just tried to solution provided,
this is the command I ran -
ShellExecuteA(nullptr, ("open"), "C:\Tcl\bin", "parameters", nullptr, SW_SHOW);
but I received an error code 2.
in addition, there is option to invoke tcl shell and tun into the tcl path to run?

thanks,
Error 2 means "The system cannot find the file specified."
"C:\Tcl\bin" You need to supply the name of the executable, not just a path.
The TCL community has information on this subject, e.g.

Invoking Tcl commands from Cplusplus
https://wiki.tcl-lang.org/page/Invoking+Tcl+commands+from+Cplusplus

How to embed Tcl in C applications
https://wiki.tcl-lang.org/page/How+to+embed+Tcl+in+C+applications

Andy
Hi,

I found a way to run the tcl test with the command system();
any chance that someone know how to see the automation script run ?
the tcl include some input from the user, in this case when I using system() I don't have the option to see the output and can not insert the information.
I am not sure why you have been getting the answers you are getting...

It depends entirely on your OS. Suppose you want to run the Tcl script file “hello.tcl” with the single argument “world”. I assume that the script is NOT a GUI script, and that your user has tclsh properly installed. (You must also consider that the script and executable have matching versions.)

Windows auto code = std::system( "cmd.exe /C tclsh hello.tcl world" );

Linux auto code = std::system( "/usr/bin/tclsh hello.tcl world" );

Check the exit code for success or failure:

  -1 means that the script failed to execute
   0 means that the script executed and terminated successfully
  >0 means that the script executed and terminated reporting some kind of failure

All I/O should appear in the same command window as your application’s (if your app is a non-GUI). If your application is a GUI then you may need to add a little additional logic to the Linux version to launch a terminal emulator. I have not bothered to do this... and I don’t recall how to do it off the top of my head. Let us know if you need that. (It isn’t difficult.)

Good luck!

[edit]
BTW, andywestken’s links are a really good place to start if you want to bind Tcl and your application more tightly than simply executing a subprocess and waiting for it to terminate, including good information about why embedding Tcl is usually the wrong way to do it.

From what I understand of your question, though, simply frobbing std::system() is sufficient.
Last edited on
Topic archived. No new replies allowed.