Cemapi Practice strategy (1)

Source: Internet
Author: User

Http://blog.csdn.net/depraved_survival/archive/2009/03/09/3969021.aspx

By Wu chunlei QQ: 819543772 Email: wuchunlei@163.com

I. Preparations

1. Development Environment and device Platform

A) download and install

Microsoft has released the Windows Mobile 6.0 SDK and the Chinese version of the simulator. The wm6.0 SDK and simulator are divided into the standard and professional versions, because both versions support cemapi. lib, so we can choose any of the versions as our development platform. This article selects the wm6.0 SDK professional and wm6.0 Chinese Version simulators, as follows:

Windows Mobile 6.0 SDK professional:

Http://download.microsoft.com/download/f/2/3/f232f773-7edc-4300-be07-d3b76a5b3a91/Windows%20Mobile%206%20Professional%20SDK%20Refresh.msi

Windows Mobile 6.0 Chinese Version simulator:

Http://download.microsoft.com/download/0/1/2/012bfbba-9fe5-4e68-86c9-d434446d97dd/0804/Windows%20Mobile%206%20Professional%20Images%20 (CHS). MSI

In addition, the mapirule program used in the last part of this article is not given in the wm6.0 SDK sample. Therefore, to better discuss the last part of this article, you need to download the mapirule source program. I suggest you install the Windows Mobile 5.0 SDK directly. You can find the source program in the wm5.0sdk directory. As for why we do not use the wm5.0 SDK as our development environment, you will find out in the next section. WM 5.0 SDK and simulator are:

Http://download.microsoft.com/download/6/0/8/608530c5-7e9a-4032-bf18-92d90b5f4ab4/WM developer Resource Kit. MSI

After the SDK is successfully installed, we can see the names of the above two platforms in the "installed sdks" list selected in the creation of a smart device application platform. See:

Fig 1.1

In addition, the content discussed in this article and all source code are tested and implemented on Windows XP, vs2005, wm5.0/6.0, and Xia Xin's n810 (wm5.0 System) platform.

B) how to use cellular emulator to test my sms Application

Unlike general applications, the first problem that occurs when you use the cemapi library to develop text message applications is how to test the program. On a real machine, we can test the application through a free text message sending tool such as Apsara, but we usually do not have the conditions and environment for developing the application on the real machine, so how can we test the text message application? Before wm6.0 SDK was launched, we used
To test the text message application. This number is the default local number of the simulator. If you send a text message to this number, the simulator will receive the message. This number should be remembered first.
However, it is far from enough to test our applications. Because of the short messages you receive in the simulator, the attributes of the sender and receiver will always be the 14250010001 number. Obviously, we need a more powerful test environment.

Fortunately (or sadly), Microsoft finally realized this problem. To better help PPC mobile app developers, WM 6.0 SDK provides a small tool named cellular emulator. Through this small software, we can use different numbers to send text messages or make phone calls to the simulator, and the simulator can be tested in a relatively larger range. Unfortunately, this tool only supports the WM 6.0 simulator, which is the main reason why I chose the WM 6.0 SDK as the development platform in this article.

The following describes how to install and use cellular emulator in step by step mode, and how to configure cellular emulator on WM simulator. Don't worry. This is very simple.

First, if you have successfully installed the wm6.0 SDK, you have actually completed the cellular emulator configuration, you can choose "Start Menu"> "Windows Mobile 6.0 SDK"> "Tools"> "cellular emulator" to start the program. If you have not installed the wm6.0 SDK and want to use cellular emulator (I don't know what it means to install the software without installing the WM 6.0sdk ), you can find relevant information on the Internet.

Secondly, we need to make a simple setting for the WM simulator. Follow the steps above to run cellular emulator. Remember the COM port number in the lower left corner of the interface (see Figure 1.2 ). Start the WM simulator (this should be done by everyone) and select "file"-"configuration ..." Open the "simulation program properties" dialog box and select the "peripheral devices" tab page. The "Serial Port 0" title is displayed, as shown in Figure 1.3 and Figure 1.4.

Fig 1.2

Fig 1.3

Fig 1.4

Do you still remember which port I remembered on cellular emulator? (Figure 1.2), enter the port "Serial Port 0" (if not in the menu, you can manually enter it), and then restart the simulator.

All the settings of cellular emulator have been completed. It's easy, isn't it? Next we will use it to send a text message to the simulator. Start cellular emulator, select the "SMS" tab, enter the text message content in the "send to device" dialog box, and enter any number in the "phone number" position below, click "send" and wait patiently for a few seconds. Then, the wm6.0 simulator will receive this text message. It is worth noting that cellular emulator currently does not support sending Chinese text messages. Therefore, if you want to test Chinese text messages, you still need to send text messages to the local phone number 14250010001 for testing. Cellular emulator has many other interesting functions. It has nothing to do with the content discussed in this article. If you are interested, let's explore it by yourself.

Note: If the simulator does not receive any text message, restart the computer.

C) if wm5.0 is used, how can I test my program?

