Aklımda Kalası Kelimeler

* давайте работать вместе
* Zarf ve Mazruf, Zerafet(xHoyratlık) ile aynı kökten(za-ra-fe) gelir
* Bedesten
* Suç subuta ermiştir - Suç sabit olmuştur
MemManager etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
MemManager etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

1 Mayıs 2011 Pazar

Marshalling ve InteropServices üzerine güzel bir yazı

Referans: www.rupj.net/portfolio/docs/dws-writeup.pdf


3.1 Interop

The Interop software layer, known in the code base as the namespace Unf.Dhcp.Smo.Interop, is the foundation of DHCP Web Services. The DHCP server API defines functions, structures, and enumerations used to manage a DHCP server. Because the DHCP server API is in C and this project was written in C# .NET, this layer was created, as an interoperability interface between the two. This layer can also be referred to as a p/invoke wrapper. This wrapper defines C# prototypes, structures, and enumerations, to mimic its C library counterpart, using a .NET utility namespace called System.Runtime.InteropServices. Classes within this namespace allow .NET managed code to make external calls to C functions, which are exported in Dynamic Link Libraries (DLLs). The namespace contains a Marshal class that facilitates data type conversions to and from both C platform dependent types and .NET Framework types. This process is known as marshalling. The Marshal class also contains functions that allocate unmanaged memory, a normal operation when developing in C. When using p/invoke, developers have to manage memory, as if they were developing in C, since .NET does not provide a garbage collection system for this type of service. This is why a memory management class was created, called MemManager. MemManager manages memory allocations for p/invoke calls, along with the proper release of that memory, after use. In addition, the .NET Marshal class automatically does some marshaling of data types, so developers do not have to worry about memory allocations or type conversions in certain situations. For example, a C char* is automatically converted to a .NET String type, by the Marshal class. The Marshal class does not automatically handle structures, unions, and most pointer types. Instead, it provides helper functions to facilitate the marshaling of these types. Unions are one of the more difficult structures with which to deal, because C# does not have a construct for a union. However, the Marshal class does define special attributes to help mimic a C union, the most important of which is the FieldOffset attribute. This attribute allows a developer to define the byte location where a structure member starts, relative to the explicit size of that structure in memory. With this mechanism, a developer can make all union members start at the same byte offset, thus mimicking a C union in C#. This technique was used on more than one occasion in the Interop layer, since the DHCP server API defines a couple of structures containing unions.

By using the previously described techniques of p/invoke, a C# wrapper class was created. It contains all known and documented functions exported by the DHCP server API DLL, thus allowing an exchange of data between C and C#. C# extern prototypes of the DHCPSAPI functions were created in the NativeMethods class within the Unf.Dhcp.Smo.Interop namespace. For example, the NativeMethods.DhcpSetOptionValueV5 method directly references its counterpart in the external DHCPSAPI DLL. When a call to this method occurs, the system marshals the C# parameters to C data types, pushes them onto the call stack, and then executes the DHCPSAPI DLL C function. C# structures, located in the NativeStructs.cs source file, were created to mimic the C function parameters used by DHCPSAPI. NativeMethods, MemManager, and native structures make up the Unf.Dhcp.Smo.Interop namespace. These are used by the next layer, DHCP Server Management Objects, which abstracts low-level details of the C like types and functions.