TCL command in C, a simple example Nguyen Truong – GCS VN Aug 5, 2004

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Lecture 20 Arrays and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
The Web Warrior Guide to Web Design Technologies
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Using the Visual Basic Editor Visual Basic for Applications 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
1 of 3 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
1 INF160 IS Development Environments AUBG, COS dept Lecture 06 Title: Dev Env: Code::Blocks (Extract from Syllabus) Reference:
Computing IV Visual C Introduction with OpenCV Example Xinwen Fu.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Introduction to NS2 -Network Simulator- -Prepared by Changyong Jung.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
chap13 Chapter 13 Programming in the Large.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Computer Programming for Engineers Introduction to Programming in C Language on Visual C Platform Intro. Comp. Prog. C-Language1.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
OCC Network Drives  H:\  P:\ 
Tutorial 11 Five windows included in the Visual Basic Startup Screen Main Form Toolbox Project Explorer (Project) Properties.
Active-HDL Interfaces Debugging C Code Course 10.
Active-HDL Interfaces Building VHPI Applications C Compilation Course 9.
Programming with Visual C++ A short review of the process.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Microsoft Visual Studio 2005 Original by Suma Rao Revised by John G. McMahon ( 9/6/2008 )
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Illustration of a Visual Basic Program Running an Ada Program 1 by Richard Conn 11 September 1999.
Writing Tcl-Based Applications In C John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part IV.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Computer Graphics Lab 1 OpenGL.
1 How to Install OpenGL u Software running under Microsoft Windows makes extensive use of "dynamic link libraries." A dynamic link library (DLL) is a set.
Cs423-cotter1 Windows Operating Environment. cs423-cotter2 Windows Operating Environment 32 bit operating environment – Windows XP Microsoft Visual Studio.net,.net2005,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
2-1 Chapter 2 Using VB.NET to Create a First Solution.
Produce the help package
3 Introduction to Classes and Objects.
A bit of C programming Lecture 3 Uli Raich.
Command line arguments
Command Line Arguments
Programmazione I a.a. 2017/2018.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Advanced Programming Lecture 02: Introduction to C# Apps
Chapter 2 - Introduction to Java Applications
Command Line Arguments
Using JDeveloper.
Chapter 3 – Introduction to C# Programming
Tutorial: The Programming Interface
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Java Looking at our first console application in Eclipse
Tutorial 11 Using and Writing Visual Basic for Applications Code
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
SPL – PS1 Introduction to C++.
Presentation transcript:

TCL command in C, a simple example Nguyen Truong – GCS VN Aug 5, 2004

Introduction Introduce how to build a simple TCL command In this document, TCL command is a command (procedure) is implemented in C and called in TCL

Requirements Make splus command, that receives a number and return number + number, in C language. Deploy splus command as a TCL library so that splus command can be called from TCL code

Steps Step 1: Create C DLL Step 2: Deploy C DLL as a TCL library Step 3: Test

Step 1.1a: Create DLL Visual C++ project If you are familiar with Microsoft Visual C++, please skip this step Run Microsoft Visual C++, select menu File>New Select tab Projects Select Win 32 Dynamic-Link Library Select Location and type Project name Press OK

Step 1.1b: Create DLL Visual C++ project Select A simple DLL project Press Finish. Press OK on New Project Information dialog

Step 1.2a: Implement DLL body After finish creating project, there are several files created. We focus into splus.cpp only In order to easily follow subsequent slices, please print out splus.cpp in sample folder.

Step 1.2b: Impl. DLL body – Directives TCL library header This file resides in <TCLROOT>/include This file declares interfaces to C #include "stdafx.h" #include "stdlib.h" #include "tcl.h" #define SPLUS_API extern "C" __declspec(dllexport) Define constant for function-exporting. Functions declared with SPLUS_API are exported so that other applications can access In this example, only one function is exported, Spluspackage_Init

Step 1.2c: Impl. DLL body - TCL command In this document, TCL command is a command (procedure) is implemented in C and called in TCL A standard interface provided by TCL C-Library In TCL command code, behavior of a TCL command is implemented. For this sample, number + number is implemented. It is possible to implement several TCL commands in the same DLL

