LoadBuiltinModule(string name) { IntPtr initFP = Unmanaged.GetProcAddress(this.library, "init" + name); PydInit_Delegate init = (PydInit_Delegate)Marshal.GetDelegateForFunctionPointer(initFP, typeof(PydInit_Delegate)); init(); }
Load(string path) { // the ActCtx stuff allows us to import .pyds which link to msvcr90 but don't have manifests // implementation explained in stub/ic_msvcr90.c IntPtr cookie = Unmanaged._Py_ActivateActCtx(); IntPtr l = Unmanaged.LoadLibrary(path); Unmanaged._Py_DeactivateActCtx(cookie); if (l == IntPtr.Zero) { throw new Exception( String.Format("Could not load library '{0}' . Error code:{1}", path, Unmanaged.GetLastError())); } this.handles.Add(l); string funcName = "init" + Path.GetFileNameWithoutExtension(path); IntPtr funcPtr = Unmanaged.GetProcAddress(l, funcName); if (funcPtr == IntPtr.Zero) { throw new Exception( String.Format("Could not find module init function {0} in dll {1}", funcName, path)); } PydInit_Delegate initmodule = (PydInit_Delegate)Marshal.GetDelegateForFunctionPointer( funcPtr, typeof(PydInit_Delegate)); initmodule(); }
Init(dgt_getfuncptr addressGetter, dgt_registerdata dataSetter) { IntPtr initFP = Unmanaged.GetProcAddress(this.library, "init"); InitDelegate initDgt = (InitDelegate)Marshal.GetDelegateForFunctionPointer(initFP, typeof(InitDelegate)); IntPtr addressGetterFP = Marshal.GetFunctionPointerForDelegate(addressGetter); IntPtr dataSetterFP = Marshal.GetFunctionPointerForDelegate(dataSetter); initDgt(addressGetterFP, dataSetterFP); // yes, these do appear to be necessary: rare NullReferenceExceptions will be thrown // from the initDgt call otherwise. run functionalitytest in a loop to observe. GC.KeepAlive(addressGetter); GC.KeepAlive(dataSetter); }