//
archives

Marshal.AllocHGlobal

This tag is associated with 9 posts

Example Custom Marshaler – The Long Marshaler

1. Introduction. 1.1 I wish there is a survey on how many developers actually develop custom marshalers. 1.2 It would be interesting to know the type of custom marshalers that people develop. 1.3 In the course of my work, I have developed a few custom marshalers most of which deal with arrays that have to … Continue reading

Using the StringBuilder in Unmanaged API Calls.

1. Introduction. 1.1 I have written extensively about passing strings to and from unmanaged APIs. Most notably Returning Strings from a C++ API to C#. 1.2 The managed type that I have used throughout these articles have been the string. 1.3 When working with unmanaged APIs the DllImport declarations of which take a string as parameter, one of … Continue reading

Using VARIANTs in Managed Code Part 3

1. Introduction. 1.1 In part 1, we discussed how to receive native VARIANTs from unmanaged code. 1.2 In part 2, we explored techniques for passing a native VARIANT from managed code to unmanaged code. 1.3 Here in part 3, we shall study techniques for passing a native VARIANT (created from managed code) to unmanaged code by … Continue reading

Using VARIANTs in Managed Code Part 2

1. Introduction. 1.1 In part 1, we discussed how to receive native VARIANTs from unmanaged code. 1.2 Here in part 2, we will explore techniques for passing a native VARIANT from managed code to unmanaged code. 1.3 Both low- and high-level techniques will be studied. We shall also peer down into the low-level activities of … 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 Pointer to a Structure from C# to C++ Part 3.

1. Introduction. 1.1 In part 2 of this series of blogs, I have demonstrated how to marshal to unmanaged code a pointer to a structure which contains non-blittable field types. 1.2 We have examined how such a structure is to be allocated and deallocated in memory. We have seen in particular, the importance of using the Marshal.DestroyStructure() … Continue reading

Passing a Pointer to a Structure from C# to C++ Part 1.

1. Introduction. 1.1 Unmanaged APIs, especially those written in C++, sometimes require pointers to structures to be passed as parameters. 1.2 This may be a breeze for C++ developers but for C# programmers, careful attention must be paid to the contents of the structures. 1.3 This is due mainly to the fact that a managed structure needs … Continue reading

The Meaning of Marshal.SizeOf().

1. Introduction. 1.1 Recently, someone from the MSDN forum complained of a problem he faced while passing a structure from managed code to an unmanged API. The following is a link to the forum post : memory corruption pinvoke. 1.2 My advise to him was that, along with other possible problems, there was something wrong with the way … Continue reading