Help with AREXX in a C program

This forum is for general developer support questions.
User avatar
ktadd
Posts: 48
Joined: Wed Nov 09, 2011 5:50 am

Help with AREXX in a C program

Post by ktadd »

I have a program written in 'C' and I would like to send and Arexx command to a program and get the return value back to my program.
My program doesn't need to support it's own Arexx commands, just send and read return values from another one. Do I need to set up a full Arexx interface in my program but just have an empty command list to do this? I've read through the auto docs but am still a bit unclear on how to do this. Is there any example code that shows how to do this? Any help or pointers would be greatly appreciated.
Kevin - X1000 first contact / uA1
chris
Posts: 562
Joined: Sat Jun 18, 2011 11:05 am
Contact:

Re: Help with AREXX in a C program

Post by chris »

You need to create an ARexx object using arexx.class, then you can simply call something like the following:
IDoMethod(arexx_obj,AM_EXECUTE,"TOFRONT","NETSURF",NULL,NULL,NULL,NULL);

One of those NULLs is the place to put a char** to capture the result, you'll need to check the Autodoc for AM_EXECUTE for that.
User avatar
ktadd
Posts: 48
Joined: Wed Nov 09, 2011 5:50 am

Re: Help with AREXX in a C program

Post by ktadd »

chris wrote:You need to create an ARexx object using arexx.class, then you can simply call something like the following:
IDoMethod(arexx_obj,AM_EXECUTE,"TOFRONT","NETSURF",NULL,NULL,NULL,NULL);

One of those NULLs is the place to put a char** to capture the result, you'll need to check the Autodoc for AM_EXECUTE for that.
Unfortunatly it doesn't appear to be quit that simple. From what I can gather that would work if I were executing a command in my programs own Arexx port. I'm trying retrieve a return value from MPlayer's Arexx port. According the the autodocs I need to set up a hook function to get the values.

Here is what I have done but it seems my hook function never gets called. The PAUSE command does get sent to MPlayer and the playback pauses so I know my commands are getting to MPlayer. Any ideas on what I might be missing?
Since I don't expect to receive any ARexx commands I haven't set up an Arexx port and I set AREXX_Commands to NULL. Is that Ok?

Code: Select all

#include <classes/arexx.h>
#include <proto/arexx.h>
#include "SDI_hook.h"

/* callback hook function */ 
STATIC VOID reply_callback(struct Hook *hook, Object *obj , struct RexxMsg *rxm)
{
     IDOS->Printf("Args[0]: %s\nResult1: %ld   Result2: %ld\n",
                   rxm->rm_Args[0], rxm->rm_Result1, rxm->rm_Result2);
}

MakeStaticHook(reply_hook, reply_callback);  /* setup hook structure. Best to do after function. */


/* I DO THE FOLLWOING IN MAIN() */

Object *arexx_obj;

arexx_obj = IIntuition->NewObject(NULL, "arexx.class",
                                  AREXX_HostName, PROG_NAME,
                                  AREXX_Commands, NULL,
                                  AREXX_ReplyHook, &reply_hook,
                                  TAG_DONE);


/* A BUTTON GADGET EXECUTES THE FOLLOWING WHEN CLICKED */

IIntuition->IDoMethod(objects[OID_AREXX], AM_EXECUTE, "PAUSE", "MPLAYER.1",  NULL, NULL, NULL, NULL);
IIntuition->IDoMethod(objects[OID_AREXX], AM_EXECUTE, "GET_PERCENT_POS", "MPLAYER.1",  NULL, NULL, NULL, NULL);
Kevin - X1000 first contact / uA1
User avatar
ktadd
Posts: 48
Joined: Wed Nov 09, 2011 5:50 am

Re: Help with AREXX in a C program

Post by ktadd »

Thanks for your example Thomas. I've visited your home page in the past and studied several of your examples. They have been very helpful.
I downloaded, studied and compiled the example you supplied. It works as described using the example you gave opening the Ram: but I couldn't get it to send commands to MPlayer for some reason.
Kevin - X1000 first contact / uA1
User avatar
thomasrapp
Posts: 310
Joined: Sat Jun 18, 2011 11:22 pm

Re: Help with AREXX in a C program

Post by thomasrapp »

I forgot to set rm_Action. Fixed (same link as above).
User avatar
ktadd
Posts: 48
Joined: Wed Nov 09, 2011 5:50 am

Re: Help with AREXX in a C program

Post by ktadd »

It's been a while since I worked on this but with the help of ssolie's tutorial on AREXX from last years AmiWest
I was able to track down the problem with my AREXX implamentation. It now works! Thanks Steve!

Also, thanks to other that replies to this thread. I reviewed everyones inputs and learned something from all of them.
Kevin - X1000 first contact / uA1
Spirantho
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 34
Joined: Thu Jan 26, 2012 10:54 am

Re: Help with AREXX in a C program

Post by Spirantho »

Sorry for the thread grave-digging, but I found something yesterday which held me up for days scratching my head.

Certain programs such as Blitz Basic will not pick up an Arexx message if the message node isn't set correctly. It'll just ignore the message completely, and the sending task will hang waiting for a reply.

You need to add this code to Thomas' example, once the rexxmsg has been created:

Code: Select all

((struct Node*)rexxmsg)->ln_Name = "REXX";
Once that is done, Blitz behaves properly. Apparently Python has the same fault...

Hopefully my addendum may help any one else searching for a solution to this problem!
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

Re: Help with AREXX in a C program

Post by xenic »

Spirantho wrote: Certain programs such as Blitz Basic will not pick up an Arexx message if the message node isn't set correctly. It'll just ignore the message completely, and the sending task will hang waiting for a reply.
The same holds true for Dopus4; it will ignore the message unless you add: ((struct Node*)rexxmsg)->ln_Name = "REXX";

When I was looking for similar ARexx examples, I got this bit of code in an example from broadblues:

Code: Select all

if(rm->rm_Result1 == 0)
{
	if(rm->rm_Result2)
	{
		IRexxSys->DeleteArgstring((STRPTR)rm->rm_Result2);
	}
}
I could not find anything about dealing with results from ARexx or an ARexx host in any ARexx documentation but it makes sense that if you receive a result string in a message reply you would be responsible for disposing of the string. The host that sent it to you has no way of knowing when you are finished with it. On the other hand, I don't see how DeleteArgstring() would know where the memory was allocated and how to free it.
AmigaOne X1000 with 2GB memory - OS4.1 FE
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1479
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: Help with AREXX in a C program

Post by tonyw »

As long as the string was allocated by AllocVecTags(), it has an associated header describing its size, etc. But you would have to be sure that the sending program used that method of allocation, not just something from a memory Pool, for instance.
cheers
tony
Post Reply