//
archives

BSTR

This tag is associated with 12 posts

Custom Marshaling – Comments and an Example Use of the Marshal Cookie

1. Introduction. 1.1 This article and several to follow are extensions to the “Understanding Custom Marshaling” series of articles designed to help readers understand the rudiments of custom marshaling in .NET. 1.2 In this article, I discuss some general observations about the custom marshaling mechanism that I hope the reader had the opportunity to make … Continue reading

How to Store Binary Data in a BSTR.

1. Introduction 1.1 A BSTR is flexible in the sense that not only can you store a character string in it, you can also store binary data. 1.2 This programming tip presents sample code which demonstrates how this can be done. 2. The SysAllocStringByteLen() API. 2.1 Normally, when you want to allocate a BSTR, you … 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

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

The Importance of Proper BSTR Allocation.

1. Introduction. 1.1 Note that code like the following : BSTR bstr = L”My BSTR”; does not allocate a BSTR. 1.2 To allocate a BSTR, you must use the ::SysAllocString() API : BSTR bstr = ::SysAllocString(L”My BSTR”); 2. Explanation. 2.1 We can verify this with a call to the ::SysStringByteLen() API : UINT uiLen = ::SysStringByteLen(bstr); … 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 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

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