Ticol TCL - HowTo

Howto - Introductory Notes

How to View Ticol's DLL Exports

The following functions are exported from ticol.dll. This includes a number of useful ancillary functions for addressing the console.
The NirSoft DLL Export Viewer can be used to view the exports

ticol-dll-exports.png (19039 bytes)

Exported functions behave as follows. Some of the function names retain the same name as the original Picol source from which Ticol was originally based. There has been no reason to change these names. Some functions are retained for development debugging only

Export Name Description
CentreConsole Centre the console
CloseConsole Close the console
Cls Clear the console window
ConsoleSetFocus Bring the console to the foreground and set focus
Pause Pause and wait for a keypress
WriteLn Write a string to the console with newline
ticol_command The primary mechanism for calling Ticol via the DLL
ticol_eval Use ticol_command in preference to ticol_eval
ticol_exit Exit the Ticol interpreter
ticol_get_encryption_type Read a TCX file to retrieve the encryption type code
ticol_get_script Returns any loaded script as a const string.  Caller should not free
ticol_init Initialise the interpreter. Called automatically by ticol_command as required
ticol_load_script Load a script into the interpreter space, decoding TCX code as necessary via any supplied password
ticol_run_autoexec Run an autoexec.tcl file in the current interpreter space
ticol_set_app_path Override the current working directory. Useful in the VB IDE
ticol_ver Return the Ticol version as a VB compatible string

Top

DLL Function Prototypes

The following prototypes apply to exported functions. These are subject to change

C/C++ Prototype Notes for All Languages
void __stdcall CentreConsole() Centres the console
void _stdcall CloseConsole() Closes the console, releasing handles
void Cls(const int ch=' ',const int colour=-1) Can clear the screen using any ANSI character and standard console colour (0..15)
long __stdcall ConsoleSetFocus() Set focus on the console
short __stdcall Pause(LPCSTR msg="Press a key...",
    short fore=FOREGROUND_DARKGREY,
    short back=BACKGROUND_BLACK)
Pause at the console with optional prompt and text/background colours
WriteLn Unformatted print output with appended CRLF
LPSTR __stdcall ticol_command(LPSTR s,
    const long opt_breakpoint=FALSE,
    const long opt_stacktrace=FALSE,
    const long opt_run_autoexec=FALSE,
    const long opt_debug_mode=FALSE,
    long* result=NULL)
Main DLL interpreter entry point
Will call init automatically as required and cleanup as necessary
long __stdcall ticol_eval(const long argc,
    LPSTR* argv,
    LPSTR* result=NULL,
    const int opt_run_autoexec=FALSE)
Use ticol_command() instead
void __stdcall ticol_exit() Experimental alternative interpreter cleanup. Alternaitve to DLLMain cleanup
long ticol_get_encryption_type(LPCSTR filename) Retrieve the encryption code from a TCX file
LPSTR __stdcall ticol_get_script() Return the Ticol version as a VB compatible string. Not required for script execution.
Might be useful for introspcetion
Free using SysFreeString((BSTR)ptr); from C++
long __stdcall ticol_init(SAFEARRAY** spp=NULL) Initialise the interpreter.
Called automatically from ticol_command() as required so should not need calling
Designed for use with VB SAFEARRAYs
LPSTR __stdcall ticol_load_script(LPCSTR filename,
    LPCSTR password_in=NULL,
    const int run_autoexec=FALSE,
    long* errcode=NULL )
Load a script into the interpreter space.  Can be evaluated using a call to [run]
May be required to load and execute TCX password protected scripts
Environment-locked scripts are currently not supported
TCX support is not yet finalised in ticol.dll
Preferably call via ticol_command()
long __stdcall ticol_run_autoexec(long* errcode=NULL) Run an autoexec.tcl script in the interpreter space
Functionality is provided via ticol_command()
void ticol_set_app_path(LPCSTR s) Override the current working directory.
Useful in the VB IDE where the IDE will foil attempts to change the working directory using CHDIR etc.
LPSTR __stdcall ticol_ver() Return the Ticol version as a VB compatible string
Free using SysFreeString((BSTR)ptr); from C++

Top


How To Call Ticol DLL from Visual BASIC

Calling Ticol from Visual BASIC via the ticol.dll version of the interpreter is reasonably straightforward as long as the correct Declare statements are included with the project. The main recommended interface is ticol_command(). Other exports should be avoided as these are experimental/deprecated.

Note that return values, where specified, MUST be taken, and exported Ticol functions called as a function rather than as a VB command

Public Declare Sub CentreConsole Lib "ticol.dll" ()
Public Declare Sub CloseConsole Lib "ticol.dll" ()
Public Declare Sub Cls Lib "ticol.dll" (Optional ByVal Char As Long = 32, Optional ByVal color As Long = -1)
Public Declare Sub ConsoleSetFocus Lib "ticol.dll" ()
Public Declare Function Pause Lib "ticol.dll" (Optional s As String = "", _
    Optional ByVal fore As Integer = 0, _
    Optional ByVal back As Integer = 0) As Integer
