fbpx

dynamic dll import (with classmate)

More
8 months 1 day ago #1 by SHirsch
dynamic dll import (with classmate) was created by SHirsch
Hi all,

in VO I loaded a dll dynamically like this:
hDllInst := cmLoadLibrary(sDllFileName)
IF hDllInst == NULL_PTR
   RETURN FALSE
ENDIF
pDllName := cmGetProcAddress(hDllInst,"_DLLNAME")
IF pDllName == NULL_PTR
   RETURN FALSE
ENDIF
....

Now that all my framework, plugin dlls and applications are VOXPorted I try to load the plugin dlls. cmLoadLibrary seems to work since it returns a pointer != NULL. But cmGetProcAddress returns NULL_PTR. How can a load the function pointers? Or is there any other simple approach to load dlls. I have dozens of plugin dlls. So I try to avoid refactoring all of them (for now).

Regards,
Stefan

Please Log in or Create an account to join the conversation.

More
8 months 1 day ago #2 by SHirsch
Replied by SHirsch on topic dynamic dll import (with classmate)
Hi all,

I think I have found a solution:
VAR dll := System.Reflection.Assembly.LoadFile(sDll)
FOREACH t AS System.Type IN dll:GetExportedTypes()
  IF t:ToString():EndsWith("Functions")   //should start with name of dll ( -> check)
    FOREACH m AS System.Reflection.MethodInfo IN t:GetMethods()
      IF m:Name:ToUpper() == "_DLLTYPE"
        VAR n := (INT)m:Invoke(NULL, NULL) 
        ....
      ENDIF
    NEXT
  ENDIF
NEXT

Not the most elegant way but shows an approach.

Regards,
Stefan

Please Log in or Create an account to join the conversation.

More
8 months 1 day ago #3 by Chris
Replied by Chris on topic dynamic dll import (with classmate)
Hi Stefan,

The xxxLoadLibrary() functions can only be used to load standard windows dll files with functions, but in your case, now you are attempting to load .Net assemblies, which have a completely dirferent format and do not even have "real" functions, they only have classes and as you saw x# "functions" are only methods of a special class.

The correct way to use .Net dlls is exactly what you did, to use reflaction, also the VulcanLoadLibrary() runtime function does the same thing more or less. But it is better I think to use your own implementation as you have done it already, so you are in full control.

Chris

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.