passing arguments C++ => Tcl 
Author Message
 passing arguments C++ => Tcl

Question ... : I have a problem with passing multiple arguments between
a function written in C and the code in Tcl/Tk.

In C++, I have :

-----------------------------------------------------------------------------------------

Tcl_CreateCommand(interp, "get_info", Tcl_Load_Info, NULL, NULL);

.........................................
.........................................
.........................................

int Tcl_Load_Info(ClientData clientdata, Tcl_Interp* interp, int argc,
char* argv[])
{
...............................
 int a = 5;
 int b = 10;
 int c = 15;
...................................
 return TCL_OK;

Quote:
}

-----------------------------------------------------------------------------------------

In Tcl, I have :

---------------------------------------------------
proc test {} {
"get_info"

.......

# rest of the program, using values a, b, c -> obtained from C-function

Quote:
}

---------------------------------------------------

If the program is running, on a certain moment I get in the function
get_info, a function written in C++ that produces some values for
parameters a,b and c. After returning from this C-function, I want to
continue in tcl with the values that were obtained from this C-function.

My problem : whats the best way to pass the parameters from the
C-function to the tcl-procedure ?

With kind regards.



Tue, 30 Mar 2004 19:21:30 GMT  
 passing arguments C++ => Tcl
Quote:

> Question ... : I have a problem with passing multiple arguments between
> a function written in C and the code in Tcl/Tk.

> In C++, I have :
> Tcl_CreateCommand(interp, "get_info", Tcl_Load_Info, NULL, NULL);
> int Tcl_Load_Info(ClientData clientdata, Tcl_Interp* interp, int argc,
> char* argv[])
> {
> ...............................
>  int a = 5;
>  int b = 10;
>  int c = 15;
> ...................................
>  return TCL_OK;
> }
> -----------------------------------------------------------------------------------------

> In Tcl, I have :

> ---------------------------------------------------
> proc test {} {
> "get_info"

> .......

> # rest of the program, using values a, b, c -> obtained from C-function
> }
> ---------------------------------------------------

Let get_info have three parameters, namely the variable names.
Then you can set the variables from within C (or C++ in your case).
Have a look at Tcl_SetVar and Tcl_SetVar2. which set the variables
in the *current* context:

proc test {} \
  {
  get_info a b c
  puts $a
  puts $b
  puts $c
  }

If you want to use the variables outside test, the best way is
IMHO to handle this at the script level:

proc test {} \
  {
  global a      ;#makes it global
  upvar B b     ;makes it visible in the caller of test
  #nothing      ;#c stays local :-)
  get_info a b c
  }

Greetings!
Volker
--
Das meiste Geld habe ich fuer exklusive Restaurants, schnelle Autos und
teure Frauen ausgegeben. Den Rest habe ich verplempert.



Tue, 30 Mar 2004 20:01:10 GMT  
 passing arguments C++ => Tcl
Peter,

You can return the results in a list:

    Tcl_Obj *objv[3], *result;

    int a = 5;
    int b = 10;
    int c = 15;

    objv[0] = Tcl_NewIntObj(a);
    objv[1] = Tcl_NewIntObj(b);
    objv[2] = Tcl_NewIntObj(c);

    result = Tcl_NewListObj(3, objv);
    Tcl_SetObjResult(interp, result);

Your Tcl code can split up the members of the list using [lindex] or
the [foreach] idiom:

    set x [get_info]
    set a [lindex get_info 0]
    set b [lindex get_info 1]
    set c [lindex get_info 2]

    # Or, equivalently:

    foreach {a b c} [get_info] break

           Stephen

--
Stephen Trier
Technical Development Lab
Cleveland FES Center



Wed, 31 Mar 2004 02:29:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing arguments from C++ to ASM with Borland C++

2. >>>>>>>FROM SMTK TO C++

3. F90 -> F77 Argument passing

4. Argument Passing from VB5 -> DF5

5. passing functions with variable number of arguments as procedure arguments

6. Requiring arguments to be passed as keyword arguments

7. variables for arguments passed to a tcl script?

8. How does one pass Unicode command line arguments to Tcl program (Windows 2000)

9. Difference in exec argument passing at Tcl/Tk 8.4

10. Tcl Blend How to Pass arguments across files

11. passing java objects as TCL procedure arguments

12. how to pass argument to tcl script ?

 

 
Powered by phpBB® Forum Software