Retrieving DLL handle from the .mxe

BenKissBox's icon

Hello,
I am currently coding a Max object which embeds some system ressources (especially bitmaps). On Mac platform, I get no problem to load the resources from the bundle and use them.
On Windows, it does not work properly, because I need the instance handle passed to the DLL from Max when Max instanciates the object DLL (the .mxe file)
Is there a way to retrieve this handle by calling the Max API (I need it to work under Max5 and Max6)
Thanks by advance

Timothy Place's icon

Good question. I guess there are no examples in the SDK for how to do this. One object that does this in the standard Max distribution is the nslider object (it includes svg resources for the clefs and notes).

In the main function your external will receive a pointer like this (whee g_moduleref is a global/static):

int main(void *moduleRef)
{
    g_moduleref = moduleRef;
    nslider_initclass();
    return 0;
}

In the call to slider_initclass() we fetch the SVG resources with a series of calls for each resource:

    g_xml_treble = nslider_xml_from_resource(g_moduleref,"nslider-gclef");
    g_xml_bass = nslider_xml_from_resource(g_moduleref,"nslider-fclef");
    g_xml_note = nslider_xml_from_resource(g_moduleref,"nslider-notehead");
    g_xml_sharp = nslider_xml_from_resource(g_moduleref,"nslider-sharp");
    g_xml_flat = nslider_xml_from_resource(g_moduleref,"nslider-flat");

Where we've defined nslider_xml_from_resource() as:

const char* nslider_xml_from_resource(const void *moduleRef, const char *resname)
{
    t_max_err err;
    void *data = NULL;
    unsigned long datasize = 0;
    char *str;
    t_atom ext;

    atom_setsym(&ext,gensym("svg"));
    err = jgraphics_get_resource_data(moduleRef, resname, 1, &ext, &data, &datasize);
    if (data && datasize) {
        // we do stuff with the data -- basically just copying it and making sure it is NULL-terminated.
        return str; // str is a pointer to the memory we alloc'd and copied to just above
    }
    return NULL;
}

Hope this helps,
Tim

BenKissBox's icon

Hello Tim,

thanks for the help, I will check this as soon as possible (I am busy on higher priority project for now)
I will keep you informed of the result

Benoit