//
archives

CoTaskMemAlloc

This tag is associated with 14 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 4

1. Introduction. 1.1 We have reached the 4th part of this series of articles elucidating the basic principles of custom marshaling. 1.2 In the first part, we touched on marshaling one-way from managed to unmanaged code. 1.3 Then in part 2 and 3, we studied marshaling the other way from unmanaged to managed. 1.4 Here … 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

Using The UnknownWrapper Class Part 2.

1. Introduction. 1.1 In part 1 of this series of articles, I demonstrated how to use the UnknownWrapper class to pass an IUnknown interface pointer from a COM-visible managed object to a COM method. 1.2 The COM method specifically takes a VARIANT parameter and the COM-visible managed object has to be contained inside a System.Object instance. … 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

Using VARIANTs in Managed Code Part 1

1. Introduction. 1.1 The VARIANT type is a commonly used structure in unmanaged COM development. 1.2 A VARIANT is meant to be a generic container of a wide variety of COM types (in fact, every type recognized in the sub-set of the COM sub-system known as OLE automation). 1.3 Its counterpart in the managed world is … 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