Some useful tools

If we deal with definitions and also with the distinct ways in which a system's API should be called from .NET, there's a reference website: PInvoke.net (available at http://www.pinvoke.net/).

You'll find the vast majority of the system's APIs clearly detailed, explaining the way they work, how they should be defined in our code (either from C# or VB.NET), and all other related information.

For example, knowing that all window messages are defined with the WM_ prefix, we can expand it under the Constants topic to locate the one I used earlier.

Furthermore, we are shown a definition of the message and its purpose, along with the hexadecimal number associated with it, and at the end of that list, you'll find the C# definitions, where it's easy to locate those linked to the left button, as shown in the next screenshots:

Some useful tools

Following, you can see the definitions in C# code, ready to use in a program:

Some useful tools

In the code, as you already know, we can use the decimal equivalent (as I did) or the hexadecimal definition, with the same results. Actually, it's preferable to use these definitions to produce clearer and readable code instead of what I did in the first demo.

If, instead, you look for a function or scroll over some well-known system DLLs such as user32.dll, you will see that it contains lots of functions related to Windows, such as FindWindowEx. If you expand this function, you'll see the definition that we should use in our code in order to call that function, as we will do in the next section, Platform/Invoke.

A bit further down, we can even find a sample of how to use the function in practice (in this case, this is done in order to get a reference to the horizontal scroll bar of a window).

There's also another interesting tool, created by Justin Van Patten, at Microsoft: P/Invoke Interop Assistant, which we can download and install from Codeplex, at http://clrinterop.codeplex.com/releases/view/14120.

Once downloaded and installed, you'll find several tools in the Program Files (X86)/InteropSignatureToolkit directory. Two of them are command-line tools to help you define the function's signatures from other libraries or DLLs (they might be TLBs as well).

The last one is a Windows app that lets you do the work we previously did in the PInvoke.net site, only from a Windows application. It's called Windows Signature Generator (winsiggen.exe).

Within these tools, you can import DLLs from anywhere in the system, or you can even consult the previous definition without requiring any more work: by selecting the SigImp Search tag, you filter what you're looking for and see the definitions in a list. Furthermore, by selecting the definitions you want to work with, you'll have the choice to generate the C# or VB.NET code necessary, as the following screenshot shows, in which I searched for the definitions used in the previous demo:

Some useful tools

Besides this solution, there is another choice, which is pretty interesting for the developer in Visual Studio: under the Extensions and Updates menu; if you select on-line and filter pinvoke or similar, there's a version of this tool called PInvoke.net for Visual Studio Extension, lately managed by Red Gate. You should find an entry like the one shown in the next screenshot:

Some useful tools

Once installed (it takes a moment), it will create a new menu in the IDE after a restart of Visual Studio.

What you will see is a window similar to the one in the previous tool, but highly simplified and with the option to search for any function or module or visit the PInvoke.net site if you need to look for a definition.

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

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