Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL library returning a string from c++ function

Tags:

c++

tcl

I have the following code

int redirect_test(ClientData pClientData, Tcl_Interp *pInterp, int argc, char *argv[])
{
    std::string result_str =  "This is the output from the method";
    Tcl_SetObjResult(pInterp, Tcl_NewStringObj(result_str.c_str(), -1));
    return true;
}

However when I attempt to use this method in the following way

% set m [ redirect_test]
This is the output from the method
% puts $m
can't read "m": no such variable

How can I return values from TCL functions?

like image 575
Rajeshwar Avatar asked Sep 02 '25 04:09

Rajeshwar


1 Answers

The problem is your:

return true;

Tcl command implementations indicate success by returning the C constant TCL_OK (= 0) and indicate errors with TCL_ERROR (= 1). (There are other other defined result codes, but you're advised to not use them if you're uncertain what they mean.) The true gets converted to 1 by the C++ boolint cast operator, which is TCL_ERROR, making the command fail (and your result string is then the error message).

The fix is trivial. Use this instead:

return TCL_OK;
like image 122
Donal Fellows Avatar answered Sep 04 '25 17:09

Donal Fellows