For Programmers: Free Programming Magazines  


Home > Archive > Tcl > October 2004 > How to call a C function from a TCL script?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to call a C function from a TCL script?
joe

2004-10-20, 4:07 pm

Hello, I'm new to TCL so this will probably be easy for the experts
out there. I would like to call a C function from my TCL script, can
anyone point me in the right direction where I can learn how to to
this?

thanks,
joe
Aric Bills

2004-10-20, 4:07 pm

"joe" <jjlindula@hotmail.com> wrote in message
news:880397ee.0410201001.505400c1@posting.google.com...
> Hello, I'm new to TCL so this will probably be easy for the experts
> out there. I would like to call a C function from my TCL script, can
> anyone point me in the right direction where I can learn how to to
> this?


There are a number of ways to accomplish that (or something similar to
that); the one that comes closest to what you have in mind is probably to
write a Tcl extension. If you choose to go that route, you'll need to learn
the basics of the Tcl C API. Some resources to help you do that:

*This chapter from Brent Welch's _Practical Programming in Tcl and Tk_ gives
a good overview of the API.
http://www.beedub.com/book/3rd/Cprogint.pdf

*Matti Kärki's sample extension is an excellent example of a minimal
extension, complete with directions on how to compile it:
http://koti.welho.com/mkarki2/files/test.c.txt

Other, easier options include creating an executable of your C code and
communicating with it through a pipe (see the [open] command manpage) or a
socket (see the [socket] manpage). I assume it's also possible to create a
DLL of your C code and call it through ffidl; other newsgroup readers can
provide more on that option than I can.

If you aren't already familiar with the wiki, it's probably worth searching
around there for other possible solutions:
http://mini.net/tcl/

Regards,
Aric


Cameron Laird

2004-10-20, 4:07 pm

In article <880397ee.0410201001.505400c1@posting.google.com>,
joe <jjlindula@hotmail.com> wrote:
>Hello, I'm new to TCL so this will probably be easy for the experts
>out there. I would like to call a C function from my TCL script, can
>anyone point me in the right direction where I can learn how to to
>this?

Melissa Schrumpf

2004-10-21, 3:59 am

joe wrote:

> Hello, I'm new to TCL so this will probably be easy for the experts
> out there. I would like to call a C function from my TCL script, can
> anyone point me in the right direction where I can learn how to to
> this?


Depends...

