C# Rocks!

June 1, 2009

Years ago when .NET (c#) beta was first released, I had downloaded and installed to give it a shot. But I quickly realized that all that could be done in C# was possible with Visual C++ (MFC) with which I was very comfortable at the time. I lost interest and moved on.

Years later I had to choose between Java and C# to work on a new project. Experience in Win32 and MFC pushed me to use C# as I thought I could directly map the two. I was right to a great extent. So for all the people who have done Windows programming using Win32 and MFC I would say… Go for it. C# is completely object orientated and is a syntactically beautiful language. You will surely have loads of fun programming with it.

For anyone who wants to know why C# was developed, take a look at this.

To get started on programming in C#, look here.


Solution for POOM Linker Error

April 4, 2009

I was trying to build sample code from MSDN that displays the Pocket Outlook version and demonstrates  POOM initialization and basic usage on interfaces. I copied and pasted the code into a cpp file of a sample Win32 Smart device project.  Expecting that I am all done, I gave a build. But the build failed and the linker threw the following error messages.

Sample.obj : error LNK2001: unresolved external symbol CLSID_Application
Sample.obj : error LNK2001: unresolved external symbol IID_IPOutlookApp

Quick google search didn’t get any useful results until I tried the following.

Solution

The solution was to replace #define INITGUID with #include <initguid.h> and the build passed.

Original Sample code from MSDN

#define INITGUID  // Replace this by #include <initguid.h>
#include <windows.h>
#include <pimstore.h>

HRESULT hr;
IPOutlookApp * polApp;

// Initialize COM for Pocket Outlook.
if (FAILED(CoInitializeEx(NULL, 0))) return FALSE;

// Get the application object.
hr = CoCreateInstance(CLSID_Application,
NULL,
CLSCTX_INPROC_SERVER,
IID_IPOutlookApp,
(LPVOID*)&polApp);
if (FAILED(hr)) return FALSE;

// Log on to Pocket Outlook.
hr = polApp->Logon(NULL);
if (FAILED(hr)) return FALSE;

// Get the version and display it in a message box.
BSTR pwszVersion = NULL;
polApp->getVersion(&pwszVersion);
MessageBox(NULL, pwszVersion, TEXT(“POOM Version”), MB_SETFOREGROUND |
MB_OK);

// Free the version string.
SysFreeString(pwszVersion);
// Note: For Palm-size PC version 1.0 use the Application method:
// polApp->SysFreeString(pwszVersion).

// Log off and release the application object.
polApp->Logoff();
polApp->Release();
return TRUE;


KeePass Password Safe

March 15, 2009

I was looking for a simple and secure way to store my passwords. There are many utilities available like Roboform, Password Corral, KeePass etc.  I only tried KeePass and it works just the way I wanted. I can use it without installating it. Just download the zipped version, extract it on your computer and launch KeePass.exe. You can save username/password information in various builtin catergories called groups. You can also create your own groups and subgroups.

Read more about KeePass at http://keepass.info/

KeePass main screen

KeePass main screen


Windows Mobile Phone Signal Strength

March 9, 2009

There is no API to determine the current signal strength of the phone radio on a Windows Mobile Phone Device. But as the signal value changes, its updated in the registry at the following location.

[HKEY_LOCAL_MACHINE\System\State\Phone]

Signal Strength Raw” value under the above key stores signal strength value in percentage (0 – 100).

You might want to know in your application when signal strength status changes.  Polling registry is a resource intensive operation and should be avoided. Instead you can use RegistryNotifyCallback function provided by State and Notifications Broker mechanism. This powerful feature is available since WM 5.0 and gives applications an easy way to get notified when a particular registry value changes. Read more about it in MSDN.

http://msdn.microsoft.com/en-us/library/aa455748.aspx


Programmatically capture images with Camera application

March 6, 2009

While developing an application I found that following methods can be used to launch the Camera capture application.

I have seen the following code work on WM 5.0 device:

SHELLEXECUTEINFO ExecInfo = {0};
ExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ExecInfo.lpFile = L”\\windows\\pimg.exe”;
ExecInfo.lpParameters = L”-camerakey”;
ExecInfo.lpVerb = L”open”;
ShellExecuteEx(&ExecInfo);

And following code on WM 6.0 device:

void ShowCameraCapture()
{

SHCAMERACAPTURE shcc;

// Set the SHCAMERACAPTURE structure.
ZeroMemory(&shcc, sizeof(shcc));
shcc.cbSize = sizeof(shcc);
shcc.hwndOwner = hwndOwner;
shcc.pszInitialDir = NULL;
shcc.pszDefaultFileName = NULL;
shcc.pszTitle = TEXT(“PK Camera”);
shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_ALL;
shcc.nResolutionWidth = 0;
shcc.nResolutionHeight = 0;
shcc.nVideoTimeLimit = 0;
shcc.Mode = CAMERACAPTURE_MODE_STILL;

// Launch the Camera Capture screen
SHCameraCapture(&shcc);

}

You can fillup the fields of SHCAMERACAPTURE structure for controlling many aspects of camera operations. Look into MSDN for details.


How to show Control Panel applets on Windows Mobile?

February 7, 2009

Windows Mobile Control Panel applets are normal dlls renamed with special extension .cpl. They are actually loaded by ctlpnl.exe process.

Following code snipped is what you need if you want to show a Control Panel applet from your program. This way you make it easy for your user to change desired settings, without any “complex” navigation.

BOOL ShowControlPanelApplet(int id)
{
TCHAR    szParams[32];
SHELLEXECUTEINFO execinfo = {0};
memset(&execinfo, 0, sizeof(execinfo));
execinfo.cbSize=sizeof(execinfo);
execinfo.lpFile=TEXT(“\\windows\\ctlpnl.exe”);
execinfo.lpVerb=TEXT(“open”);
// Id value determines which control applet will be launched
// For e.g. 23 for bluetooth applet
wsprintf(szParams, L”cplmain.cpl,%d”, id);
execinfo.lpParameters = szParams;
BOOL bRet=ShellExecuteEx(&execinfo);
return bRet;
}

The parameter id refers to the control panel applet that you want to show. Below is the table of id values and the corresponding applet names.

Control Panel Applet Id Values

1    Password
2    Owner Information
3    Power
4    Memory
5    About
6    Brightness
7    Screen
8    Input
9    Sounds & Notifications
10    Remove Programs
11    Menus
12    Buttons
13    Today
14
15    Beam
16    Clocks & Alarms
17    Configure Network Adapters
18    Regional Settings
19    Connections
20
21
22    Manage Certificates
23    Bluetooth
24    Error Reporting
25    GPS Settings
26
27    USB to PC


Launch application with ShellExecuteEx API

January 7, 2009

It easy to launch an application and pass parameters using ShellExecuteEx API.  Following is a wrapper which prepares SHELLEXECUTEINFO structure and then calls ShellExecuteEx.

BOOL LaunchApplication(TCHAR* pStrPath, TCHAR* pStrParams)
{
SHELLEXECUTEINFO    shell_exec_info = {0};
shell_exec_info.cbSize = sizeof(SHELLEXECUTEINFO);
shell_exec_info.lpVerb = L”open”;
shell_exec_info.lpFile = pStrPath;
shell_exec_info.lpParameters = pStrParams;
shell_exec_info.fMask = SEE_MASK_NOCLOSEPROCESS;
return ShellExecuteEx(&shell_exec_info);
}

Another way which is more powerful but could be more complicated is to use CreateProcess API.