Importing function pointers without a DLL

Started by grable, June 15, 2005, 06:40:27 PM

Previous topic - Next topic

grable

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. =)
grable

HPW

#1

(importfn functionName functionAddress {callingConvention})


Can you give examples where such a command is usefull?

Sounds interesting.



Lutz,



will you add it to the distribution?
Hans-Peter

grable

#2
Im using the recompiled newlisp.dll from http://www.blitzbasic.com">blitzmax, 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 =)
grable

HPW

#3
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.
Hans-Peter

grable

#4
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.
grable

Lutz

#5
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

grable

#6
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 =)
grable