How to switch on/off the speaker phone in .NETCF

A few years back, I stumbled upon this article while trying to find a solution on how to switch on/off the speaker phone. It uses a DLL found in Windows Mobile 5 (and higher) devices called ossvcs.dll. This library exposes some pretty neat API’s for controlling communication related features in the device (like controlling the wireless/bluetooth radio and other cool stuff).

Here’s a quick way for switching the speaker on/off in .NETCF

DllImport("ossvcs.dll", EntryPoint = "#218")]        
static extern int SetSpeakerMode(uint mode);
void EnableSpeakerPhone()
void DisableSpeakerPhone()

Unfortunately, ossvcs.dll is not documented and might not exist in the future. But for now, it pretty much works in all devices I’ve tried

How to enumerate storage cards in .NETCF

In Windows Mobile, external storage cards are usually represented as the '\SD Card' or '\Storage Card' folder, this varies from device to device. A simple way to enumerate the external storage cards is to check all directories under the root directory that have the temporary attribute flag set.

Here’s a quick way to do so in .NETCF:

public static List<string> GetStorageCards()
    var list = new List<string>();
    var root = new DirectoryInfo("\\");
    foreach (DirectoryInfo directory in root.GetDirectories()) 
        if (FileAttributes.Temporary == (directory.Attributes & FileAttributes.Temporary))
    return list;

How to send keyboard events in .NETCF

I’ve seen some people ask how to how to send keyboard events in .NET Compact Framework in the community. Although the operation is pretty simple, I’ll share a code snippet anyway.

const int KEYEVENTF_KEYPRESS = 0x0000;
const int KEYEVENTF_KEYUP = 0x0002;
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
void SendKey(Keys key)
    keybd_event((byte) key, 0, KEYEVENTF_KEYPRESS, 0);
    keybd_event((byte) key, 0, KEYEVENTF_KEYUP, 0);

The code above will send the keyboard event to currently focused window or control