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.

GCHandle Structure

[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. 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!

Type-safe Managed wrappers for kernel32!GetProcAddress - Mike Stall's .NET Debugging Blog

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!! 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.

CLR Inside Out: Marshaling between Managed and Unmanaged Code

The world is not perfect. 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] The first marshaling topic we'll discuss concerns the use of InAttribute and OutAttribute, attribute types located in the System.Runtime.InteropServices namespace. Consider the code in Figure 3. Let's look at the results more closely. Here's the output: Return Values Copying and Pinning. 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.

Mastering C# structs - C

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#