//
archives

Archive for

Interoping COM Structures.

1. Introduction. 1.1 COM structures, or User-Defined Types (UDTs) are very useful constructs. Their interoperability in managed code, however, is not perfect and there are situations in which their use is not possible. 1.2 This blog examines various scenarios in which UDTs are used between COM and managed code (specifically C#). We will also analyze the success … Continue reading

DynamicMethod as a Viable Alternative to Type.MemberInvoke() and MethodInfo.Invoke()

1. Introduction. 1.1 When using Type.InvokeMember() or MethodInfo.Invoke() to call a method that takes a reference parameter, the referenced object in the arguments array will be updated by the method. However, the arguments array items cannot be used to hold references to existing objects. 1.2 Hence after the method call, the arguments array items will be updated with potentially … Continue reading

Structure Member Pack Alignment

1. Introduction. 1.1 A few days ago someone from the Visual C# General MSDN Forum asked a question concerning the importance of structure member pack alignment. He was developing a C# application that calls a C++ API. A structure is passed between the 2 sets of codes. He wondered whether it was really essential to … Continue reading

SafeArrayGetElement() API and Clearing SAFEARRAY UDT Elements.

1. When we use SafeArrayGetElement() to retrieve an element from a SAFEARRAY, special attention must be afforded to situations where the element is a User-Defined Type (UDT) (i.e. a structure in common programming terms). 2. More specifically, a UDT which contains member fields which are pointers to allocated memory (e.g. BSTRs). 3. Just before calling … Continue reading

Marshaling a SAFEARRAY of Managed Structures by COM Interop Part 3

1. Introduction. 1.1 In this final part of the series of blogs, we will look at the most interesting aspect of marshaling a SAFEARRAY of managed structures by COM interop : passing a SAFEARRAY of UDTs as an “in” and “out” (reference) parameter. 1.2 A SAFEARRAY of UDTs marshaled in this way may be modified by the COM … Continue reading

Marshaling a SAFEARRAY of Managed Structures by COM Interop Part 2

1. Introduction. 1.1 In part 1 of this series of blogs, we studied how to pass a SAFEARRAY of UDTs (which originate from managed code) to an unmanaged client. 1.2 In this part 2, we shall be looking at how to pass a SAFEARRAY of UDTs from an unmanaged COM client application to a managed COM server. 2. … Continue reading

Marshaling a SAFEARRAY of Managed Structures by COM Interop Part 1

1. Introduction. 1.1 There are two basic ways to pass an array of managed structures to unmanaged code. These include : By C-style array. By SAFEARRAY. 1.2 In this series of blogs I shall expound on the use of SAFEARRAYs to pass an array of managed structures to unmanaged code. 1.3 In this part 1, I shall provide background information … Continue reading

Passing Managed Structures With Strings To Unmanaged Code Part 1

1. Introduction. 1.1 Managed structures that contain strings are a common sight. The trouble is that managed strings are non-blittable. This means that they do not have a single common representation in the unmanaged world. However, there are several standardized representations that are recognized by the CLR. 1.2 A managed string is also a referenced type which means … Continue reading

Misunderstanding IDL Parameter Direction Leading to BSTR Memory Leakage.

1. Introduction. 1.1 Just yesterday a Visual C# Forum Member posed an interesting question. The post can be found here : Possible BSTR memory leak 1.2 Basically, he designed a COM class (in IDL) that takes charge of acquiring data from some source. He also defined a callback interface that is to be implemented by clients. An instance … Continue reading

Invoking Finalizers of .NET-based COM Objects in an Unmanaged App Part 1

1. Introduction. 1.1 .NET objects, when exposed as COM objects in an unmanaged application, face a tricky problem : their finalizers are often not invoked throughout the application’s lifetime. 1.2 For more information on finalizers in general and the limitation that we have just mentioned above, please refer to : Object.Finalize Method on MSDN. 1.3 This … Continue reading