Wednesday, February 25, 2009

Windows Mobile Emulator network

Microsoft Pocket PC Emulator and Network

If you have to develop a software with Microsoft Visual Studio 2005 for a mobile device (for example Pocket PC 2003) which has to access to your network (local area or internet) you must enable the support for the network into the emulator. These are the steps:

1. Download and install the Virtual Machine Network Driver for Microsoft Device Emulator at this address.
I suggest to do the setup with Visual Studio 2005 and the emulator turned off.
2. From the Network Settings select the Local Area Connection, right click and select Properties, check the box near Virtual Machine Network Services
3. In Visual Studio open Tools -> Options
4. Open the Device Tools -> Devices node
5. Select Pocket PC 2003 Emulator and click Properties
6. From the Transport section select the TCP Connect Transport
7. Click the Configure button and select Obtain an IP address automatically using ActiveSync, then close the dialog box
8. Click Emulator Options..., tab Network, check the box Enable NE2000 PCMIA network adapter and bind to: and from the drop down list select your network card
9. Close all the dialog boxes, run your application, in the emulator open then File -> Configure dialog box
10. Check the box or verify the state of the Enable NE2000 PCMIA network adapter and bind to: box, it must be checked

Now from your software you can use the code to connect to internet, databases servers and so on.

Tuesday, February 3, 2009

Alone in the dark - mutex use for single instance of application

Drawback is a denial of service approach

static class Program
{
[STAThread]
static void Main()
{
bool onlyInstance = false;
string mutexName = AssemblyGuid;
// if you want your app to be limited to a single instance across ALL SESSIONS (multiple users & terminal services), then use the following line instead:
// string mutexName = String.Format("Global\\{0}", AssemblyGuid);
Mutex mutex = new Mutex(true, mutexName, out onlyInstance);
if (!onlyInstance) {
IntPtr hwndFirstInstance = WinApi.FindWindow(null, applicationName); // For FindWindow to work, applicationName must be the same as the title (the Text property) of your MainForm.
WinApi.PostMessage(
hwndFirstInstance,
WinApi.WM_SHOWFIRSTINSTANCE,
IntPtr.Zero,
IntPtr.Zero);
return;
}
Application.Run(new MainForm);
GC.KeepAlive(mutex);
}
const string applicationName = "YourApplicationNameGoesHere";
static public string ApplicationName
{
get { return applicationName; } // You could use an Assembly Attribute Accessor instead of a variable, but it involves a bit more code.
}
static public string AssemblyGuid
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
if (attributes.Length == 0) {
return String.Empty;
}
return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value;
}
}
}

public class WinApi
{
public static readonly int WM_SHOWFIRSTINSTANCE = RegisterWindowMessage("WM_SHOWFIRSTINSTANCE");

[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr FindWindow (string lpClassName, string lpWindowName);

public static void ShowToFront(string windowName)
{
IntPtr window = FindWindow(null, windowName);
ShowWindow(window, 1);
SetForegroundWindow(window);
}
}

public partial class MainForm : System.Windows.Forms.Form
{
bool minimizedToTray;

public MainForm()
{
InitializeComponent();
this.Text = Program.ApplicationName;
}
private void btnMinToTray_Click(object sender, EventArgs e)
{
MinimizeToTray();
}
void MinimizeToTray()
{
notifyIcon = new NotifyIcon();
notifyIcon.Click += new EventHandler(NotifyIconClick);
notifyIcon.Icon = this.Icon;
notifyIcon.Text = Program.ApplicationName;
notifyIcon.Visible = true;
this.ShowInTaskbar = false;
this.Hide();
minimizedToTray = true;
}
public void ShowWindow()
{
if (minimizedToTray) {
notifyIcon.Visible = false;
this.ShowInTaskbar = true;
this.Show();
minimizedToTray = false;
} else {
WinApi.ShowToFront(this.Text);
}
}
void NotifyIconOpen(Object sender, System.EventArgs e)
{
ShowWindow();
}
protected override void WndProc(ref Message message)
{
if (message.Msg == WinApi.WM_SHOWFIRSTINSTANCE) {
ShowWindow();
}
base.WndProc(ref message);
}
}