【IT168 技术】先说说用VS 2010开发WM程序的好处吧。说到好处这里就不得不提提,最近Windows 7 的正式发售和Visual Studio 2010 Beta2的上线了。这些都是微软微软即将发布的系列产品的一部分。这些新的产品将拥有更高是集成度和更紧密的相互支持。对于开发人员来说,统一的开发标准带来的不仅仅是方便,更是效率的提高。
拿“Hello World”程序来举个例子吧。只需要将Win32版下,程序里的PSTR改为LPTSTR就可以编译运行了。
代码:
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, TEXT("Hello, Windows Mobile!"), TEXT("HelloMsg"), 0);
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, TEXT("Hello, Windows Mobile!"), TEXT("HelloMsg"), 0);
return 0;
}
这表示,Windows Mobile 的开发与一般的Windows程序开发基本是一样的。不需要很复杂的学习过程大家就可以上手了。并且可以使用自己熟悉的语言来进行编写。
伴随着智能手机的普及,加上3G时代的到来,编写手机应用程序的需求必将迎来一个爆炸式的增长。
下面来看一段windows mobile中用C#实现的cmnet链接的代码:
public bool Connectcmnetmethod(Guid destGuid, bool exclusive, bool mode)
{
{
connInfo.cbSize = (uint)Marshal.SizeOf(connInfo);
connInfo.dwParams = 0x1;//CONNMGR_PARAM_GUIDDESTNET;
connInfo.dwPriority = 0x08000;
connInfo.dwFlags = 0x1 | 0x2 | 0x4 | 0x8;//0;
connInfo.bExclusive = 0;// exclusive;
connInfo.bDisabled = 0;// false;
connInfo.guidDestNet = destGuid;
connInfo.hWnd = this.Handle;
if (mode == true)
{
result = ConnMgrEstablishConnectionSync(ref connInfo, ref hConnection, 10000, out dwStatus);
if (result != 0) MessageBox.Show(result.ToString("X"), dwStatus.ToString("X"));
return result == 0;
}
return false;
}
}
public struct CONNMGR_CONNECTIONINFO
{
public uint cbSize;
public uint dwParams;
public uint dwFlags;
public uint dwPriority;
public int bExclusive;
public int bDisabled;
public Guid guidDestNet;
public IntPtr hWnd;
public uint uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
[DllImport("cellcore.dll", EntryPoint = "ConnMgrEstablishConnectionSync", SetLastError = true)]
internal static extern int ConnMgrEstablishConnectionSync(ref CONNMGR_CONNECTIONINFO pConnInfo, ref IntPtr phConnection, int dwTimeout, out int dwStatus);
[DllImport("cellcore.dll", EntryPoint = "ConnMgrEstablishConnection", SetLastError = true)]
internal static extern int ConnMgrEstablishConnection(ref CONNMGR_CONNECTIONINFO pConnInfo, ref IntPtr phConnection);
[DllImport("cellcore.dll", EntryPoint = "ConnMgrReleaseConnection", SetLastError = true)]
internal static extern int ConnMgrReleaseConnection(IntPtr hConnection, int lCache);
[DllImport("coredll.dll")]
private static extern int CloseHandle(IntPtr hObject);
[DllImport("Coredll.dll", EntryPoint = "GetLastError", SetLastError = true)]
internal static extern int GetLastError();
private void menuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show(Connect(new Guid("7022E968-5A97-4051-BC1C-C578E2FBA5D9"), false, true).ToString());
}
{
{
connInfo.cbSize = (uint)Marshal.SizeOf(connInfo);
connInfo.dwParams = 0x1;//CONNMGR_PARAM_GUIDDESTNET;
connInfo.dwPriority = 0x08000;
connInfo.dwFlags = 0x1 | 0x2 | 0x4 | 0x8;//0;
connInfo.bExclusive = 0;// exclusive;
connInfo.bDisabled = 0;// false;
connInfo.guidDestNet = destGuid;
connInfo.hWnd = this.Handle;
if (mode == true)
{
result = ConnMgrEstablishConnectionSync(ref connInfo, ref hConnection, 10000, out dwStatus);
if (result != 0) MessageBox.Show(result.ToString("X"), dwStatus.ToString("X"));
return result == 0;
}
return false;
}
}
public struct CONNMGR_CONNECTIONINFO
{
public uint cbSize;
public uint dwParams;
public uint dwFlags;
public uint dwPriority;
public int bExclusive;
public int bDisabled;
public Guid guidDestNet;
public IntPtr hWnd;
public uint uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
[DllImport("cellcore.dll", EntryPoint = "ConnMgrEstablishConnectionSync", SetLastError = true)]
internal static extern int ConnMgrEstablishConnectionSync(ref CONNMGR_CONNECTIONINFO pConnInfo, ref IntPtr phConnection, int dwTimeout, out int dwStatus);
[DllImport("cellcore.dll", EntryPoint = "ConnMgrEstablishConnection", SetLastError = true)]
internal static extern int ConnMgrEstablishConnection(ref CONNMGR_CONNECTIONINFO pConnInfo, ref IntPtr phConnection);
[DllImport("cellcore.dll", EntryPoint = "ConnMgrReleaseConnection", SetLastError = true)]
internal static extern int ConnMgrReleaseConnection(IntPtr hConnection, int lCache);
[DllImport("coredll.dll")]
private static extern int CloseHandle(IntPtr hObject);
[DllImport("Coredll.dll", EntryPoint = "GetLastError", SetLastError = true)]
internal static extern int GetLastError();
private void menuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show(Connect(new Guid("7022E968-5A97-4051-BC1C-C578E2FBA5D9"), false, true).ToString());
}
编写的思路:判断connInfo.guidDestNet = destGuid,导入cellcore.dll使用它的三个方法,最后异常处理Coredll.dll,GetLastError.
在Visual Studio 2010下很容易的实现了简单,易用,稳定,高效的程序要求。但是也有一些问题存在,比如:手机资源消耗大,要安装移动版的Framework程序。 市场上windows mobile的占有率很低,短时间内不会有很多撰写程序的需求。
不过,微软拓展手机市场的决心和力度还是可以看到的。新版的WM 7 也有了长足的进步。相信前景是光明的。