How to encapsulate Native DLL provided to. NET Compact Framework in Windows Mobile and Wince (Windows Embedded CE) for calling

Source: Internet
Author: User
Background

In the development of Windows Mobile and Wince (Windows Embedded CE) products, sometimes C ++ is used to encapsulate some common code, for example, C ++ is used in our project to encapsulate a public code library for USB communication. The shared code is compiled into a static library. When other modes are used, you only need to include the header file, link to the lib library,. the NET Compact Framework program cannot use the static library compiled by C ++, so the Native DLL encapsulation is provided. NET Compact Framework program call requirements.

 

Introduction

This article describes how to encapsulate Native DLL provided to. NET Compact Framework in Windows Mobile and Windows Wince (Windows Embedded CE) for calling.

 

Optional

In. under the. NET framework, You can compile C ++ into a hosted DLL. NET programs can directly call this DLL, but in. under the NET Compact Framework, managed c ++ is not supported, so it can only be encapsulated into Native DLL, and then. NET Compact Framework P/Invoke the Native DLL. The Native DLL package has two optional solutions.

 

Solution 1:. DEF File

Use the. DEF file to define the output interface.

LIBRARY   NativeLib
EXPORTS
Insert @1
Delete @2
Member @3
Min @4

The. def File above defines four output interfaces. If you use the. def file, you need to configure it in the compilation options, such:

Use. def files must be updated manually each time they are updated. def file, but use. one advantage of the def file is that after the dll is updated, if the defined ordinal does not change, the caller can use the new DLL without re-linking the program, today's Windows Mobile components are used. to define the def file, you must implement the ordinal interfaces 240 and 241:

InitializeCustomItem        @240    NONAME
CustomItemOptionsDlgProc @241 NONAME

In this way, the shell32 process can load the DLL that implements the two interfaces without reconnecting to support new components.

 

Solution 2: _ declspec (dllexport)

I personally prefer to use the _ declspec (dllexport) solution, because P/Invoke does not have the problem of the caller reconnecting, so I prefer to use the _ declspec (dllexport) solution.

Use the _ declspec (dllexport) scheme and add _ declspec (dllexport) to the function to be output ). This function generates an output table.

#define EXPORT_API __declspec(dllexport)
EXPORT_API BOOL OpenIndicator()
{
return TRUE;
}

For example, the above definition generates the following output. Use Dumpbin/exports for viewing.

Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file NativeLib.dll

File Type: DLL

Section contains the following exports for NativeLib.dll

00000000 characteristics
4AF757EA time date stamp Sun Nov 08 10:44:42 2009
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 0000BA30 ?OpenIndicator@@YAHXZ = ?OpenIndicator@@YAHXZ (int __c
decl OpenIndicator(void))

Summary

1000 .data
2000 .pdata
2000 .rdata
2000 .reloc
19000 .text

Is there any output? OpenIndicator @ YAHXZ instead of OpenIndicator.

If the. NET Compact Framework caller is as follows:

[System.Runtime.InteropServices.DllImport("DeviceCommsLib.dll", SetLastError = true]
private static extern bool OpenIndicator();

When the program is executed, an exception cannot be found at the entry point, such:

Why is C ++ used to name the output when generating the output? OpenIndicator @ YAHXZ instead of OpenIndicator. To solve this problem, there are two ways: method 1, specify the entry point in the Caller:

[System.Runtime.InteropServices.DllImport("DeviceCommsLib.dll", SetLastError = true, EntryPoint = "?OpenIndicator@@YAHXZ")]
private static extern bool OpenIndicator();

This method is not good because the caller needs to use Dumpbin/exports to view the output entry point.

 

 

 

Method 2: Add extern "C" as follows:

#define EXPORT_API __declspec(dllexport)
extern "C" {
EXPORT_API bool OpenIndicator()
{
return true;
}
}

In this way, the C name is used. Use Dumpbin/exports to view the output as follows:

Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file NativeLib.dll

File Type: DLL

Section contains the following exports for NativeLib.dll

00000000 characteristics
4AF75999 time date stamp Sun Nov 08 10:51:53 2009
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 0000BA30 OpenIndicator = OpenIndicator

Summary

1000 .data
2000 .pdata
2000 .rdata
2000 .reloc
19000 .text

The output interface is changed to OpenIndicator.

 

Summary

To sum up, if a shared static library of Native C ++ is available. NET Compact Framework call. First, create a win32 DLL, link the static library to the DLL, and define the DLL to output the interface through extern "C" _ declspec (dllexport, finally. the NET Compact Framework program can use P/Invoke to call the functional modules in the original static library.

For more information about P/Invoke, I have previously written some articles.

Use of Win32 api p/Invoke under. NET Compact Framework

Develop P/Invoke tools and Website

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.