示例#1
0
    public static void ValidateLoadUnmanagedDll()
    {
        Console.WriteLine($"Running {nameof(ValidateLoadUnmanagedDll)}...");

        ALC alc = new ALC();
        var asm = alc.LoadFromAssemblyPath(Assembly.GetExecutingAssembly().Location);

        Console.WriteLine(" -- Validate explicit load...");

        // ALC implementation returns a fake handle value
        IntPtr ptr = NativeLibrary.Load(FakeNativeLibrary.Name, asm, null);

        alc.Validate(FakeNativeLibrary.Name);
        Assert.Equal(FakeNativeLibrary.Handle, ptr);

        alc.Reset();
        ptr = IntPtr.Zero;

        bool success = NativeLibrary.TryLoad(FakeNativeLibrary.Name, asm, null, out ptr);

        Assert.True(success, $"NativeLibrary.TryLoad should have succeeded");
        alc.Validate(FakeNativeLibrary.Name);
        Assert.Equal(FakeNativeLibrary.Handle, ptr);

        alc.Reset();

        // ALC implementation calls NativeLibrary.TryLoad with a different name
        ptr = NativeLibrary.Load(FakeNativeLibrary.RedirectName, asm, null);
        alc.Validate(FakeNativeLibrary.RedirectName, FakeNativeLibrary.Name);
        Assert.Equal(FakeNativeLibrary.Handle, ptr);

        alc.Reset();
        ptr = IntPtr.Zero;

        success = NativeLibrary.TryLoad(FakeNativeLibrary.RedirectName, asm, null, out ptr);
        Assert.True(success, $"NativeLibrary.TryLoad should have succeeded");
        alc.Validate(FakeNativeLibrary.RedirectName, FakeNativeLibrary.Name);
        Assert.Equal(FakeNativeLibrary.Handle, ptr);

        alc.Reset();

        Console.WriteLine(" -- Validate p/invoke...");
        int addend1  = rand.Next(int.MaxValue / 2);
        int addend2  = rand.Next(int.MaxValue / 2);
        int expected = addend1 + addend2;

        int value = NativeSumInAssemblyLoadContext(alc, addend1, addend2);

        alc.Validate(NativeLibraryToLoad.InvalidName);
        Assert.Equal(expected, value);
    }
    public static int Main()
    {
        // Events on the Default Load Context

        try
        {
            AssemblyLoadContext.Default.ResolvingUnmanagedDll += HandlerFail;
            NativeSum(10, 10);
        }
        catch (DllNotFoundException e)
        {
            if (HandlerTracker != 0)
            {
                Console.WriteLine("Event Handlers not called as expected");
                return(101);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"Unexpected exception: {e.Message}");
            return(102);
        }

        try
        {
            AssemblyLoadContext.Default.ResolvingUnmanagedDll += HandlerPass;

            if (NativeSum(10, 10) != 20)
            {
                Console.WriteLine("Unexpected ReturnValue from NativeSum()");
                return(103);
            }
            if (HandlerTracker != 0)
            {
                Console.WriteLine("Event Handlers not called as expected");
                return(104);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"Unexpected exception: {e.Message}");
            return(105);
        }

        // Events on a Custom Load Context

        try
        {
            string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string testAsmDir = Path.Combine(currentDir, "..", "TestAsm", "TestAsm");

            ALC alc = new ALC();
            alc.ResolvingUnmanagedDll += HandlerPass;

            var assembly = alc.LoadFromAssemblyPath(Path.Combine(testAsmDir, "TestAsm.dll"));
            var type     = assembly.GetType("TestAsm");
            var method   = type.GetMethod("Sum");
            int value    = (int)method.Invoke(null, new object[] { 10, 10 });

            if (value != 20)
            {
                Console.WriteLine("Unexpected ReturnValue from TestAsm.Sum()");
                return(106);
            }
            if (HandlerTracker != 1)
            {
                Console.WriteLine("Event Handlers not called as expected");
                return(107);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"Unexpected exception: {e.Message}");
            return(108);
        }

        return(100);
    }