Subscribe: Devin Jenson's WebLog
Preview: Devin Jenson's WebLog

Devin Jenson's WebLog

That One Guy

Last Build Date: Sat, 13 Aug 2005 04:10:00 +0000


The Danger of Marshal.StructureToPtr

Sat, 13 Aug 2005 04:10:00 +0000

Pop Quiz! Is the following code secure: public static unsafe IntPtr WriteListToNewNativeBuffer( List list) { if (list == null) { throw new ArgumentNullException("list"); } IntPtr nativeBuffer = IntPtr.Zero; Int32 elementSize = Marshal.SizeOf(typeof(T)); try { nativeBuffer = Marshal.AllocCoTaskMem(list.Count * elementSize); Byte* bytePtr = (Byte*)nativeBuffer.ToPointer(); foreach (T element in list) { Marshal.StructureToPtr(element, new IntPtr(bytePtr), false); bytePtr +=...

Dynamically Writing and Executing Native Assembly in C#

Wed, 13 Jul 2005 02:44:00 +0000

Generally, executing native assembly in C# is bad because you lose all the benefits of the managed world. In the cases where it is impossible to perform something in C#, it is better to make a C++ DLL. That said, the new method Marshal.GetDelegateForFunctionPointer() in the .NET Framework v2.0 opens up the possibility for dynamically...

Accessing Dynamic Generic Methods via Interfaces

Mon, 11 Jul 2005 23:34:00 +0000

It is possible to use the Reflection.Emit classes to emit generic methods on a dynamic type. Additionally, dynamic types can implement interfaces making it easy to access it's generic methods (or any of it's published members). Without interfaces, to call a generic method on a dynamic type, a call to MethodInfo.MakeGenericMethod() is required to bind...