Step 1.2d: Impl. DLL body - TCL command, Interface Arbitrary data passed to command Not used in this example In fact, it is pointer to void. Return TCL_OK or TCL_ERROR. int splusCommand(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) { . . . } TCL Interpreter data structure Number of arguments came from TCL command Argument values came from TCL command

Step 1.2e: Impl. DLL body - TCL command, Error processing Need 1 parameter at TCL command argv[0] contains DLL name int splusCommand(ClientData clientData, Tcl_Interp* interp, int argc, char* argv[]) { if (argc != 2) // Tell TCL splus needs 'number' parameter interp->result = "wrong # args: should be \"splus number\""; return TCL_ERROR; } . . . Return Error Message to TCL interpreter

Step 1.2f: Impl. DLL body - TCL command, Main function Result is returned as a string int splusCommand(ClientData clientData, Tcl_Interp* interp, int argc, char* argv[]) { . . . char buf[16]; int number = atoi(argv[1]); sprintf(buf, "%d", number + number); // Set result to TCL interpreter Tcl_SetResult(interp, buf, TCL_VOLATILE); return TCL_OK; } Return result to interpreter TCL_VOLATILE means buf will not be available after Tcl_SetResult. Return OK

Step 1.3a: Impl. DLL body – Package Initiation, Interface See Directives Function name must be compliant following pattern: <packagename>_Init SPLUS_API int Spluspackage_Init (Tcl_Interp *interp) { . . . } This function is automatically called by TCL shell when ever user calls package require SplusPackage command or equivalent commands

Step 1.3b: Impl. DLL body – Package Initiation, Stub initiation Ensure that the correct version of TCL is loaded Returns string contains actual version of loaded TCL or NULL SPLUS_API int Spluspackage_Init(Tcl_Interp *interp) { if (Tcl_InitStubs (interp, "8.1", 0) == NULL) return TCL_ERROR; } . . . TCL 8.1 is required TCL 8.x is also accepted as long as x >= 1

Step 1.3c: Impl. DLL body – Package Initiation, Command registration Define new command in TCL interpreter Can register more than one command Add new function, ‘splus’, to TCL interpreter SPLUS_API int Spluspackage_Init(Tcl_Interp *interp) { . . . Tcl_CreateCommand (interp, "splus", splusCommand, ClientData)NULL, Tcl_CmdDeleteProc *)NULL); } Command implementation See Step 1.2

Step 1.3d: Impl. DLL body – Package Initiation, Package registration Indicate package name and version of new package Like TCL command package provide. Package version SPLUS_API int Spluspackage_Init(Tcl_Interp *interp) { . . . Tcl_PkgProvide (interp, "SplusPackage", "1.0); } Package name

Step 1.4a: Compile and build – Project settings In MS Visual C++, select menu Project>Settings In tab Link, add tcl83.lib into end of box Object/library modules. Press OK

Step 1.4b: Compile and build – Environment settings In MS Visual C++, select menu Tools>Options In tab Directories, select Include files in box Show directories for. Click new button and enter <TCLHOME>\include in box Directories. See next slice

Step 1.4c: Compile and build – Environment settings Select Library files in box Show directories for. Click new button and enter <TCLHOME>\lib in box Directories. Press OK

Step 1.4d: Compile and build – Build In MS Visual C++, select menu Build>Build splus.dll. File splus.dll is created

Step 2: Deploy C DLL as a TCL library Create a text file named pkgIndex.tcl with 1 line in the content: package ifneeded SplusPackage 1.0 [list load [file join $dir splus.dll] SplusPackage] Please see article TCL Library - a simple example for further information. Copy splus.dll produced in step 1.4 and pkgIndex.tcl to deployment directory, for example, C:\MyTclCmd. Add deployment directory to environment variable TCLLIBPATH, for example TCLLIBPATH=C:/tcl C:/MyTclCmd Noted: using SPACE as separators between directories

Step 3: Test Start TCL shell Run commands and see result like picture below