0
votes

I have to dynamically load the DLL interface of libxml2 via LoadLibrary and GetProcAddress under Windows. All of the function pointers I've used are properly loaded, except for xmlFree.

xmlFree is not a normal DLL export, but instead a function pointer. GetProcAddress on "xmlFree" will thus return a pointer to a pointer to the xmlFree function.

typedef void (*LibXmlFree) (void* mem);
LibXmlFree xmlFree = GetProcAddress( hModule, "xmlFree" );

This will thus succeed, but calling this function will fail because xmlFree doesn't point to the real function.

How do I create a proper pointer to DLL's xmlFree(void*) export?

2

2 Answers

0
votes

To assign the real xmlFree pointer you have to dereference the pointer returned by GetProcAddress.

The first part of the cast specifies the result type, the second part dereferences it with the proper type specification.

xmlFree = (void (__cdecl *)(void *))    *((void (__cdecl **)(void *)) GetProcAddress( hModule, "xmlFree" ));

Same should apply to libxml's other function pointers (malloc, realloc & friends).

0
votes

There is a funciton to obtain an address xmlFree:

xmlGlobalState xmlMem = {};
xmlMemGet(  &xmlMem.xmlFree,
            &xmlMem.xmlMalloc,
            &xmlMem.xmlRealloc,
            &xmlMem.xmlMemStrdup
            );
xmlMem.xmlFree( result );

I had similar problem with xmlFree being NULL, while compiling under mingw. Effectively xmlFree() failed with SIGSEGV.