You want to call a C function, but in what context? Do you wish to
simply call an extant complex C function which you do not wish to port
to tcl? Does it involve large amounts of data (e.g. you want to process
a large data set that you've stored in a tcl variable)? Are you going
to call it infrequently, or repeatedly? Are you intending to trigger an
event in another program that runs for a long time?

The best way to integrate an external function depends a lot on context.

If you are looking to process a large data set, it might be best to
convert your C code into a Tcl extension.

If you have an existing program that sits around and you want to trigger
an event, you might attempt to send it a message using inter-process
communication channels (e.g. sockets, COM, DDE, X-Window system
messages). Some sort of client/server model. These are surprisingly
trivial in Tcl, and if you give some idea of your environment, someone
should be able to point you to example code.

If it's an infrequent call to an OS-specific facility, you might
consider wrapping your C function in a simple command-line utility and
[exec] or [open] it (I've done this a bit with collecting Windows
performance counter data, including server apps on multiple computers
with a client that connects via socket to plot multiple-machine load
statistics).

What is the context of your existing code?

--
MKS
Cameron Laird

2004-10-21, 8:57 am

In article <m_schrumpf_at_yahoo_com_NOT-AEB02E.23364820102004@comcast.dca.giganews.com>,
Melissa Schrumpf <m_schrumpf_at_yahoo_com_NOT@microsoft.com> wrote:
>joe wrote:
>
>
>Depends...
>
>You want to call a C function, but in what context? Do you wish to
>simply call an extant complex C function which you do not wish to port
>to tcl? Does it involve large amounts of data (e.g. you want to process
>a large data set that you've stored in a tcl variable)? Are you going
>to call it infrequently, or repeatedly? Are you intending to trigger an
>event in another program that runs for a long time?
>
>The best way to integrate an external function depends a lot on context.
>
>If you are looking to process a large data set, it might be best to
>convert your C code into a Tcl extension.
>
>If you have an existing program that sits around and you want to trigger
>an event, you might attempt to send it a message using inter-process
>communication channels (e.g. sockets, COM, DDE, X-Window system
>messages). Some sort of client/server model. These are surprisingly
>trivial in Tcl, and if you give some idea of your environment, someone
>should be able to point you to example code.
>
>If it's an infrequent call to an OS-specific facility, you might
>consider wrapping your C function in a simple command-line utility and
>[exec] or [open] it (I've done this a bit with collecting Windows
>performance counter data, including server apps on multiple computers
>with a client that connects via socket to plot multiple-machine load
>statistics).
>
>What is the context of your existing code?

joe

2004-10-21, 8:57 pm

claird@lairds.us (Cameron Laird) wrote in message news:<75ik42-7l1.ln1@lairds.us>...
> In article <m_schrumpf_at_yahoo_com_NOT-AEB02E.23364820102004@comcast.dca.giganews.com>,
> Melissa Schrumpf <m_schrumpf_at_yahoo_com_NOT@microsoft.com> wrote:
> .
> .
> .
> On-target. And don't forget, Melissa, that different
> people think of "a C function" as alternatively its
> source representation (for them, critcl might even be
> the beset choice) or a compiled object image (which
> might better suggest ffidl and friends). You're ab-
> solutely right: while the question looks simple, a
> correct answer requires considerable analysis.


Hello, I already have a script but know I would like to call a C
function that is located in another file, say for example "burn.c"
which burns executable boot code into flash memory. Here is my
function:
void burn()
{
unsigned int *pBurnAdd[6];
unsigned int i;

pINTMEM = (unsigned int *) INT_MEM;
pNOVRAM = (unsigned int *) NOVAD;

MemLen = 8540;

//pINTMEM++;
for (i=0; i<MemLen; i++)
{
loaderData = *pINTMEM;
*pNOVRAM++ = loaderData;
*pNOVRAM++ = loaderData >> 8;
*pNOVRAM++ = loaderData >> 16;
*pNOVRAM++ = loaderData >> 24;
*pINTMEM++;
}

//burn sequence
pBurnAdd[0] = (unsigned int *) 0x10304E38;
pBurnAdd[1] = (unsigned int *) 0x1030B1C7;
pBurnAdd[2] = (unsigned int *) 0x103083E0;
pBurnAdd[3] = (unsigned int *) 0x10307C1F;
pBurnAdd[4] = (unsigned int *) 0x1030703F;
pBurnAdd[5] = (unsigned int *) 0x10304C63;

}

The .tcl file and the burn function are in the same directory. Could I
include some kind of label that I could insert in the function like
START_BURN and STOP_BURN and then reference them in my .tcl script?

thanks for all your suggestions,
joe
Cameron Laird

2004-10-21, 8:57 pm

In article <880397ee.0410211206.5776decb@posting.google.com>,
joe <jjlindula@hotmail.com> wrote:
joe

2004-10-25, 4:02 pm

claird@lairds.us (Cameron Laird) wrote in message news:<8ukl42-9kc.ln1@lairds.us>...
> In article <880397ee.0410211206.5776decb@posting.google.com>,
> joe <jjlindula@hotmail.com> wrote:
> .
> .
> .
> .
> .
> .
> Interesting! What you want certainly can be achieved,
> and might, in fact, make a provocative demonstration of
> critcl. Where are INT_MEM and NOVAD defined?
>
> In principle, the solution is only a couple of minutes
> away from someone experienced in Tcl. It might take a
> while to explain, though ... Please start with my
> question above.


Hello, INT_MEM and NOVAD are just define statements and are equal to
some 32-bit number, I could easily insert the actual value instead of
using a #define statement.

thanks,
joe
Cameron Laird

2004-10-25, 4:02 pm

In article <880397ee.0410250546.287b9623@posting.google.com>,
joe <jjlindula@hotmail.com> wrote:
>claird@lairds.us (Cameron Laird) wrote in message
>news:<8ukl42-9kc.ln1@lairds.us>...
>
>Hello, INT_MEM and NOVAD are just define statements and are equal to
>some 32-bit number, I could easily insert the actual value instead of
>using a #define statement.

spam@controlq.com

2004-10-27, 3:59 pm


On Wed, 20 Oct 2004, joe wrote:
> Hello, I'm new to TCL so this will probably be easy for the experts
> out there. I would like to call a C function from my TCL script, can
> anyone point me in the right direction where I can learn how to to
> this?


MkTclApp is your friend.

http://www.hwaci.com/sw/mktclapp/index.html

Simple interface, and D.R. Hipp has given the community a number of ""
tools. (MkTclApp, SQLite, various widgets ... )

(just a happy customer ... I'm not Hipp).
joe

2004-10-28, 4:00 pm

spam@controlq.com wrote in message news:<20041027110059.D72795@dam.controlq.com>...
> On Wed, 20 Oct 2004, joe wrote:
>
> MkTclApp is your friend.
>
> http://www.hwaci.com/sw/mktclapp/index.html
>
> Simple interface, and D.R. Hipp has given the community a number of ""
> tools. (MkTclApp, SQLite, various widgets ... )
>
> (just a happy customer ... I'm not Hipp).


Thanks everyone for your help, I hope to start playing with this soon.
I'll let you know if I'm successful or need more help.

thanks,
joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com