#define USE_TCL_STUBS
#include "tcl.h"

#define VERSTR	    STRINGIFY(JOIN(TCL_MAJOR_VERSION,TCL_MINOR_VERSION))

/* make sure we link to the matching stubs library */
#pragma comment (lib, "tclstub" VERSTR ".lib")

/* make sure we load the matching dll from the proper place */
#define TCLPATH	    "c:\\progra~1\\tcl\\bin\\"
#ifdef _DEBUG
#   define TCLDLLNAME  TCLPATH "tcl" VERSTR "d.dll"
#else
#   define TCLDLLNAME  TCLPATH "tcl" VERSTR ".dll"
#endif

#include <windows.h>

HMODULE hTclModule;
typedef Tcl_Interp *(*LPFN_createInterpProc) ();
LPFN_createInterpProc createInterpProc;


Tcl_Interp *
LoadIt(const char *application)
{
    Tcl_Interp *interp;

    hTclModule = LoadLibrary(TCLDLLNAME);
    if (hTclModule == NULL) {
	puts("can't load " TCLDLLNAME);
	return NULL;
    }

    createInterpProc = (LPFN_createInterpProc)
	    GetProcAddress (hTclModule, "Tcl_CreateInterp");
    if (createInterpProc == NULL) {
	puts("can't find Tcl_CreateInterp()");
	return NULL;
    }

    // make an interpreter.
    interp = createInterpProc();

    // initilize our local Stubs table.
    Tcl_InitStubs(interp, TCL_VERSION, 0);

    // setup the library and encoding paths and all that hidden stuff.
    Tcl_FindExecutable(application);

    if (Tcl_Init(interp) != TCL_OK) {
	puts(Tcl_GetStringResult(interp));
	return NULL;
    }

    return interp;
}

void
UnloadIt(Tcl_Interp *interp)
{
    Tcl_DeleteInterp(interp);
    Tcl_Finalize();
    FreeLibrary(hTclModule);
}

int
main (int argc, char *argv[])
{
    Tcl_Interp *interp;
    
    interp = LoadIt(argv[1]);
    if (interp == NULL) {
	return 1;
    }

    // do work here with Tcl.
    Tcl_Eval(interp, "puts stdout \"hello from interp #1\"");

    UnloadIt(interp);

    // Do it again.
    interp = LoadIt(argv[1]);
    if (interp == NULL) {
	return 1;
    }

    // do work here with Tcl.
    Tcl_Eval(interp, "puts stdout \"hello from interp #2\"");

    UnloadIt(interp);

    return 0;
}
