newLISP Fan Club

Forum => newLISP and the O.S. => Topic started by: grable on June 15, 2005, 06:40:27 PM

Title: Importing function pointers without a DLL
Post by: grable on June 15, 2005, 06:40:27 PM
I didnt find any other way to share this but over this forum, so here goes.



this should be builtin in my opinion, for easier use of newlisp.dll



i just stripped down the p_importLib() function and registered it as
(importfn functionName functionAddress {callingConvention})

{} = optional, win32 only



CELL * p_importFunc(CELL * params)
{
char * funcName;
UINT funcPtr;
#ifdef WINCC
char * options = NULL;
#endif
CELL * pCell;
SYMBOL * symbol;

params = getString(params, &funcName);
params = getInteger(params, &funcPtr);

#ifdef WINCC
if(params != nilCell)
getString(params, &options);

if(options != NULL && strcmp(options, "cdecl") ==  0)
pCell = getCell(CELL_IMPORT_CDECL);
else
pCell = getCell(CELL_IMPORT_DLL);
#else
pCell = getCell(CELL_IMPORT_CDECL);
#endif

symbol = translateCreateSymbol(funcName, pCell->type, currentContext, TRUE);
if(isProtected(symbol->flags))
return(errorProcExt2(ERR_SYMBOL_PROTECTED, stuffSymbol(symbol)));

deleteList((CELL *)symbol->contents);
symbol->contents = (UINT)pCell;
pCell->contents = (UINT)funcPtr;
pCell->aux = (UINT)symbol->name;

if(pCell->contents == 0)
return(errorProcExt2(ERR_IMPORT_FUNC_NOT_FOUND, stuffString(funcName)));

return(copyCell(pCell));
}


Works quite nicely. many thanks to the author of newlisp for making it so easy to add stuff. =)
Title:
Post by: HPW on June 15, 2005, 10:19:26 PM

(importfn functionName functionAddress {callingConvention})


Can you give examples where such a command is usefull?

Sounds interesting.



Lutz,



will you add it to the distribution?
Title: hehe.. sure
Post by: grable on June 16, 2005, 04:40:52 AM
Im using the recompiled newlisp.dll from blitzmax (//http), a basic like language.



' function to import
Function MySuperPrint( s:Byte Ptr)
   Print String.FromCString( s)
EndFunction

' import function
newlispEvalStr( "(importfn {mysuperprint} " + Int(MySuperPrint) + ")")

' call function
newlispEcalStr( "(mysuperprint {Hello World})")


it works the same in any other language, like vb,c,pascal etc.

and it saves me from creating a seperate dll with support functions.

quite handy for using newlisp as an embedded scripting language =)
Title:
Post by: HPW on June 16, 2005, 05:07:40 AM
Very interesting,



I own a Blitz3D license and was waiting that Blitzmax get the same powerfull features in 3D as it's DirectX brother. All what I read on the forums there about Blitzmax and your news here will raise my interest in the new member of the blitz family.



I hope Lutz will add this feature to the distribution.
Title: hehe..
Post by: grable on June 16, 2005, 05:27:41 AM
BlitzMax is great.. and its link with mingw (c/c++/asm) is very nice =)



Even if lutz doesnt add it, its not that hard to add yourself,

the source of newLISP is very clean and readable.
Title:
Post by: Lutz on June 16, 2005, 05:38:01 AM
This can already be done using exisiting newLISP functions. Earlier this month I posted a method how to execute binary content embedded in a newLISP program (sorry can't find the pointer to this at the moment).



A similar method could be used to import a function pointer:



; get a template cell from any built-in function

(set 'foo print)

; change to a library function use 265  'stdcall', or 264 for 'cdecl'

(cpymem (pack "ld" 265) (first (dump foo)) 4)

; copy the function address

(cpymem (pack "ld" func-address) (+ (first (dump foo)) 12) 4)


Grable will immedeately understand what we are doing here, the newLISP code pretty much replicates his C-code. In the second step use 265 for standard Win32 calling conventions and use 264 for 'cdecl' 'C'-calling conventions.



After the 3 statements do a (dump MySuperPrint) to make sure type value and function address are in their places



Lutz



ps: welcome to the group garble
Title:
Post by: grable on June 16, 2005, 06:01:25 AM
Thanks lutz .. realy like newLISP .. ive tried many lisp variants, and this one is the cleanest and best documentet IMHO.



hehe.. didnt know that was possible in newlisp itself.. thanks again =)