class NativeMethods(object): __metaclass__ = clrtype.ClrClass DllImport = clrtype.attribute(DllImportAttribute) PreserveSig = clrtype.attribute(PreserveSigAttribute) @staticmethod @DllImport("kernel32.dll") @PreserveSig() @clrtype.accepts(System.IntPtr, System.UInt64, System.UInt64, System.UInt64) @clrtype.returns(System.IntPtr) def VirtualAlloc(lpStartAddr, size, flAllocationType, flProtect): raise NotImplementedError("No Virtual Alloc?") @staticmethod @DllImport("kernel32.dll") @PreserveSig() @clrtype.accepts(System.IntPtr, System.UInt32, System.UInt32, System.IntPtr) @clrtype.returns(System.Boolean) def VirtualProtect(lpAddr, dwSize, newProtect, oldProtect): raise NotImplementedError("No Virtual Protect?") @staticmethod @DllImport("kernel32.dll") @PreserveSig() @clrtype.accepts(System.IntPtr, System.IntPtr) @clrtype.returns(System.IntPtr) def GetProcAddress(hModule, procName): raise NotImplementedError("No ProcAddr?") @staticmethod @DllImport("kernel32.dll") @PreserveSig() @clrtype.accepts(System.String) @clrtype.returns(System.IntPtr) def LoadLibrary(lpFileName): raise NotImplementedError("No LoadLibrary?") @staticmethod @DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=False) @PreserveSig() @clrtype.accepts(System.IntPtr, System.IntPtr, System.Int32) @clrtype.returns() def RtlMoveMemory(lpFileName): raise NotImplementedError("No LoadLibrary?") @staticmethod @DllImport("kernel32.dll") @PreserveSig() @clrtype.accepts() @clrtype.returns(System.UInt64) def GetLastError(): raise NotImplementedError("No GetLastError?")
class NativeMethods(object): __metaclass__ = clrtype.ClrClass DllImport = clrtype.attribute(DllImportAttribute) PreserveSig = clrtype.attribute(PreserveSigAttribute) @staticmethod @DllImport("kernel32.dll") @PreserveSig() @clrtype.accepts() @clrtype.returns() def FreeConsole(): raise NotImplementedError("No Free Console?")
class Product(IProduct, metaclass=clrtype.ClrClass): __metaclass__ = clrtype.ClrClass # https://github.com/IronLanguages/ironpython3/issues/836 _clrnamespace = "IronPython.Samples.ClrType" _clrfields = {"name": str, "cost": float, "_quantity": int} CLSCompliant = clrtype.attribute(System.CLSCompliantAttribute) clr.AddReference("System.Xml") XmlRoot = clrtype.attribute(System.Xml.Serialization.XmlRootAttribute) _clrclassattribs = [ # Use System.Attribute subtype directly for custom attributes without arguments System.ObsoleteAttribute, # Use clrtype.attribute for custom attributes with arguments (either positional, named, or both) CLSCompliant(False), XmlRoot("product", Namespace="www.contoso.com") ] def __init__(self, name, cost, quantity): self.name = name self.cost = cost self._quantity = quantity # IProduct methods def Name(self): return self.name def Cost(self): return self.cost def IsAvailable(self): return self.quantity != 0 @property @clrtype.accepts() @clrtype.returns(int) def quantity(self): return self._quantity @quantity.setter @clrtype.accepts(int) @clrtype.returns() def quantity(self, value): self._quantity = value @clrtype.accepts(float) @clrtype.returns(float) def calc_total(self, discount=0.0): return (self.cost - discount) * self.quantity
class NativeMethods(object): # Note that you could also the "ctypes" modules instead of pinvoke declarations __metaclass__ = clrtype.ClrClass from System.Runtime.InteropServices import DllImportAttribute, PreserveSigAttribute DllImport = clrtype.attribute(DllImportAttribute) PreserveSig = clrtype.attribute(PreserveSigAttribute) @staticmethod @DllImport("user32.dll") @PreserveSig() @clrtype.accepts(System.Char) @clrtype.returns(System.Boolean) def IsCharAlpha(c): raise RuntimeError("this should not get called") @staticmethod @DllImport("user32.dll") @PreserveSig() @clrtype.accepts(System.IntPtr, System.String, System.String, System.UInt32) @clrtype.returns(System.Int32) def MessageBox(hwnd, text, caption, type): raise RuntimeError("this should not get called")
class Win32(object): __metaclass__ = clrtype.ClrClass from System.Runtime.InteropServices import DllImportAttribute DllImport = clrtype.attribute(DllImportAttribute) @staticmethod @DllImport("user32.dll") @clrtype.accepts(System.IntPtr, System.String, System.String, System.UInt32) @clrtype.returns(System.Int32) def MessageBox(hwnd, text, caption, type): raise RuntimeError("Runtime Error")
class Product(IProduct): DataContract = clrtype.attribute(DataContractAttribute) __metaclass__ = clrtype.ClrClass _clrnamespace = "VBTools" _clrfields = { "name": str, "cost": float, "_quantity": int, "extensionData_Value": ExtensionDataObject } _field_attributes = {"cost": [DataMemberAttribute]} _clrclassattribs = [DataContract()] def __init__(self, name, cost, quantity): self.name = name self.cost = cost self._quantity = quantity # IProduct methods def Name(self): return self.name def Cost(self): return self.cost def IsAvailable(self): return self.quantity != 0 @property @clrtype.accepts() @clrtype.returns(int) def quantity(self): return self._quantity @quantity.setter @clrtype.accepts(int) @clrtype.returns() def quantity(self, value): self._quantity = value @clrtype.accepts(float) @clrtype.returns(float) def calc_total(self, discount=0.0): return (self.cost - discount) * self.quantity #ExtensionData methods @property @clrtype.accepts() @clrtype.returns(ExtensionDataObject) def ExtensionData(self): return self.extensionData_value @ExtensionData.setter @clrtype.accepts(ExtensionDataObject) @clrtype.returns() def ExtensionData(self, value): self.extensionData_value = value def Serialize(self): ms = MemoryStream() dcs = DataContractSerializer(self.GetType()) return dcs dcs.WriteObject(ms, self) return ms
clr.AddReference("Peach.Core") clr.AddReference("Peach.Pro") import System import Peach.Core from Peach.Core import Variant from Peach.Core.Agent import IterationStartingArgs, MonitorData from Peach.Core.Agent.MonitorData import Info from Peach.Pro.Core.Agent.Monitors import BasePythonMonitor import mscer2 import time # Create wrappers for class attributes we will use MonitorAttr = clrtype.attribute(Peach.Core.Agent.MonitorAttribute) DescriptionAttr = clrtype.attribute(System.ComponentModel.DescriptionAttribute) ParameterAttr = clrtype.attribute(Peach.Core.ParameterAttribute) class MSCER2Monitor(BasePythonMonitor): __metaclass__ = clrtype.ClrClass _clrnamespace = "PythonExamples" _clrclassattribs = [ MonitorAttr("MSCER2Monitor"), DescriptionAttr("MSCER2 Monitor"), ] @clrtype.accepts(clr.GetClrType(str)) @clrtype.returns()