Public Declare Function WriteLn Lib "ticol.dll" (ByVal s As String) As Long
Public Declare Function ticol_command Lib "ticol.dll" ( _
    ByVal s As String, _
    Optional ByVal OptBreakpoints As Long = 0, _
    Optional ByVal OptStackTrace As Long = 0, _
    Optional ByVal RunAutoexec As Long = 0, _
    Optional ByVal DebugMode As Long = 0, _
    Optional ByRef errcode As Long = 0) As String
Public Declare Sub ticol_exit Lib "ticol.dll" ()
Public Declare Function ticol_get_encryption_type Lib "ticol.dll" (ByVal Filename As String) As Long
Public Declare Function ticol_get_script Lib "ticol.dll" () As String
Public Declare Function ticol_init Lib "ticol.dll" (ByRef Argv() As String) As Long
Public Declare Function ticol_load_script Lib "ticol.dll" (_
    ByVal Filename As String, _
    Optional ByVal Password As String = "" _
    Optional RunAutoExec As Long = 0, _
    Optional ByRef ErrorCode As Long = 0) As String) As String
Public Declare Function ticol_run_autoexec Lib "ticol.dll" (Optional ByRef errcode As Long = 0) As Long
Public Declare Sub ticol_app_path Lib "ticol.dll" (ByVal Path As String)
Public Declare Function ticol_ver Lib "ticol.dll" () As String

Simple calling example. Load ticol_wordcount.dll and count words in $qbf

Dim r As Long
Dim Breakpoints As Long
Dim StackTrace As Long
Dim RunAutoexec As Long
Dim DebugMode As Long
Dim Q As String
Q = Chr$(34)
ticol_set_app_path "c:\\tcl"

' Load ticol_wordcount.dll plugin
Debug.Print ticol_command("cls;lib ticol_wordcount.dll", Breakpoints, StackTrace, RunAutoexec, DebugMode, VarPtr(r))
' Call it...
Debug.Print ticol_command("puts " & Q & "[wordcount $qbf] word(s) [wordcount $qbf -chars] char(s)" & Q);
End

Result:

ticol-dll-vb-result.png (2931 bytes)

Top


How To Call Ticol DLL from C/C++

Sample code:

// Example C++ calling ticol.dll (A __stdcall DLL)
// Ensure stack is sufficient for recursion (try 30,000-100,000)
// Loading the DLL dynamically is straightforward

#pragma comment(linker, "/stack:0x300000") 
#include <windows.h>
#include <stdio.h> 

int main(int argc, char** argv)
{
	LPSTR ptr=NULL;
	long breakpoint=0;
	long stacktrace=0;
	long autoexec=0;
	long debug=0;
	long errnum=0;

	// Embedded Tcl source code:
	// DO NOT indent this multi-line literal!
	// Some compilers may also restrict the size of string literals
	// e.g. MSVC 5.0

LPSTR script="\
textcolor magenta\r\n\
puts \"Hello world\"\r\n\
textcolor\r\n\
";

	// Declare funct*
	LPSTR (__stdcall* ticol_command)(LPSTR s,long,long,long,long,long*);

	HINSTANCE hAPI=LoadLibraryA("ticol.dll"); // Load the DLL
	if(hAPI) // Check handle
	{
		ticol_command=(LPSTR (__stdcall*)(LPSTR,long,long,long,long,long*)) \
		GetProcAddress(hAPI,"ticol_command"); // Load the export
		if(ticol_command) // If successful
		{
			printf("test: Loaded DLL ticol.dll->ticol_command()\r\n");
			// Call Tcl ...
			ptr=ticol_command(script,breakpoint,stacktrace,autoexec,debug,&errnum);
			printf("test: ticol_command: Returned result:'%s' errorcode:%li\r\n"\,(ptr?ptr:"NULL"),errnum);
		}
		else
		{
			printf("test: Failed to ticol_command in ticol.dll\r\n");
			return 1;
		}
	}
	else
	{
		printf("test: Failed to load ticol.dll\r\n");
		return 2;
	}
	return 0;
}

 


How to Test ticol.dll via The Windows Test Interface

The test interface allows basic scripts or commands to be run. The image below shows a pop-up console operating and which is spawned from the VB5 test app.  The autoexec.tcl file has run and a simple, 3-line script has been run. Some commands can't be run from VB such as [spawn],  [exec] must be used instead. Encrypted TCX files are handled in all encryption formats.

More recent versions allow the CLI to be launched and which then permits normal CLI operation

ticol-wintest-x450.jpg (30459 bytes)

Showing unit tests running successfully on the DLL version of Ticol

ticol-dll-unit-tests-x450.png (94075 bytes)

Back | Top

Last updated on 15 May 2023 - This page is designed for 1024 x760 and higher resolution displays