.
In Depth Banner
Skip Navigation Links

Interop

The code in this example relies heavily on Interop.  It is of course possible to recreate the signatures of all the methods required for each application that being an inherently lazy person I want to reuse as much as I can.  For this reason, the application code contains a couple of files that I have been building up for a while which have a lot of handy definitions of both the function signatures which are used often and structures and enumerations that they require. 

Generally, my approach to interop is "don't do it unless it's necessary" because it tends to add a level of indirection that breaks the flow of a managed application.  It is however quite common that necessity rears it's ugly head and so my interop files have gradually become longer and longer. 

The first file, shown in this listing, contains the windows structure definitions and enumerations such as the WM_XXXX messages and WS_XXXX window styles.  These were lifted directly from winuser.h and converted for the language.  The second file, shown in this listing, contains the imported Win32 function signatures that have been suitably modified for the language.  When importing function signatures the most important thing is to get the marshalling of the parameters and return types correct.  Very often this will require the recreation of a structure such as the POINT or RECT structures that Win32 uses but which don't necessarily correspond exactly to the definitions of the equivalent structures in .net.

One of my pet peeves with the .NET Framework is the seemingly ridiculous inability to perform sensible operations with unsigned integer types.  For example, you cannot assign the value from a signed integer to an unsigned integer.  It's also impossible to perform some tests such as value equality or magnitude comparisons. Therefore, I tend to replace unsigned types with signed types of the same size as much as possible.  This means that in certain cases in the Interop files you will see a parameter or a return type which is unsigned in the original definition declared as signed in my Interop files.  

Return to the article.

Copyright © Bob Powell 2000-.  All rights reserved.