background preloader

Marshaling Structs

Facebook Twitter

FreeCoTaskMem Method. Frees a block of memory allocated by the unmanaged COM task memory allocator. public static void FreeCoTaskMem( IntPtr ptr ) Parameters ptr Type: System.IntPtrThe address of the memory to be freed. FreeCoTaskMem exposes the COM CoTaskMemFree function, which frees all bytes so that you can no longer use the memory that the ptr parameter points to. .NET Framework Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client Profile Supported in: 4, 3.5 SP1 Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2.

GCHandle Structure. Provides a way to access a managed object from unmanaged memory. [ComVisibleAttribute(true)] public struct GCHandle The GCHandle type exposes the following members. You can also use GCHandle to create a pinned object that returns a memory address to prevent the garbage collector from moving the object in memory. .NET Framework Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client Profile Supported in: 4, 3.5 SP1 Portable Class Library Supported in: Portable Class Library .NET for Windows Store apps Supported in: Windows 8 .NET for Windows Phone apps Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1 Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported) Any public static (Shared in Visual Basic) members of this type are thread safe.

Func(T, TResult) Delegate. Type-safe Managed wrappers for kernel32!GetProcAddress - Mike Stall's .NET Debugging Blog. Pinvoke is cool in managed code, but sometimes you need to get straight at kernel32! GetProcAddress. For example, maybe you need dynamic control over which unmanaged dll you want to load. But that returns a void*, which is a major no-no in managed code. Here's a helper class I wrote (as part of the growing unmanaged code support in MDbg) that provides a pretty managed veneer over LoadLibrary, GetProcAddress, and FreeLibrary. Sample usage may be: At the end of the day, that's what you'd expect for managed code. Big disclaimer!! 1. 2. Here's the code: Dependency Walker (depends.exe) Home Page. Managed, Native, and COM Interop Team. Managed, Native, and COM Interop Team - Download: PInvoke Interop Assistant.

CLR Inside Out: Marshaling between Managed and Unmanaged Code. CLR Inside Out Marshaling between Managed and Unmanaged Code Yi Zhang and Xiaoying Guo Code download available at:CLRInsideOut2008_01.exe(1269 KB) Let's face it. The world is not perfect. Very few companies are developing entirely in managed code, and on top of that there's a lot of legacy unmanaged code out there you need to work with. How do you integrate your managed and unmanaged projects? Fortunately, Microsoft® .NET Framework interop opens a channel between managed and unmanaged code, and marshaling plays a very important role in that connection in that it allows for data exchange between the two (see Figure 1).

Figure 1 Bridging the Gap between Managed and Unmanaged Code (Click the image for a larger view) Because of the sheer number of these factors, it can be a challenge to marshal correctly, which requires you to understand many details in both unmanaged and managed code. [InAttribute] and [OutAttribute] Consider the code in Figure 3. Let's look at the results more closely. Pinvoke - C# P/Invoke: Marshalling structures containing function pointers. Mastering C# structs - C. This article was originally published in VSJ, which is now part of Developer Fusion. Structs are a fundamental data type in C# and most other modern programming languages.

They are inherently simple, but you might be surprised at how fast things can become more complicated. The problems mostly arise when you have to work with structures created in other languages, either saved on disk or when calling functions in DLLs or COM. In this article I’m going to assume that you know what a struct is, how to define one and the basics of using one.

I’m also going to assume that you have a rough idea of how to call an API function using p/Invoke, and what marshalling is all about. Layout In many situations you can simply declare and use a struct without worrying about how it is implemented – specifically how its fields are laid out in memory. Public struct struct1 { public byte a; // 1 byte public int b; // 4 bytes public short c; // 2 bytes public byte d; // 1 byte } Being exact Calling the API. Marshal C++ struct array into C#