-
Working on a project where I am reflectively loading the IronPython engine and everything is working except if I try to do anything with ClrType. I use it for things like p/invoke like this example: import clr
clr.AddReference("System")
clr.AddReference("System.Core") # Add this reference for LINQ support
import System
from System import IntPtr, Func, UInt64
from System.Runtime.InteropServices import DllImportAttribute, PreserveSigAttribute
import clrtype
clr.AddReference("Microsoft.Dynamic")
from Microsoft.Scripting.Generation import Snippets
#this defines a none generic delegate as the marshaller does not accept generic delegates such as Func
EnumWindowsProc = Snippets.Shared.DefineDelegate("MyDelegate", bool, IntPtr, IntPtr)
class NativeMethods(object, metaclass=clrtype.ClrClass):
__metaclass__ = clrtype.ClrClass # https://github.com/IronLanguages/ironpython3/issues/836
DllImport = clrtype.attribute(DllImportAttribute)
PreserveSig = clrtype.attribute(PreserveSigAttribute)
@staticmethod
@DllImport("user32.dll", SetLastError=True)
@PreserveSig()
@clrtype.accepts(EnumWindowsProc, IntPtr)
@clrtype.returns(UInt64)
def EnumWindows(lpEnumFunc, lParam):raise NotImplementedError("No EnumWindow")
def outputwindow(hwnd, lParam):
print("callback called", hwnd)
return True
# Create an instance of the delegate using the callback function
delegate_instance = EnumWindowsProc(outputwindow)
# Call the EnumWindows function with the delegate instance as the callback
NativeMethods.EnumWindows(delegate_instance, IntPtr.Zero)
print("testing complete") If I use ctypes instead it works fine. I just can't access clrtypes for some reason |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Just make sure to include its folder in the search paths so it's picked up by import. |
Beta Was this translation helpful? Give feedback.
clrtype.py
is packaged IronPython's version of with the standard library. So you can grab it from the lib folder where you installed IronPython, fro the IronPython.StdLib NuGet package or directly from git clrtype.py.Just make sure to include its folder in the search paths so it's picked up by import.