From bellis@hsph.harvard.edu  Mon May 14 00:47:25 2001
From: bellis@hsph.harvard.edu (Byron Ellis)
Date: Sun, 13 May 2001 19:47:25 -0400 (EDT)
Subject: [Rd] Dynamic C Symbols and Embedding Suggestion
Message-ID: <Pine.GSO.4.10.10105131930300.12616-100000@hsph.harvard.edu>


Hello, I've been playing around w/ the new dynamic C symbol stuff (thank
you for doing that!) in the, um, 5/11/01 R-devel package and, unless I've
missed something incredibly obvious, there doesn't appear to be a
mechanism for registering functions of an embedding executable. It seems
like this would be a Good Thing to have. 

Since I'm doing most of my work in Windows at the moment (all of the
embedding apps I'm working on are Windows based so I've not really messed
w/ the *NIX embedding stuff) I did a little experiment to see if I could
bind my primary executable. Adding the code below to Rdynload.c seemed to
work (in the Windows case). You'll note the remarkable resemblance to
AddDLL. :-) My test code calls this to set up its function list just after
R_ReplDllinit() just before entering the do1-loop--- though perhaps this
should be integrated into some part of the setup procedure for embedding
apps.


DllInfo* R_RegisterEmbeddedExecutable(HINSTANCE handle,char* path)
{
	char* dpath,*name,DLLname[PATH_MAX], *p;

	DeleteDLL(path);
	if(CountDLL == MAX_NUM_DLLS) {
		strcpy(DLLerror,"unable to register embedded executable.
too many libraries");
		return 0;
	}

    dpath = malloc(strlen(path)+1);
    if(dpath == NULL) {
	strcpy(DLLerror,"Couldn't allocate space for 'path'");
	return 0;
    }
    strcpy(dpath, path);
    
    if(R_osDynSymbol->fixPath)
	R_osDynSymbol->fixPath(dpath);

    p = strrchr(dpath, R_DIR_SEPARATOR); 
    if(!p) p = dpath; else p++;
    strcpy(DLLname, p);
    p = strchr(DLLname, '.');
    if(p) *p = '\0';
    name = malloc(strlen(DLLname)+1);
    if(name == NULL) {
	strcpy(DLLerror,"Couldn't allocate space for 'name'");
	free(dpath);
	return 0;
    }
    strcpy(name, DLLname);

    LoadedDLL[CountDLL].path = dpath;
    LoadedDLL[CountDLL].name = name;
    LoadedDLL[CountDLL].handle = handle;

    LoadedDLL[CountDLL].numCSymbols = 0;
    LoadedDLL[CountDLL].numCallSymbols = 0;
    LoadedDLL[CountDLL].numFortranSymbols = 0;
    LoadedDLL[CountDLL].CSymbols = NULL;
    LoadedDLL[CountDLL].CallSymbols = NULL;
    LoadedDLL[CountDLL].FortranSymbols = NULL;

	return LoadedDLL + (CountDLL++);

}



Byron Ellis (bellis@hsph.harvard.edu)

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] Dynamic C Symbols and Embedding Suggestion
Byron Ellis