Ejemplo n.º 1
0
        public static NDLLFunction Load(String lib, String name, int numArgs)
        {
            if (numArgs < -1 || numArgs > 5)
            {
                throw new ArgumentOutOfRangeException("Invalid numArgs: " + numArgs);
            }

            IntPtr module = IntPtr.Zero;

            try
            {
                if (LibraryDir != null && (lib.StartsWith("./") || lib.StartsWith(".\\")))
                {
                    lib = LibraryDir + Path.DirectorySeparatorChar + LibraryPrefix + lib.Substring(2) + LibrarySuffix;
                }
                else
                {
                    lib = lib + LibrarySuffix;
                }
                module = NativeMethods.LoadLibraryWrap(lib);
                if (module == IntPtr.Zero)
                {
                    return(null);
                }

                String funcName;
                if (numArgs != -1)
                {
                    funcName = String.Format("{0}__{1}", name, numArgs);
                }
                else
                {
                    funcName = String.Format("{0}__MULT", name);
                }

                IntPtr funcPtr = NativeMethods.GetProcAddressWrap(module, funcName);
                if (funcPtr == IntPtr.Zero)
                {
                    return(null);
                }
                NDLLFunctionDelegate func  = (NDLLFunctionDelegate)Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(NDLLFunctionDelegate));
                Delegate             cfunc = null;
                switch (numArgs)
                {
                case -1:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(CallMultDelegate));
                    break;

                case 0:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call0Delegate));
                    break;

                case 1:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call1Delegate));
                    break;

                case 2:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call2Delegate));
                    break;

                case 3:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call3Delegate));
                    break;

                case 4:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call4Delegate));
                    break;

                case 5:
                    cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call5Delegate));
                    break;
                }

                IntPtr dll_hx_set_loader_ptr = NativeMethods.GetProcAddressWrap(module, "hx_set_loader");
                if (dll_hx_set_loader_ptr == IntPtr.Zero)
                {
                    return(null);
                }
                HxSetLoaderDelegate dll_hx_set_loader = (HxSetLoaderDelegate)Marshal.GetDelegateForFunctionPointer(dll_hx_set_loader_ptr, typeof(HxSetLoaderDelegate));
                IntPtr callbackPtr;
                if (loaderDelegate == null)
                {
                    loaderDelegate   = new CFFICSLoader.CFFILoaderDelegate(CFFICSLoader.Load);
                    callbackPtr      = Marshal.GetFunctionPointerForDelegate(loaderDelegate);
                    pinnedLoaderFunc = GCHandle.Alloc(callbackPtr, GCHandleType.Pinned);
                }
                else
                {
                    callbackPtr = (IntPtr)pinnedLoaderFunc.Target;
                }

                dll_hx_set_loader(callbackPtr);

                NDLLFunction ndllFunc = new NDLLFunction(module, cfunc, numArgs);
                module = IntPtr.Zero;
                return(ndllFunc);
            }
            finally
            {
                if (module != IntPtr.Zero)
                {
                    NativeMethods.FreeLibraryWrap(module);
                }
            }
        }
Ejemplo n.º 2
0
        public static NDLLFunction Load(String lib, String name, int numArgs)
        {
            if (numArgs < -1 || numArgs > 5)
                throw new ArgumentOutOfRangeException("Invalid numArgs: " + numArgs);

            IntPtr module = IntPtr.Zero;
            try
            {
                if (LibraryDir != null && (lib.StartsWith("./") || lib.StartsWith(".\\")))
                    lib = LibraryDir + Path.DirectorySeparatorChar + LibraryPrefix + lib.Substring(2) + LibrarySuffix;
                else
                    lib = lib + LibrarySuffix;
                module = NativeMethods.LoadLibraryWrap(lib);
                if (module == IntPtr.Zero)
                    return null;

                String funcName;
                if (numArgs != -1)
                    funcName = String.Format("{0}__{1}", name, numArgs);
                else
                    funcName = String.Format("{0}__MULT", name);

                IntPtr funcPtr = NativeMethods.GetProcAddressWrap(module, funcName);
                if (funcPtr == IntPtr.Zero)
                    return null;
                NDLLFunctionDelegate func = (NDLLFunctionDelegate)Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(NDLLFunctionDelegate));
                Delegate cfunc = null;
                switch (numArgs)
                {
                    case -1:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(CallMultDelegate));
                        break;
                    case 0:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call0Delegate));
                        break;
                    case 1:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call1Delegate));
                        break;
                    case 2:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call2Delegate));
                        break;
                    case 3:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call3Delegate));
                        break;
                    case 4:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call4Delegate));
                        break;
                    case 5:
                        cfunc = Marshal.GetDelegateForFunctionPointer(func(), typeof(Call5Delegate));
                        break;
                }

                IntPtr dll_hx_set_loader_ptr = NativeMethods.GetProcAddressWrap(module, "hx_set_loader");
                if (dll_hx_set_loader_ptr == IntPtr.Zero)
                    return null;
                HxSetLoaderDelegate dll_hx_set_loader = (HxSetLoaderDelegate)Marshal.GetDelegateForFunctionPointer(dll_hx_set_loader_ptr, typeof(HxSetLoaderDelegate));
                IntPtr callbackPtr;
                if (loaderDelegate == null)
                {
                    loaderDelegate = new CFFICSLoader.CFFILoaderDelegate(CFFICSLoader.Load);
                    callbackPtr = Marshal.GetFunctionPointerForDelegate(loaderDelegate);
                    pinnedLoaderFunc = GCHandle.Alloc(callbackPtr, GCHandleType.Pinned);
                }
                else
                {
                    callbackPtr = (IntPtr)pinnedLoaderFunc.Target;
                }

                dll_hx_set_loader(callbackPtr);

                NDLLFunction ndllFunc = new NDLLFunction(module, cfunc, numArgs);
                module = IntPtr.Zero;
                return ndllFunc;
            }
            finally
            {
                if (module != IntPtr.Zero)
                    NativeMethods.FreeLibraryWrap(module);
            }
        }