Native structure versus managed structure

The first thing we would run into, when attempting to tie something similar to the implementation of our core's interface to a platform such as .NET, is the way data is passed between managed code and native code. There's hardly any possibility for managed and native code to access the same memory areas. It is not impossible, but definitely is not healthy, hence we would have to pass data between the two domains--the managed domain and native domain. Luckily, there is a class in the .NET framework that allows us to perform such operations relatively painlessly--System.Runtime.InteropServices.Marshal. Since we are using a pointer to a structure containing pointers to exported procedures, we need to implement a managed structure to be used with our .NET crypto class and this is done in a rather simple manner:

// First of all, we tell the compiler how members of the
//struct are stored in memory and alignment thereof
[StructLayout(LayoutKind.Sequential, Pack=1)]

// Then we implement the structure itself
internal struct Funcs
{
internal IntPtr f_set_data_pointer;
internal IntPtr f_set_data_length;
internal IntPtr f_encrypt;
internal IntPtr f_decrypt;
}

The preceding code perfectly declares the type of structure we need and we may get to implement the crypto class. Although the implementation of misbelief C# class falls way beyond the scope of this book, it seems appropriate to dedicate a few lines to a definition of methods and delegates in this case.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset