Platform Invoke in C#.NET

Making QR Code ISO/IEC18004 in C#.NET Platform Invoke
Platform Invoke
QR Code Generation In Visual C#.NET
Using Barcode creation for VS .NET Control to generate, create QR Code 2d barcode image in .NET framework applications.
All of the APIs invoked in this section are Windows APIs Although the same APIs are not available on other platforms, developers can still use P/Invoke for APIs native to their platform, or for calls into their own DLLs The guidelines and syntax are the same
Bar Code Encoder In Visual C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create barcode image in .NET applications.
Declaring External Functions Once the target function is identified, the next step of P/Invoke is to declare the function with managed code Just like all regular methods that belong to a class, you need to declare the targeted API within the context of a class, but by using the extern modifier Listing 201 demonstrates how to do this
Encoding Quick Response Code In VS .NET
Using Barcode generator for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
Listing 201: Declaring an External Method
Make QR Code 2d Barcode In .NET
Using Barcode generator for .NET framework Control to generate, create QR Code JIS X 0510 image in .NET framework applications.
using System; using SystemRuntimeInteropServices; class VirtualMemoryManager { [DllImport("kernel32dll", EntryPoint="GetCurrentProcess")] internal static extern IntPtr GetCurrentProcessHandle(); }
Generating Denso QR Bar Code In VB.NET
Using Barcode drawer for VS .NET Control to generate, create Denso QR Bar Code image in .NET applications.
In this case, the class is VirtualMemoryManager, because it will contain functions associated with managing memory (This particular function is available directly off the SystemDiagnosticsProcessor class, so there is no need to declare it in real code) extern methods are always static and don t include any implementation Instead, the DllImport attribute, which accompanies the method declaration, points to the implementation At a minimum, the attribute needs the name of the DLL that defines the function The runtime determines the function name from the method name However, it is possible to override this default using the EntryPoint named parameter to provide the function name (The NET platform will automatically attempt calls to the Unicode [ W] or ASCII [ A] API version) It this case, the external function, GetCurrentProcess(), retrieves a pseudohandle for the current process which you will use in the call for virtual memory allocation Here s the unmanaged declaration:
Bar Code Drawer In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create bar code image in .NET applications.
HANDLE GetCurrentProcess();
Make Barcode In Visual C#
Using Barcode generator for .NET framework Control to generate, create bar code image in .NET applications.
20: Platform Interoperability and Unsafe Code
Code 39 Maker In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create Code39 image in .NET framework applications.
Parameter Data Types Assuming the developer has identified the targeted DLL and exported function, the most difficult step is identifying or creating the managed data types that correspond to the unmanaged types in the external function1 Listing 202 shows a more difficult API
Code 128 Generator In C#.NET
Using Barcode creator for VS .NET Control to generate, create Code-128 image in Visual Studio .NET applications.
Listing 202: The VirtualAllocEx() API
Data Matrix Generator In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create Data Matrix image in .NET framework applications.
LPVOID VirtualAllocEx( HANDLE hProcess, // // // // // // // // // // // // // // // // The handle to a process The function allocates memory within the virtual address space of this process The pointer that specifies a desired starting address for the region of pages that you want to allocate If lpAddress is NULL, the function determines where to allocate the region The size of the region of memory to allocate, in bytes If lpAddress is NULL, the function rounds dwSize up to the next page boundary The type of memory allocation The type of memory allocation
Barcode Printer In .NET
Using Barcode generation for ASP.NET Control to generate, create bar code image in ASP.NET applications.
LPVOID lpAddress,
Code 3 Of 9 Drawer In VB.NET
Using Barcode maker for Visual Studio .NET Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications.
SIZE_T dwSize,
ECC200 Creation In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create ECC200 image in Visual Studio .NET applications.
DWORD flAllocationType, DWORD flProtect);
Data Matrix 2d Barcode Drawer In .NET
Using Barcode maker for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
VirtualAllocEx() allocates virtual memory that the operating system
USS Code 128 Generator In Java
Using Barcode encoder for Java Control to generate, create ANSI/AIM Code 128 image in Java applications.
specifically designates for execution or data To call it, you also need corresponding definitions in managed code for each data type; although common in Win32 programming, HANDLE, LPVOID, SIZE_T, and DWORD are undefined in the CLI managed code The declaration in C# for VirtualAllocEx(), therefore, is shown in Listing 203
UCC - 12 Generation In .NET Framework
Using Barcode generator for Visual Studio .NET Control to generate, create GS1 - 12 image in VS .NET applications.
Listing 203: Declaring the VirtualAllocEx() API in C#
EAN13 Reader In .NET
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
using System; using SystemRuntimeInteropServices; class VirtualMemoryManager { [DllImport("kernel32dll")] internal static extern IntPtr GetCurrentProcess();
Barcode Printer In .NET
Using Barcode maker for .NET framework Control to generate, create bar code image in VS .NET applications.
1 One particularly helpful resource for declaring Win32 APIs is wwwpinvokenet This provides a great starting point for many APIs, helping to avoid some of the subtle problems that can arise when coding an external API call from scratch
Data Matrix ECC200 Printer In Java
Using Barcode generation for Java Control to generate, create ECC200 image in Java applications.
Platform Invoke
Bar Code Creator In .NET
Using Barcode printer for .NET framework Control to generate, create barcode image in .NET applications.
[DllImport("kernel32dll", SetLastError = true)] private static extern IntPtr VirtualAllocEx( IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, uint flProtect); }
One distinct characteristic of managed code is the fact that primitive data types such as int do not change size based on the processor Whether the processor is 16, 32, or 64 bits, int is always 32 bits In unmanaged code, however, memory pointers will vary depending on the processor Therefore, instead of mapping types such as HANDLE and LPVOID simply to ints, you need to map to SystemIntPtr, whose size will vary depending on the processor memory layout This example also uses an AllocationType enum, which I discuss in the section Simplifying API Calls with Wrappers, later in this chapter An interesting point to note about Listing 203 is that IntPtr is not just useful for pointers; it is also useful for other things such as quantities IntPtr does not just mean pointer stored in an integer ; it also means integer that is the size of a pointer An IntPtr need not contain a pointer; it just needs to contain something the size of a pointer Lots of things are the size of a pointer but are nevertheless not pointers
Using ref Rather Than Pointers Frequently, unmanaged code uses pointers for pass-by-reference parameters In these cases, P/Invoke doesn t require that you map the data type to a pointer in managed code Instead, you map the corresponding parameters to ref (or out), depending on whether the parameter is in-out or just out In Listing 204, lpflOldProtect, whose data type is PDWORD, is an example that returns the pointer to a variable that receives the previous access protection of the first page in the specified region of pages