winim icon indicating copy to clipboard operation
winim copied to clipboard

CLR event handlers

Open daddycocoaman opened this issue 1 year ago • 1 comments

Hi,

Thank you so much for creating a great library. Do you have an example of how to use the add_* and remove_* functions here? https://github.com/khchen/winim/blob/3abd0ae122cd41efd0ce6d45fea720ef7091e4ac/winim/inc/mscoree.nim#L1240

My scenario is that I'm trying to load a bunch of DLLs and the DLLs that have dependencies on other DLLs fail to return types when calling GetTypes(). My assumption is that the DLLs have to be loaded or resolved in a certain order so that there's no dependency failure. In pythonnet, I can do something like this:

def assembly_resolve(app_domain, resolve_event_args):
    name = resolve_event_args.Name.split(",")[0]
    try:
        return next(x for x in app_domain.GetAssemblies() if x.GetName().Name == name)
    except StopIteration:
        pass
        
System.AppDomain.CurrentDomain.AssemblyResolve += assembly_resolve

I'm trying to accomplish the same with winim but unsuccessful.

proc resolveAssembly(domain: CLRVariant,
    resolve_event_args: CLRVariant): CLRVariant =
  echo "RESOLVING FROM: " & $domain
  echo "RESOLVING: " & $resolve_event_args.Name

  for assembly in domain.GetAssemblies():
    if assembly.Name == resolve_event_args.Name:
      return assembly

proc setupClr(): CLRVariant =
  var appDomain = clrStart()

  # Add the assembly resolver
  appDomain.CurrentDomain.add_AssemblyResolve(cast[pointer](resolveAssembly))
  load("mscorlib") # Expect resolveAssembly to be called here
  return appDomain.CurrentDomain

Thanks for any help you can provide.

daddycocoaman avatar Feb 02 '23 06:02 daddycocoaman