//
archives

Marshal.FreeCoTaskMem

This tag is associated with 12 posts

Example Custom Marshaler – The Array Marshaler

1. Introduction. 1.1 A very useful .NET feature is the marshaling of managed arrays to unmanaged functions by using the MarshalAsAttribute with the MarshalAsAttribute.Value set to UnmanagedType.LPArray in conjunction with using the SizeParamIndex field. 1.2 The array may even be marshaled in both directions, i.e. to unmanaged code and then back to managed with data … Continue reading

Understanding Custom Marshaling Part 3

1. Introduction. 1.1 This is the 3rd installment of our series of articles expounding .NET custom marshaling. 1.2 In part 2, we studied how custom marshaling may be performed from unmanaged code to managed. The idea being that the unmanaged code must return an object to managed code. 1.3 We also noted in part 2 … Continue reading

Understanding Custom Marshaling Part 2

1. Introduction. 1.1 This article is a continuation of Understanding Custom Marshaling Part 1. 1.2 In part 1, we learned how to code custom marshaling for the purpose of passing an object (a managed string) from managed code to unmanaged. 1.3 Here in part 2, we study how custom marshaling is done in the other … Continue reading

How to Implement Pointer To Pointer in C# Without Using Unsafe Code.

1. Introduction. 1.1 In C#, pointer management has traditionally been done using the IntPtr type. 1.2 There is a large number of Marshal class methods that work with IntPtrs and they are very useful indeed. 1.3 However, IntPtr dereferencing (i.e. the referencing of data pointed to by a pointer which is itself pointed to by … Continue reading

Using Structured Exception Handling to Handle Exceptions from C++ in C#.

1. Introduction. 1.1 At some point, every developer who has ever worked with a mix of managed and unmanaged code would wonder whether it is possible to handle exceptions thrown from unmanaged code in managed code. 1.2 In this blog, I shall expound on one way this can be achieved. The unmanaged code that I shall use to … Continue reading

Passing Managed Structures With Strings To Unmanaged Code Part 3

1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (which contains strings) to unmanaged code. The structure was passed as an “in” (by-value) parameter, i.e. the structure was passed to the unmanaged code as a read-only parameter. 1.2 Then in part 2, we studied the techniques for receiving … Continue reading

Passing Managed Structures With Strings To Unmanaged Code Part 2

1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (which contains strings) to unmanaged code. The structure was passed as an “in” (by-value) parameter, i.e. the structure was passed to the unmanaged code as a read-only parameter. 1.2 Here in part 2, we shall explore the … Continue reading

Passing a Structure which Emdeds another Structure from C# to C++.

1. Introduction. 1.1 It is not uncommon to see structures embedding other structures. 1.2 In managed code, such structures are also defineable. 1.3 This blog examines how such complex structures may be passed to an unmanaged API via standard interop marshaling. 2. Test Structures. 2.1 Let’s define 2 structures that we can use for demonstrative purposes : [StructLayout(LayoutKind.Sequential, … Continue reading

Returning an Array of Strings from C++ to C# Part 2

1. Introduction. 1.1 In part 1 of this series of blogs, I have provided a rigorous low-level technique for transferring an array of C-style strings from an unmanaged C++ API to a managed C# client application. 1.2 Here in part 2, a relatively simpler method (from the point of view of the C# client application) for … Continue reading

Returning an Array of Strings from C++ to C# Part 1

1. Introduction. 1.1 I’ve previously written a blog entry expounding on ways to return a C-style string from C++ unmanaged code to C#. 1.2 Besides those that return a single string, APIs that return an array of strings are also never short in demand. 1.3 In this 2-part series of blogs I shall be discussing ways … Continue reading