Platform call
P/invoke, whose full name is platform invoke, is used to call the unmanaged flat programming interface implemented in DLL, it is called calling conventions ). The most famous example is the Win32 programming interface, which is a huge library and exposes all the built-in functions of windows.
To call a monotonous unmanaged programming interface, you must first define the function to be called, which can be divided into two steps: Step 1, using system. runtime. the dllimport feature (attribute) in the interopservices namespace, which can define the function to be imported. DLL, with some other optional features; Step 2, use the keyword extern to sign the C-style function call. In this way, the return type is F #, and the function name is specified, finally, the parameter types and names enclosed in parentheses are used. As a result, this function can be called like an external. Net method.
The following example shows how to import the WINDOWS function messagebeep and call it:
Open System. runtime. interopservices
// Declare a function found in an external DLL
[<Dllimport ("user32.dll")>]
Extern boolmessagebeep (uint32 beeptype)
// Call this method ignoring the result
Messagebeep (0ul) |> ignore
Note:
The most difficult problem with platform calling is to find the signature of the function to be called. On the http://pinvoke.net website, there is a list of signatures of common programming interfaces in C # and VB. NET, and the signatures required in F # are similar. This site is a Wikipedia, so you can add the F # signature as needed.
The following code demonstrates how to use the platform call. The target function expects a pointer. Pay attention to the following points when setting pointers. When defining a function, you need to add a star (*) next to the type name to transfer the pointer. before calling the function, you need to define a variable identifier to indicate the memory area to which the Pointer Points, it may not be global, but at the top layer, it must be part of the function definition. This is why the main function is defined. The identifier status is part of the function definition. Finally, the address operator (&) must be used to ensure that the pointer instead of the value is passed to the function.
Prompt
There is always a warning when compiling this code, because the address operator (&) is used (&&). To suppress this warning, you can use the compiler to switch -- nowarn 51, or the command # nowarn 51.
Opensystem. runtime. interopservices
// Declare a function found in an external DLL
[<Dllimport ("advapi32.dll")>]
Extern boolfileencryptionstatus (string filename, uint32 * Status)
Let main () =
// Declare a mutable idenifier to be passed to the Function
Let mutable status = 0ul
// Call thefunction, using the address of operator with
// Secondparameter
Fileencryptionstatus (@ "C: \ test.txt", & Status) |> ignore
Printfn "% d" status
Main ()
The running result of this example is as follows (assume that there is a file test.txt in the C: root directory, encrypted ):
1ul
Note:
Platform calls can also run on the mono platform. The syntax is exactly the same as that in F #. The difficulty is to ensure that the Library to be called is available on all target platforms, the naming conventions for databases on different platforms are also followed. For more information, see the document on http://www.mono-project.com/interop_with_native_libraries.
The dllimport feature has some useful functions that can be used to control how to call unmanaged functions. Table 14-1 is summarized.
Table 14-1 useful features of dllimport
Feature name |
Description |
Charset |
Defines the character set for transmitting string data, which can be charset. Auto, charset. ANSI, charset. Unicode |
Entrypoint |
Set the name of the called function. If no name is specified, the name after the keyword extern is used as the default function name. |
Setlasterror |
This is a logical value that specifies whether any error is encountered and should be transmitted. Therefore, the availability is checked by calling the Marshell. getlastwin32error () method. |
Note:
Because there are COM components, there is no equivalent. the number of unmanaged programming interfaces of. NET is continuously decreasing. Therefore, it usually saves a lot of time to check whether there are equivalent managed functions before calling the function.