# Why am I getting an error from MLPutFunction after getting the MathLink from LibraryLink?

Based on the example on the documentation page for processMathLink, I've got the following C++ code:

void MathematicaFunctionDescriptor::Register(WolframLibraryData lib_data) const
{
Checked(&MLPutFunction, link, "EvalutatePacket", 1, "MLPutFunction(EvaluatePacket, 1) failed");
Checked(&MLPutFunction, link, "Set", 2, "MLPutFunction(Set, 2) failed");
Checked(&MLPutSymbol, link, maFunctionName.c_str(), "MLPutSymbol(" + maFunctionName + ") failed");
Checked(&MLPutString, link, mgFunctionName.c_str(), "MLPutString(\"" + mgFunctionName + "\") failed");
Checked(MLPutSymbol, link, output.c_str(), "MLPutSymbol(" + output + ") failed");
if (errorCode != 0)
throw GMathematica::CallBackError(errorCode, "Cannot register function " + maFunctionName);
if (nextPacket == RETURNPKT)
}


This is called from a function initializeAll which catches all exceptions, and converts them into error returns to Mathematica. Checked is a simple template function which wraps a call to its first arguement, checking its return status, and throwing an exception with its last argument as a message if the call fails.

I've invoked this from Mathematica by:

LibraryFunctionLoad[ "MaCAL", "initializeAll", {}, Integer ]
%[]


The LibraryFunctionLoad seems to work; the dll "MaCAL" contains the function "initializeAll", which loads all of the necessary DLLs (about 20) in the correct order, then invokes the wrapper for the above function in the DLL "MaCALFrontEnd". I get back the error message "MLPutFunction(EvaluatePacket, 1) failed". Regretfully, I don't know how to find out why it failed.

More generally: I'm trying to execute "LibraryFunctionLoad" for all of my functions, from the C++ code invoked by the first function. (I actually tried this first in WolframLibrary_initialize; I moved it to a separate function when this failed, but with no noticeable difference.) Is it possible to do this from WolframLibrary_initialize? And is this the correct technique, or is there a better solution?

And more generally, is there some good literature, on the net or in the form of a book, which explains how to do these sort of things. The Wolfram documentation is some of the worst I've encountered—they seem to confuse quantity with quality.

-