This tag is associated with 11 posts

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

Passing Managed Structures With Arrays To Unmanaged Code Part 1

1. Introduction 1.1 Managed structures that contain arrays are a common requirement in .NET development. Such structures, however, cannot be conveniently passed to unmanaged code. This is because arrays are referenced types and are not blittable. And because arrays are not blittable, the containing structure is also non-blittable. 1.2 This series of blogs will explore how managed structures … 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

Passing Multi-Dimensional Managed Array To C++ Part 2

1. Introduction. 1.1 This blog forms part 2 of a series of blogs which explores techniques for passing multi-dimensional managed arrays to C++ applications. 1.2 We saw in part 1 how the distinctive feature of the layout of a C/C++ array enabled the seamless access of the data of a multi-dimensional managed array from a C++ program. Cool. 1.3 In … Continue reading

Delegates As Callbacks Part 2

1. Introduction. 1.1 This blog is a continuation of an earlier blog Delegates As Callbacks Part 1. 1.2 In this part 2, I shall demonstrate using managed methods as callbacks that can be invoked from unmanaged code. 2. Sample Code. 2.1 Listed below is a sample unmanaged code written in C++ : typedef void (__stdcall … Continue reading

Delegates as Callbacks Part 1

1. Introduction 1.1 A concept frequently used in real-world software development is that of a callback. 1.2 A callback, as we know, is a function which is asynchronously called, often when some event of significance occurs. 1.3 This series of blogs examines how this concept is realized between managed and unmanaged code. In this part … 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

Returning a C++ Class from an API in C#

1. Introduction. 1.1 Just today someone from the MSDN Common Language Runtime Forum asked a question about writing an API that can be called in C# to return a C++ class. 1.2 First of all, it must be noted that it is not possible to use an unmanaged class (e.g. a C++ class) in managed code (e.g. … Continue reading

Passing a Managed Array to a C++ ActiveX via a Pointer.

1. Introduction. A few days ago, a member from the MSDN Visual C++ Forum posted a question presenting the following problem : 1.1 He has an ActiveX control which contains the following method : [id(1), helpstring(“method CopyToBuffer”)] SHORT CopyToBuffer(SHORT* src); 1.2 The twist is that the forum member wants the method to receive an array of short … Continue reading