string as message argument in c object

José López-Montes's icon

Hi everybody,

I´m VERY newbie to C and to creating externals. I need to pass a string as a messages argument. In this external, when I send the message ' getText "a simple string" ' the method getText must send exactly the same string to the Max window. I only get weird symbols (always different ones in every max session). The external code is in the attachment.

I suppose this a very simple question (related maybe to proxies), but I'm blocked here after readings tutorials and forum threads.
Thanks in advance for your help.

José López-Montes

string_as_argument.txt
txt
Luigi Castelli's icon

Hi there,

you need to declare your t_symbol as a pointer.
Here is your corrected code:
`
////////////////////////// object struct
typedef struct _genomus
{
t_object s_obj;
long s_value;
t_symbol *x_getText;
long m_outlet;
void *s_proxy;
} t_genomus;

///////////////////////// function prototypes
//// standard set
void *genomus_new(t_symbol *s, long argc, t_atom *argv);
void genomus_getText(t_genomus *x, t_symbol *getText);

t_class *s_genomus_class;

int main()
{
post("GenoMus - test version\n");
t_class *c;
c = class_new("genomus", (method)genomus_new, (method)NULL, sizeof(t_genomus), 0L, 0);
class_addmethod(c, (method)genomus_getText, "getText", A_SYM, 0);
class_register(CLASS_BOX, c);
s_genomus_class = c;
return 0;
}

void *genomus_new()
{
t_genomus *x = (t_genomus *)object_alloc(s_genomus_class);
x->s_proxy = proxy_new((t_object *)x, 1, &x->s_value);
x->m_outlet = outlet_new((t_object *)x, NULL);
x->x_getText = NULL;
return x;
}

void genomus_getText(t_genomus *x, t_symbol *getText)
{
post("received string: %s", getText->s_name);
}
`

You might also carefully study the code examples that come with the SDK.
They show a number of useful techniques that you can use in various contexts.

Let me know if the above code works as you want because I just made the corrections without testing the new code.

Best

- Luigi

Peter Castine's icon

You have to understand the difference between Max symbols and C strings.

Max symbols are pointers to a data structure that, among other things, contains a pointer to the C string that is associated with the symbol.

Try this:

`
void genomus_getText(t_genomus *x, t_symbol getText)
{
post("received string: %s\n", getText->s_name);
}
`

For production code you will need to add safety checks (make sure that getText is not null).

There is a lot to digest in there, but you probably need to spend more time with the SDK documentation.

vichug's icon

since i am currenty going through this, here's a concise external tutorial, albeit dating back to max 4.6 unfortunately :
http://www.music.mcgill.ca/~ich/classes/mumt402_06/MaxMSPExternalsTutorials/MaxMSPExternalsTutorial3.2.pdf

and these "Flop" xcode projects by Nicolas Danet are a precious set of dummies for beginners like us :
https://github.com/nicolasdanet/Flop
and i could advise to begin with doing some tutorials, it might seem long and too much for a beginning, but it is really a timesaver to know some basics about what you are doing.

José López-Montes's icon

Thank you very much to all posters for the astonishing quick responses. Luigi, your code is right. As you all say, I must spent a lot of time exploring the SDK. I'm just at the beginning with development.
See you on the net!
jlm