//
archives

SysFreeString

This tag is associated with 14 posts

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

Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 1.

1. Introduction. 1.1 I have previously written about exchanging SAFEARRAYs of managed structures with unmanaged code via COM interop. 1.2 In this new series of articles, I shall expound on the exchange of such SAFEARRAYs via P/Invoke. 1.3 I have arranged this series of articles into multiple parts. In the first 3 parts, I shall show … 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

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

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

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