Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to call a C program from tk 1

Status
Not open for further replies.

desmondkenny

Programmer
Feb 26, 2002
6
0
0
IE
Hey all....

I have a large C program and I don't particular want to change all the code to add in the C API for tcl/tk. I've never used it before and to be honest the API looks complicated.

tk itself is simple enough, though....so is there a way I can call a C program from inside tk without too much difficulty?

thanks,
Des.
 
exec proggy or set pd [open "| proggy" r+], would be
my best guesses.
On unix you can use expect to "spawn" the program.
 
thanks, but what do you mean by spawn?....I am on UNIX, so maybe i could use that?

thanks again,
Des
 
I'm assuming that your existing C program is a command-line driven application. In that case, it is quite possible (and frequently done) to create a Tcl/Tk GUI "wrapper" for your application. The wrapper would be responsible for managing the GUI and for marshalling communications with the existing C application.

In a situation like this, exec is probably not powerful enough of a tool to get the job done. The Tcl exec command runs another application and, optionally, captures its output for use by your Tcl script. However, exec blocks (waits) until the application exits; it can't perform sophisticated interaction with the application, which is what I suspect you'll require.

Tcl has a more elegant mechanism for interprocess communication: command pipelines. In addition to opening simple files, Tcl's open command allows you to start another application and connect to its standard input and and standard output channels through pipes. The syntax is simple:

Code:
set fid [open "| myprog" r+]

Now you can use puts to write to the program's standard input, and gets to read from its standard output.

That's the simple part. The difficult part is to solve the various buffering and blocking problems that can quickly arise. In general, especially with a GUI Tcl program, you'll want to set up event-driven I/O with the pipe using the fileevent command. For more information on all of this, there are several pages on the Tcl'ers Wiki ( you'll probably want to start with
On Unix, the Expect extension is another possibility, that allows your Tcl script to spawn another application under its control, send messages to the application, and expect responses. You can get links to more information about Expect from the Tcl'ers Wiki at
In general, Tcl is excellent in situations like this for creating wrappers to other applications; it's much simpler than doing so in almost any other language. However, the issues involved are subtle and complex. It's easy to get stuck if you don't have a good understanding of what's going on between the applications and how the communication is being managed. If this is your first experience with Tcl/Tk programming, be prepared for a challenge because you're getting into more advanced Tcl programming techniques. I hope that the links I've provided help get you started. And, to get a little plug in here, you can always check out the Tcl training and consulting my company offers. In addition to our basic Tcl and Tk courses, we have a 1-day course on Interprocess Communication and a 2-day couse on Expect that addresses exactly these types of issues.

Good luck! - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top