Esempio n. 1
0
    public static Int32 Main(String[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Usage: ClrLibRunner.exe <NativeSharedLibrary> <Args>...");
            return(1);
        }
        String sharedLibrary = args[0];

        // TODO: detect which platform we are on to know how to load a shared library correctly
        bool onLinux = true;

        EntryDelegate entry;

        if (onLinux)
        {
            const uint RTLD_NOW     = 2;
            IntPtr     moduleHandle = LinuxNativeMethods.dlopen(sharedLibrary, RTLD_NOW);
            if (moduleHandle == IntPtr.Zero)
            {
                Console.WriteLine("Error: dlopen '{0}' failed: {1}", sharedLibrary, LinuxNativeMethods.dlerror());
                return(1);
            }
            IntPtr funcPtr = LinuxNativeMethods.dlsym(moduleHandle, EntryName);
            if (funcPtr == IntPtr.Zero)
            {
                Console.WriteLine("Error: dlsym '{0}' failed: {1}", EntryName, LinuxNativeMethods.dlerror());
                return(1);
            }
            entry = (EntryDelegate)Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(EntryDelegate));
        }
        else
        {
            Console.WriteLine("Error: unsupported platform");
            return(1);
        }
        IntPtr createDelegateFuncPtr = Marshal.GetFunctionPointerForDelegate(new CreateDelegateDelegate(CreateDelegate));

        return(entry(createDelegateFuncPtr, args.Length, args));
    }
    public static Int32 Main(String[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Usage: ClrLibRunner.exe <NativeSharedLibrary> <Args>...");
            return(1);
        }
        String sharedLibrary = args[0];

        // TODO: might want to make this a command-line option (--dlopen or --loadlibrary)?
        // TODO: need a way to detect whether or not this mechanism is supported
        //       maybe I should use runtime reflection?
        //bool onWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
        bool onWindows = Directory.Exists("C:\\Windows\\System32");

        EntryDelegate entry;

        if (onWindows)
        {
            IntPtr moduleHandle = WindowsNativeMethods.LoadLibrary(sharedLibrary);
            if (moduleHandle == IntPtr.Zero)
            {
                Console.WriteLine("Error: LoadLibrary '{0}' failed: {1}", sharedLibrary, Marshal.GetLastWin32Error());
                return(1);
            }
            IntPtr funcPtr = WindowsNativeMethods.GetProcAddress(moduleHandle, EntryName);
            if (funcPtr == IntPtr.Zero)
            {
                Int32 errorCode = Marshal.GetLastWin32Error();
                if (errorCode == WindowsNativeMethods.ERROR_PROC_NOT_FOUND)
                {
                    Console.WriteLine("Error: library '{0}' is missing function '{1}'", sharedLibrary, EntryName);
                }
                else
                {
                    Console.WriteLine("Error: GetProcAddress '{0}' failed: {1}", EntryName, errorCode);
                }
                return(1);
            }
            entry = (EntryDelegate)Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(EntryDelegate));
        }
        else
        {
            const uint RTLD_NOW     = 2;
            IntPtr     moduleHandle = LinuxNativeMethods.dlopen(sharedLibrary, RTLD_NOW);
            if (moduleHandle == IntPtr.Zero)
            {
                Console.WriteLine("Error: dlopen '{0}' failed: {1}", sharedLibrary, LinuxNativeMethods.dlerror());
                return(1);
            }
            IntPtr funcPtr = LinuxNativeMethods.dlsym(moduleHandle, EntryName);
            if (funcPtr == IntPtr.Zero)
            {
                Console.WriteLine("Error: dlsym '{0}' failed: {1}", EntryName, LinuxNativeMethods.dlerror());
                return(1);
            }
            entry = (EntryDelegate)Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(EntryDelegate));
        }
        IntPtr createDelegateFuncPtr = Marshal.GetFunctionPointerForDelegate(new CreateDelegateDelegate(CreateDelegate));

        return(entry(createDelegateFuncPtr, args.Length, args));
    }