Esempio n. 1
0
 public CallbackHolder(DotNetCallback toWrap, NativeNodeHost parent)
 {
     Wrapped     = toWrap;
     _wrapper    = OnCalled;
     CallbackPtr = Marshal.GetFunctionPointerForDelegate(_wrapper);
     _parent     = parent;
 }
Esempio n. 2
0
 public TaskHolder(Task task, NativeNodeHost parent)
 {
     _task       = task;
     _wrapper    = OnCalled;
     CallbackPtr = Marshal.GetFunctionPointerForDelegate(_wrapper);
     _parent     = parent;
 }
        internal static void RunHostedApplication(IntPtr context,
                                                  IntPtr nativeMethodsPtr,
                                                  int argc,
                                                  [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 2)]
                                                  string[] argv,
                                                  IntPtr resultValue)
        {
            // Console.WriteLine($"NativeEntryPoint c :: asm path {_assembly_path}");
            // Console.WriteLine("NativeEntryPoint Waiting for debugger!");
            // while(!System.Diagnostics.Debugger.IsAttached) System.Threading.Thread.Sleep(50);
            // System.Diagnostics.Debugger.Launch();
            // Console.WriteLine("NativeEntryPoint Debugger awaited!");


            // Switch to default ALC
            // var myAlc = AssemblyLoadContext.GetLoadContext(typeof(NativeEntryPoint).Assembly);
            // if (myAlc != AssemblyLoadContext.Default)
            // {
            //     var inCtx = AssemblyLoadContext.Default.LoadFromAssemblyName(typeof(NativeEntryPoint).Assembly.GetName());
            //     var tInCtx = inCtx.GetType(typeof(NativeEntryPoint).FullName);
            //     tInCtx.GetMethod(nameof(RunHostedApplication), BindingFlags.Static | BindingFlags.NonPublic)
            //           .Invoke(null, new object[] { context, nativeMethodsPtr, argc, argv, resultValue });
            //     return;
            // }

            var nativeMethods = Marshal.PtrToStructure <NativeApi>(nativeMethodsPtr);

            _host             = new NativeNodeHost(context, nativeMethods);
            NodeHost.Instance = new NodeBridge(_host);

            try
            {
                _assembly_path = argv[0];
                var assembly = Assembly.Load(Path.GetFileNameWithoutExtension(_assembly_path));

                var entryPoint = assembly.EntryPoint;
                if (entryPoint.IsSpecialName && entryPoint.Name.StartsWith("<") && entryPoint.Name.EndsWith(">"))
                {
                    entryPoint = entryPoint.DeclaringType.GetMethod(entryPoint.Name.Substring(1, entryPoint.Name.Length - 2),
                                                                    BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                }

                // Console.WriteLine($"NativeEntryPoint c :: asm path {_assembly_path} entrypoint name {entryPoint.Name}");
                // Console.WriteLine("NativeEntryPoint Waiting for debugger!");
                // while(!System.Diagnostics.Debugger.IsAttached) System.Threading.Thread.Sleep(50);
                // System.Diagnostics.Debugger.Launch();
                // Console.WriteLine("NativeEntryPoint Debugger awaited!");

                var result = _host.Scheduler
                             .RunCallbackSynchronously(s =>
                                                       entryPoint.Invoke(null,
                                                                         new object[] { argv.Skip(1).ToArray() }),
                                                       null);

                Marshal.StructureToPtr(DotNetValue.FromObject(result, _host), resultValue, false);
            }
            catch (TargetInvocationException tie)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(tie.InnerException, _host), resultValue, false);
            }
            catch (Exception e)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(e, _host), resultValue, false);
            }
        }