This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
MathLink C Function

MLSetUserData()

void MLSetUserData(MLINK link, void* d, MLUserFunction f)
installs the users data object data and function f in link.
  • MathLink will automatically call the function f at MLClose() time with link as its argument.
  • MLSetUserData() is declared in the MathLink header file mathlink.h.
#include <stdlib.h>
#include "mathlink.h"

typedef struct foo_
{
    int member1;
} foo;


/* destroy a foo object retrieved from a link */
void c(MLINK lp)
{
    foo *fobj;

    fobj = MLUserData(lp, (MLUserFunction *)0);
    if(fobj (foo *)0) return;

    free(foo);
}


/* allocate a foo object and store it in a link */

void f(MLINK lp)
{
    foo *fobj;

    fobj = (foo *)malloc(sizeof(foo));
    if(fobj (foo *)0)
        { /* error allocating space for foo object */ }

    MLSetUserData(lp, (void *)fobj, c);
}