Making calls to API in Windows Mobile is lot easier compared to other platforms such as Symbian etc, but we need to take care of few aspects mentioned below.
- Since the parameters sent to the API are not user friendly, developers need to have some device level knowledge
- Since all APIs that the developers need might not be exposed in a single DLL, the DLL name will have to be specifically mentioned. DLL name where the API exists has to be hardcoded while instantiating the API method
Below is an example emphasizing this. In the example, the input mode of a Text box control is set at runtime. The APIs which are used are: - GetCapture: Retrieves the handle of control which is currently active
- GetWindow: Child window is retrieved for given handle
- SendMessage: Any operation can be performed by sending a message using current handle, appropriate modes. This API is generally used for setting the input mode of any input controls (e.g. textbox control)
The API call in C# can be as below.
[DllImport("coredll.dll", EntryPoint="GetCapture")]
private static extern IntPtr GetCapture();
Note: Above code has to be repeated for other two APIs. Windows CE (the operating system Windows Mobile is based upon) aims to have a Win32 API implementation which is fairly compatible with the implementation found in desktop versions of the Microsoft Windows operating system. However, for a number of technical and historical reasons the APIs are commonly exposed by different DLLs. This leads to a potential issue since the DLLImport attribute utilized to Platform Invoke a function requires hard coding the DLL’s name into your application.
Next step involves setting the input mode of the given control.
The function should do the following operations in the order given below: - Retrieve the handle for the control by using the GetCapture API
- Retrieve child window (.NET Compact Framework text box using Spy refers to the real text box as a “Child window”) using GetWindow API
- Send the message to the child handle to set the input mode using SendMessage API
Definition of “InputMode” is given below:
public enum InputMode
{
Spell = 0,
T9 = 1,
Numbers = 2,
Text = 3
}
Warning: The use of handles (hWnds) from within the .NET Compact Framework is not supported and should be thoroughly tested within your application. Due to the way the runtime internally manages hWnds, the use of hWnds may not work with future releases or could break your code if you do a lot of manipulation with them. And note that the class uses Interop to call a few native APIs.
If you find any other easy way to call APIs then please let me know
To summarize, “Why making calls to APIs in Windows Mobile is lot easier compared to other platforms?” - Implementation of API calls in other platforms is not as straightforward as it is in Windows Mobile. There are many steps involved and developers should have a thorough background of the same.
- In other platforms, we cannot write Managed code as it is not supported




