//
archives

Archive for

Interop Assemblies – Some General Advise on Usage.

1. Introduction. 1.1 An interop assembly is a special .NET assembly which contains type information on imported COM types. 1.2 Unlike a typical assembly, it contains mostly metadata. This metadata enables managed code compilers to resolve COM object property access and method calls in code. 1.3 At runtime, the metadata of an interop assembly enables the CLR … Continue reading

Perform Custom Action during Registration of a Managed COM Server.

1. Introduction. 1.1 When a COM-visible managed class library is COM registered via regasm.exe, it is possible to perform various custom activities, e.g. adding additional registry keys/values. 1.2 In fact, just about anything can be performed, e.g. reading/writing data to a file, launch external processes, etc. 1.3 Then, if the class library is unregistered, reverse activities … Continue reading

Creating a COM Server Using C#.

1. Introduction. 1.1 COM remains a popular technology today and, with tremendous support from managed language compilers, e.g Visual C#, building COM servers in managed code is a viable option. 1.2 This blog is intended for the C# developer who wants to develop COM servers using C#, either to provide managed implementations for some COM-based framework or simply to … 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

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

Passing a Pointer to a Structure from C# to C++ Part 2.

1. Introduction. 1.1 In Passing a Pointer to a Structure from C# to C++ Part 1 I demonstrated the basic principles of marshaling a pointer to a structure from managed code (C#) to unmanaged code (C++). 1.2 In part 1, I used a simple managed structure with a single Int32 type member. The idea is to … Continue reading

Passing a Pointer to a Structure from C# to C++ Part 1.

1. Introduction. 1.1 Unmanaged APIs, especially those written in C++, sometimes require pointers to structures to be passed as parameters. 1.2 This may be a breeze for C++ developers but for C# programmers, careful attention must be paid to the contents of the structures. 1.3 This is due mainly to the fact that a managed structure needs … Continue reading

Passing a Structure which Emdeds another Structure from C# to C++.

1. Introduction. 1.1 It is not uncommon to see structures embedding other structures. 1.2 In managed code, such structures are also defineable. 1.3 This blog examines how such complex structures may be passed to an unmanaged API via standard interop marshaling. 2. Test Structures. 2.1 Let’s define 2 structures that we can use for demonstrative purposes : [StructLayout(LayoutKind.Sequential, … Continue reading

The Meaning of Marshal.SizeOf().

1. Introduction. 1.1 Recently, someone from the MSDN forum complained of a problem he faced while passing a structure from managed code to an unmanged API. The following is a link to the forum post : memory corruption pinvoke. 1.2 My advise to him was that, along with other possible problems, there was something wrong with the way … Continue reading