LoadFunction() public static method

public static LoadFunction ( IntPtr module, IntPtr ordinal ) : IntPtr
module System.IntPtr
ordinal System.IntPtr
return System.IntPtr
Esempio n. 1
0
            // __nonzero__

            /// <summary>
            /// Creates a new CFuncPtr object from a tuple.  The 1st element of the
            /// tuple is the ordinal or function name.  The second is an object with
            /// a _handle property.  The _handle property is the handle of the module
            /// from which the function will be loaded.
            /// </summary>
            public _CFuncPtr(PythonTuple args)
            {
                if (args == null)
                {
                    throw PythonOps.TypeError("expected sequence, got None");
                }
                else if (args.Count != 2)
                {
                    throw PythonOps.TypeError($"argument 1 must be a sequence of length 2, not {args.Count}");
                }

                object nameOrOrdinal = args[0];
                object dll           = args[1];
                IntPtr intPtrHandle  = GetHandleFromObject(dll, "the _handle attribute of the second element must be an integer");

                IntPtr tmpAddr;
                string funcName = args[0] as string;

                if (funcName != null)
                {
                    tmpAddr = NativeFunctions.LoadFunction(intPtrHandle, funcName);
                }
                else
                {
                    tmpAddr = NativeFunctions.LoadFunction(intPtrHandle, new IntPtr((int)nameOrOrdinal));
                }

                if (tmpAddr == IntPtr.Zero)
                {
                    if (CallingConvention == CallingConvention.StdCall && funcName != null)
                    {
                        // apply std call name mangling - prepend a _, append @bytes where
                        // bytes is the number of bytes of the argument list.
                        string mangled = "_" + funcName + "@";

                        for (int i = 0; i < 128 && tmpAddr == IntPtr.Zero; i += 4)
                        {
                            tmpAddr = NativeFunctions.LoadFunction(intPtrHandle, mangled + i);
                        }
                    }

                    if (tmpAddr == IntPtr.Zero)
                    {
                        throw PythonOps.AttributeError($"function {args[0]} is not defined");
                    }
                }

                _memHolder = new MemoryHolder(IntPtr.Size);
                addr       = tmpAddr;
                _id        = Interlocked.Increment(ref _curId);
            }
Esempio n. 2
0
            public SimpleCData in_dll(CodeContext /*!*/ context, object library, string name)
            {
                IntPtr handle = GetHandleFromObject(library, "in_dll expected object with _handle attribute");
                IntPtr addr   = NativeFunctions.LoadFunction(handle, name);

                if (addr == IntPtr.Zero)
                {
                    throw PythonOps.ValueError("{0} not found when attempting to load {1} from dll", name, Name);
                }

                SimpleCData res = (SimpleCData)CreateInstance(context);

                res.SetAddress(addr);
                return(res);
            }