As mentioned above, every WM simulator will bind a number as the local number. You should remember this number: 14250010001. You can send a text message to this number, in this way, the WM simulator can receive this text message. However, the recipient and sender attributes of the SMS will always be 14250010001.

2. How to import the cemapi library,

Cemapi provides both DLL and Lib methods for developers to call. This document uses the most traditional lib call method to use this library (this does not mean that you do not need to use DLL files, only the declaration of symbols is stored in Lib, and the actual implementation is carried in DLL ). Like all lib static libraries, cemapi also includes cemapi. h and cemapi. Lib. we can import the Lib library like other libraries. The two methods are as follows:

Method 1: click "project"> "properties" in the menu to go to the "Project Properties" page ", select "configuration properties"> "connector"> "input" from the tree list on the left side of the "Project properties page", and enter cemapi at the "add dependency" position on the right. lib, see Figure 1.5.

Fig 1.5

Method 2: Add the following code to the source program to import the static library.

# Pragma comment (Lib, "cemapi. lib ")

After the above two methods are used to import the Lib library, you need to include the cemapi. h header file in the source program, so that we can use the objects in cemapi.

3. initialize and release mapi

Just like Initialization is required when you use csocket to connect to develop network applications, you also need to initialize components before using cemapi. Since cemapi uses the COM Component Library, you must first call coinitializeex () to initialize the COM component library, and then call mapiinitializalize () to initialize the cemapi, the purpose of this operation is to tell the system that I will use cemapi from now on. When you no longer need cemapi, you need to use the mapiuninitialize () and couninitailize () functions to release references to cemapi and COM. The coinitializeex function is no longer in the scope of this article. We only provide the calling method for it. As to why we need to select these parameters and their functions, you can collect relevant information on the Internet, the call code is as follows:

Hresult hr;

HR = coinitializeex (null, coinit_multithreaded); // initialize com

 

If (failed (HR ))

{

// Exception Handling

}

Mpaiinitialize (lpvoid lpmapiinit) is defined in the mapix. h file as a function pointer. Its definition is as follows:

Typedef hresult (stdapicalltype mapiinitialize )(

Lpvoid lpmapiinit

);

Mapiinitialize;

This function returns an object of the hresult type, through which we can determine whether the function is successfully called.

The mapiinitialize function has an lpvoid parameter, which is not required in cemapi. Because cemapi comes from mapi, and mapi is originally used to encapsulate the library for operations on exchange, outlook, and other mail systems on the Windows platform, when it is run on different platforms on the desktop system, you can pass a mapiinit_0 struct to the mapiinitialize function to determine the version and work platform (such as the NT System) used ). Cemapi is the version of mapi on the mobile platform. Therefore, the function prototype is retained. this parameter is not used on mobile and can be directly set to null.

After use, release the call to cemapi and reference to the com library as follows.

Mapiuninitialize ();

Couninitialize ();

4. source program in this section

// Initialize cemapi

Void initmapi ()

{

Hresult hR = 0;

 

HR = coinitializeex (null, coinit_multithreaded); // initialize com

 

If (failed (HR ))

{

// Exception Handling

}

 

HR = mapiinitialize (null); // initialize mapi

 

If (failed (HR ))

{

// Exception Handling

}

Return;

}

// Release cemapi

Void uninit ()

{

Hresult hr;

HR = mapiuninitialize ();

If (failed (HR ))

{

// Exception Handling

}

Couninitialize ();

Return;

}

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.