FreeLibrary for newLISP?

Started by HPW, November 20, 2004, 09:52:07 AM

Previous topic - Next topic

HPW

I would need the counterpart of 'import' to relaese the handle to a loaded DLL from newLISP to be able to delete the imported DLL.



Situation: I have unzipped a DLL to the temp-dir and used it from there.

Now I want to clean up, but the system don't let me delete it.



Any idea's?
Hans-Peter

HPW

#1
I think I found a API method:



> (import "kernel32.dll" "GetModuleHandleA")
GetModuleHandleA <77E6DF37>
> (GetModuleHandleA "MyDll.dll")
13434880
> (import "kernel32.dll" "FreeLibrary")
FreeLibrary <77E6C79E>
> (FreeLibrary 13434880)
1
> (FreeLibrary 13434880)
1
> (FreeLibrary 13434880)
0
>


But what about a lisp-command:

> (import "MyDll.dll" "MyCommand")
MyCommand <5C4738>
> (freelib MyCommand)
Hans-Peter

HPW

#2
Here a Lisp-Function:

(define (freelib dllName dllHandle freeret)
  (import "kernel32.dll" "GetModuleHandleA")
  (import "kernel32.dll" "FreeLibrary")
  (setq dllHandle(GetModuleHandleA dllName))
  (setq freeret 1)
  (while (!= freeret 0)
    (setq freeret(FreeLibrary dllHandle))))


So at least should it be native?
Hans-Peter

Lutz

#3
After freeing the library, you could just use 'delete' to eliminate the symbol:



(delete (symbol "mycommand"))

or

(delete (sym "mycommand")) ;; can use shorter name since 8.2.7



All references to 'mycommand' in you code will be replaced with 'nil'.



But why would you do all this at all? The library would be released automatically when newLISP exits and the amount of space 'mycommand' takes is minimal, its only the symbol and one lisp cell holding the procedure address. Importing a library sevarl times wil not take up more space, but only increase its reference count and overwrite the already defined symbol.



Lutz

HPW

#4
Thanks Lutz for the additional info.



>But why would you do all this at all?



I was not after freeing the reference to get more mem-space in newlisp.

I unzip the DLL into the temp-dir and want to free the disk-space.

And I can only delete it when it is no more referenced.
Hans-Peter