. NET Compact framework using the P/invoke Service (1)

Source: Internet
Author: User

Some of the things that need to be programmed at the underlying layer of Wince recently are used in processes in PPC,. NET Compact framework does not support the process component. If you find these items online, you have never touched the underlying things before. I think it is better. Let's take a look at them!

1. With the support of. NET Compact framework, P/invoke can easily and efficiently develop applications suitable for mobile devices without considering specific hardware environments .. NET Compact framework shields developers from the underlying hardware details, allowing developers to focus on business logic solutions. As a subset of. NET Framework,. NET Compact framework only provides some. NET Framework functions. Therefore, sometimes some functions have to be implemented using the Windows ce api. In addition, some third-party components/resources are provided in the form of dynamic link libraries or COM components. Compared with. NET Compact framework, they all belong to unmanaged resources. We need a feature to access these unmanaged resources in a hosted environment. Like. NET Framework, the Platform calls P/invoke (platform invocation services) to provide the function of hosting code calls the unmanaged functions residing in the DLL. The following is a schematic of P/invoke from msdn.

In summary, p/invoke is used in the following scenarios: 1 ,. NET Compact framework does not implement a certain function, so you need to use Windows ce api; 2. Existing DLL or COM components and other resources, and hope to make full use of them to reduce development costs and risks; 3. In view of the DLL execution performance and decompilation capability. NET Compact framework, improve program performance and security with DLL. Of course, you cannot generalize whether the DLL execution performance is higher than the managed code. 2. For P/invoke under. NET Compact framework, Let's first look at a p/invoke example. The following uses the dllimport feature to import the definition of the Windows ce api function messageboxw. Public class apihelper {[dllimport ("coredll. dll", setlasterror = true)] public static extern int messageboxw (intptr hwnd, string text, string caption, uint type );}

Then you can call it. Private void button#click (Object sender, eventargs e) {apihelper. messageboxw (intptr. Zero, "test messageboxw function", "API call", 0 );}

We can see that using P/invoke includesStatementAndCallTwo procedures, and oneError Handling. The. NET Compact framework also uses the dllimport feature to declare the unmanaged functions to be called through declaration, including the module name, function name, and call conventions. Unlike the full-version dllimport feature of. NET Framework,. NET Compact framework has five common fields: callingconvention, charset, entrypoint, preservesig, and setlasterror. For details about each field, refer to msdn. Entrypoint can be specified as the function name or serial number value, for example, entrypoint = "messageboxw" or entrypoint = "#858 ". It is worth noting that under. NET Compact framework, callingconvention only supports callingconvention. winapi, that is, the default platform call. The encoding method only supports Unicode. Therefore, charset only has one charset. Unicode value. Therefore, the effects of the callingconvention and charset fields omitted during the import definition are not different. In addition, the dllimport modifier must use the static and extern keywords to indicate that the method is implemented externally, and there is no restriction on its visibility modifier. When you call an unmanaged function imported by dllimport, the clr p/invoke service extracts metadata from the Declaration and locates the module to be called (coredll. DLL), load it To the memory, and then retrieve the unmanaged function address based on the entry point information. If no error occurs, P/invoke completes parameter encapsulation and calls the function, and returns the return value of the function. P/invoke produces two kinds of errors. One is that the P/invoke mentioned above has an error in locating the call module and retrieving the function address. If P/invoke cannot find the entry point, an error occurs and a missingmethodexception exception is thrown. If the function call convention declares that there is an error, a notsupportedexception exception is thrown, in this case, check whether the parameter and return value definitions of the function are consistent with the functions in the module. Another Error of P/invoke is that occurs during the execution of an unmanaged function. In addition, P/invoke in. NET Compact framework does not support callback, that is, it cannot pass a delegate to an unmanaged function and be called in an unmanaged function. An exception occurs when you use an unmanaged function that requires callback.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.