The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

How to integrate Tcl in an application

We have a sample application which implements CLI for our API. The CLI runs our crappy script parser. I want to replace our script parser with Tcl. Can you recommend any places where I can learn how to integrate Tcl into existing application?

I'm quite new to Tcl and I found nothing about Tcl integration in ActiveTcl docs.

Jack Send private email
Wednesday, September 10, 2008
Brent Welch's book, "Practical Programming in Tcl and Tk" is often cited as a good resource.

Here's a simple example from

 /* tclembed.c - Copyright (C) 2003 Pat Thoyts <>
  * Sample of an embedded Tcl application linked using the Tcl stubs mechanism
  * Method taken from
  * ----------------------------------------------------------------------
  * This source code is public domain.
  * ----------------------------------------------------------------------
  * $Id: 2074,v 1.34 2006-08-15 18:00:05 jcw Exp $

 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <tchar.h>

 #define USE_TCL_STUBS
 #include <tcl.h>

 typedef Tcl_Interp *(*LPFNTCLCREATEINTERP)();

 static Tcl_Interp *InitializeTcl(int argc, char *args[]);

 main(int argc, char *argv[])
    Tcl_Interp *interp;
    int r = TCL_OK;

    interp = InitializeTcl(argc, argv);
    if (interp == NULL) {
     fprintf(stderr, "error: failed to initialize Tcl runtime\n");
    } else {
     if (argc > 1) {
        r = Tcl_EvalFile(interp, argv[1]);

    return r;

 static Tcl_Interp *
 InitializeTcl(int argc, char *argv[])
    Tcl_Interp *interp = NULL;
    Tcl_DString dString;
    TCHAR szLibrary[16];
    char *args;
    int nMinor;
    LPFNTCLCREATEINTERP lpfnTcl_CreateInterp;

    for (nMinor = 5; hTcl == NULL && nMinor > 2; nMinor--) {
     wsprintf(szLibrary, _T("tcl8%d.dll"), nMinor);
     hTcl = LoadLibrary(szLibrary);

    if (hTcl != NULL) {
     lpfnTcl_CreateInterp = (LPFNTCLCREATEINTERP)
        GetProcAddress(hTcl, "Tcl_CreateInterp");
     if (lpfnTcl_CreateInterp != NULL) {
        interp = lpfnTcl_CreateInterp();
        if (interp != NULL) {
         Tcl_InitStubs(interp, "8.2", 0);
    return interp;

Wednesday, September 10, 2008
Thanks! I also found

Thursday, September 11, 2008
If you want the TCL script to be able to do anything useful you will also have to hookup interfaces to your programs functions in TCL.
Martin Send private email
Thursday, September 11, 2008
You can find the C-Tcl interfacing chapter of Brent Welch's book (3rd edition) online at .
It's a little out of date, but good background material.

Newsgroup comp.lang.tcl is generally helpful if you have specific questions.
Colin Macleod Send private email
Thursday, September 11, 2008
Don't get carried away with implementing your own types in weird serialisation things in TCL -- just store identifiers in the TCL variables (and suddenly they're just strings or integers) and when you need the structures, look them up somewhere.

The people doing the scripting will never notice (because your verbs will know how to refer through the IDs), but it's far easier than trying to handle TCL's conversions.

It does mean you can't use a lot of the streaming operators, but in an app, you probably don't really want to.
Katie Lucas
Friday, September 12, 2008

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz