Executing external x++ code


runbuf() executes x++ code passed to it. The code must be defined as a function and can return a value. Parameters passed to runbuf() will be forwarded, but default parameters won’t work. To show how it works, I am going to use this function to execute code read from an external file. Not very useful and you probably wouldn’t want to allow it, it’s just to show that it can be done (easily).
static void ExecuteCodeFromFile(Args _args)
{
#File
AsciiIo asciiIo = new AsciiIo(“c:\\temp\\findCustomer.xpp”,#io_read);
XppCompiler xppCompiler = new XppCompiler();
Source source;
str line;
CustTable custTable;
;
if (asciiIo)
{
asciiIo.inFieldDelimiter(#delimiterEnter);
[line] = asciiIo.read();
while (asciiIo.status() == IO_Status::Ok)
{
source += #delimiterEnter;
source += line;
[line] = asciiIo.read();
}
if (!xppCompiler.compile(source))
error (xppCompiler.errorText());
custTable = runbuf(source,’4000′);
print CustTable.Name;
}
else
print “Could not open file”;
pause;
}
The external file c:\temp\findCustomer.xpp:
CustTable findCustomer(CustAccount _accountNum)
{
return CustTable::find(_accountNum);
}
First the file c:\temp\findCustomer.xpp is read into source. Source is then compiled and if that goes okay it is executed. As you can see ‘4000’ is passed as a parameter simply by adding it to the runbuf() call. You can also see runbuf returns the function’s return value.
I had trouble getting code compiled that I had written using notepad. As it turns out, the compiler does not accept the tab character. So if you are going to try this out, watch out for that.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.