Tcl_GetObjResult

This C API function is used to retrieve a pointer to the result object of a Tcl command procedure written in C (using the Tcl C API).

The manual page for this function is here: https://www.tcl-lang.org/man/tcl8.5/TclLib/SetResult.htm

A command procedure is set up with Tcl_CreateObjCommand. When the command procedure is called, a Tcl_Obj is automatically created for you with a reference count if 0 (see also Tcl_Obj refCount HOWTO). You use Tcl_GetObjResult to get a pointer to this object. This pointer can then be used to set the value of the result object, which then becomes the result of the Tcl command itself. Assume, your command procedure has calculated something, that is now contained in an integer varible:

 int something;
 something = 2*3+5;

Now you want to return this value as the result:

 Tcl_Obj *resultPtr;
 resultPtr = Tcl_GetObjResult(interp);
 /* now, you have a pointer to the result -> just fill it with the value of the integer: */
 Tcl_SetIntObj(resultPtr, something);
 return TCL_OK;

It is as easy as this. APN Not quite. Any time you modify a Tcl_Obj, you need to first make sure it is not shared (via Tcl_IsShared). Same applies here.

You can find more complete examples of the usage on these pages:


Related functions: Tcl_SetObjResult, and for the deprecated string interface: Tcl_SetResult,Tcl_ResetResult, and Tcl_AppendResult