//
archives

SysAllocString

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

Defining a VARIANT Structure in Managed Code Part 1

1. Introduction. 1.1 Not content with the 3 part “Using VARIANTs in Managed Code” write-up, I decided to start a new series of articles that expound further on the VARIANT structure. 1.2 This time, the goal is to study how we can represent a VARIANT structure in managed code. Along with this, various techniques for working with … 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

Using BSTR in Managed Code Part 2

1. Introduction. 1.1 In part 1 of this series of blogs, I demonstrated how to use BSTRs in managed code complete with example codes that focuses on passing BSTRs from managed code to unmanaged code. 1.2 In this part 2, I shall provide example codes that demonstrate the receiving of BSTRs from unmanaged code. 1.3 … Continue reading

Using BSTR in Managed Code Part 1

1. Introduction. 1.1 The COM BSTR may be used within managed code for various purposes including the exchanging of strings to and from unmanaged code. 1.2 There are several ways to work with BSTRs and these are listed in summary below : Using Marshal Class methods (e.g. Marshal.PtrToStringBSTR(), Marshal.StringToBSTR(), Marshal.FreeBSTR()). Using Unmanaged APIs (e.g. SysAllocString(), SysFreeString(), etc). … Continue reading

Returning Strings from a C++ API to C#

1. Introduction. 1.1 APIs that return strings are very common. However, the internal nature of such APIs, as well as the use of such APIs in managed code, require special attention. This blog will demonstrate both concerns. 1.2 I will present several techniques for returning an unmanaged string to managed code. But before that I shall first provide an … Continue reading