[说明:本文是网上,积累资料 进行整理后的文章,部分代码归个人所有!]
详细功能实现 含自己写的代码:http://blog.csdn.net/li0531/article/details/7868775
正文:
wince 嵌入式应用程序常见问题
1、为什么在调试过程中会提示内存不足? 在默认状态下调试会提示内存不足,无法完成部署,出现这样的问题是因为我们调试默 认选中了部署最新.net环境,有些时候我们调试必须部署电脑上最新.net怎么办呢?这样 我们就在wince系统设置中调节内存控制条,把存储内存调节到9M以上,也不要调节太大, 太大之后会影响应用程序运行速度
|
2、 为什么 USB 同步过程中会频繁丢失连接? 我们在同步调试应用程序或者拷贝过程中有时候会经常丢失连接,出现这样的问题请检 查 USB 线是否完好,电脑 USB 插口最好选择主板上的 USB 口进行连接,或检查同步工具 以及同步驱动是否安装正常 |
3、 为什么调试过程中不会进入断点? 设 置 的 断 点 在 调 试 过 程 中 无 法 进 入 , 这 个 原 因 是 因 为 我 们 电 脑 中 .net Compack |
FrameWork2.0 版本太低,所以我们需要在电脑上进入添加删除程序界面,卸载现有.net Compack FrameWork2.0 环境,然后安装 NETCFSetupv2.msi(SP2) |
卸载前 |
安装后 |
4、 为什么同步总是来宾模式,无法调试,也无法拷贝文件? 这种情况下,我们需在我们电脑注册表中天剑如下键值 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services] "GuestOnly"=dword:00000001 |
5、 如何禁用开始菜单输入法 |
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace JustTest { public class LockTaskBar { [DllImport("CoreDll.dll", SetLastError = true)] public static extern IntPtr FindWindow(string className, string WindowsName); [DllImport("coredll.dll", EntryPoint = "EnableWindow")] public static extern bool EnableWindow(IntPtr hwnd, bool bEnable); // <summary> /// this is for enable and disable task bar. /// Basically this is provide access control Start menu. /// </summary> /// <param name="HHTaskBar">HHTaskBar</param> /// <param name="enabled">default false</param> /// <returns></returns> public static bool Execute(string HHTaskBar, bool enabled) { bool IsState = false; try { IntPtr hwnd = FindWindow(HHTaskBar, null); if (!hwnd.Equals(IntPtr.Zero)) { if (enabled) { IsState = EnableWindow(hwnd, false); } else { IsState = EnableWindow(hwnd, true); } } } catch (DllNotFoundException dllex) { throw dllex; } catch (Exception ex) { throw ex; } return IsState; }
如何调用 /// <summary> /// 禁止开始菜单栏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { LockTaskBar.Execute("HHTaskBar", true); } /// <summary> /// 解禁开始菜单栏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { LockTaskBar.Execute("HHTaskBar", false); } /// <summary> /// 禁止输入法按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { LockTaskBar.Execute("MS_SIPBUTTON", true); } /// <summary> /// 解禁输入法按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { LockTaskBar.Execute("MS_SIPBUTTON", false); }
6、 如何禁用掉系统 wifi 操作界面自动弹出
修改 wince 设备注册表位置 [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Ethman\Popup] "Popup"=dword:00000001
7、 如何让启动、桌面、程序等文件夹下内容可以保存
wince 设备这些文件默认是保存到内存中的,所以用户存放的文件掉电情况下会丢失,我
们可以通过修改注册表,将这些文件夹保存到磁盘中,这样掉电就不会丢失
[HKEY_LOCAL_MACHINE\System\Explorer\Shell Folders] "StartUp"="\\PocketMory1\\Documents and Settings\\StartUp" "Application Data"="\\磁盘名\\Application Data" "Desktop"="\\磁盘名\\桌面" "Favorites"="\\磁盘名\\Favorites" "Fonts"="\\Windows\\Fonts" "My Documents"="\\磁盘名\\My Documents" "Program Files"="\\磁盘名\\Program Files" "Programs"="\\磁盘名\\Windows\\iles" "Recent"="\\Windows\\Recent" "Windows"="\\Windows"
8、 如何去掉窗体打开动画效果
修改注册表位置
[HKEY_LOCAL_MACHINE\system\gwe] "PORepaint"=dword:1
去掉 APP 窗口的动画显示效果(无此键值请添加)
"Animate"=dword:0
9、 如何调用控制面板中的应用程序
System.Diagnostics.Processp=newProcess(); for(inti=0;i<50;i++) ...{ p.StartInfo.FileName=\"ctlpnl.exe\"; p.StartInfo.Arguments=\"\\windows\\cplmain.cpl,\"+i.ToString(); p.StartInfo.UseShellExecute=true; p.Start(); MessageBox.Show(\"ok\"); }
控制面板需要经过 ctlpnl 来调用的。
一般的控制面板参数
ctlpnl.execplmain.cpl,1-setpassword ctlpnl.execplmain.cpl,2-setownerinfo ctlpnl.execplmain.cpl,3-batterystatus ctlpnl.execplmain.cpl,4-memorystatus ctlpnl.execplmain.cpl,5-about ctlpnl.execplmain.cpl,7-align ctlpnl.execplmain.cpl,8-keyboardsettings ctlpnl.execplmain.cpl,9-sound ctlpnl.execplmain.cpl,10-removeprograms ctlpnl.execplmain.cpl,11-startmenu ctlpnl.execplmain.cpl,12-buttonassignments ctlpnl.execplmain.cpl,13-todayscreen ctlpnl.execplmain.cpl,15-beamsettings ctlpnl.execplmain.cpl,16-clock ctlpnl.execplmain.cpl,17-networkadapters ctlpnl.execplmain.cpl,18-regionalsettings ctlpnl.execplmain.cpl,19-connections ctlpnl.execplmain.cpl,20-phonesettings ctlpnl.execplmain.cpl,22-managecertificates ctlpnl.execplmain.cpl,23-bluetooth
10、
如何激活另外一个打开的应用程序窗口
[DllImport("coredll.Dll")] public static extern IntPtr FindWindow( String classname,String title); [DllImport("coredll.Dll")] public static extern void SetForegroundWindow(IntPtr hwnd); public void Start() { IntPtr hDlg; hDlg=FindWindow(null,"窗口标题"); SetForegroundWindow(hDlg); }
11、文件的删除和复制
一般来说,C#在 WinCE 平台上进行文件的删除和复制有两种方法,下面我们将对这两
种方法进行介绍。在介绍之前,对一种情况要特殊说明一下:关于文件的复制,大家都
知道,最简便的方法是将源文件直接复制到目标文件,如果目标文件事先存在,则直接覆
盖;但在 WinCE 的平台上,由于采用 TFAT 文件系统,当进行文件覆盖时,要求系统
剩余空间至少大于所需更新的文件大小,否则文件拷贝时,系统将报错。所以我们建议,
如果目标文件事先存在,先删除目标文件,然后再将源文件复制过去。
两种方法的申明均是:
using System.IO;
1) 方法一:静态 File 方法(最简单的方法)
文件删除: File.Delete(string path);
文件复制: File.Copy(string sourceFileName, string destFileName);
2) 方法二:FileInfo 方法(最灵活的方法)
使用这种方法,每个具体的文件要定义一个 FileInfo 类,然后通过操作具体的类来进行
文件删除、复制或其它操作。下面以一个具体范例来说明:
string sourceFileName, destFileName; sourceFileName = @"\USB Storage\mysourcefile.bin"; //源文件名及路径 destFileName = @"\NandFlash\mydestfile.bin"; //目标文件名及路径 FileInfo sourceFile = new System.IO.FileInfo(sourceFileName); FileInfo destFile = new System.IO.FileInfo(destFileName); try { if (destFile.Exists) destFile. Delete(); //如果目标文件已经存在,则删除 sourceFile.CopyTo(destFileName,true); //将源文件复制到目标文件 } catch(Exception ex) { MessageBox.Show(ex.Message); }
12、获取存储设备的大小信息
调用 Win32 的申明:
using System.Runtime.InteropServices;
在 WinCE 下已经没有了驱动器名的概念,文件存储设备都是在 WinCE 的根目录下中
以目录的形式出现,可以采用如下方法并调用相应函数来获取存储设备的总的大小和空
闲空间的信息:
[DllImport("coredll.dll")] private static extern bool GetDiskFreeSpaceEx(string directoryName, ref long freeBytesAvailable, ref long totalBytes, ref long totalFreeBytes); 调用例子如下: long freeBytes = 0, totalBytes = 0, totalFreeBytes = 0; GetDiskFreeSpaceEx("\\Disk1", ref freeBytes, ref totalBytes, ref totalFreeBytes); string strtotalBytes = "Nor Flash 磁盘空间大小为:"+totalBytes.ToString()+"Bytes";
13、重启系统函数
调用 Win32 的申明:
using System.Runtime.InteropServices;
调用“cordll.dll”里的 Win32 函数 SetCleanRebootFlag( … )和 KernelIoControl( … )
可以实现 WINCE 系统重新启动, 定义的代码如下:
[DllImport("Coredll.dll")]
extern static int KernelIoControl(int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr
lpOutBuf, int nOutBufSize, ref int lpBytesReturned);
[DllImport("Coredll.dll")]
extern static void SetCleanRebootFlag();
可以写成一个系统重启的函数:
public void HardReset()
;mso-height-rule:exactly'>MessageBox.Show(ex.Message);
{
int IOCTL_HAL_REBOOT = 0x101003C;
int bytesReturned = 0;
SetCleanRebootFlag();
KernelIoControl(IOCTL_HAL_REBOOT,IntPtr.Zero,0, IntPtr.Zero, 0, ref bytesReturned);
}
}
然后在程序里需要重启的地方直接调用 HardReset()这个函数即可。
14、隐藏/显示 Windows 任务栏
调用 Win32 的申明: using System.Runtime.InteropServices; 很多客户的应用程序需要独占屏幕,而不需要下方的 Windows 任务栏。因此显示/隐藏 Windows 任务栏是一个很实用的功能。这个时候就需要调用 coredll.dll 里的 FindWindow( … )和 ShowWindow( … )函数来实现任务栏的显示和隐藏(Windows 的任 务 栏实际上也是一个特殊的 Windows 窗口)。 [DllImport("coredll.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpWindowName, string lpClassName ); [DllImport("coredll.dll", EntryPoint = "ShowWindow")] public static extern int ShowWindow( int hwnd, int nCmdShow ); public const int SW_SHOW = 5; //显示窗口常量 public const int SW_HIDE = 0; //隐藏窗口常量 下面是一个隐藏/显示 Windows 任务栏的小例子: public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int Hwnd = FindWindow("HHTaskBar", null); if (Hwnd != 0) { ShowWindow(Hwnd, SW_HIDE); //隐藏任务栏 button2.Enabled = true; button1.Enabled = false; } } private void button2_Click(object sender, EventArgs e) { int Hwnd = FindWindow("HHTaskBar", null); if (Hwnd != 0) { ShowWindow(Hwnd, SW_SHOW); //显示任务栏 button1.Enabled = true; button2.Enabled = false; } }
15、如何在 wince 上实现 GPRS 拨号上网
调用 Win32 的申明:
using System.Runtime.InteropServices;
要使用的两个 Win32 函数申明如下:
#region Win32 API RASDial 函数声明
[DllImport("coredll.dll")]
public static extern uint RasDial(IntPtr dialExtensions, IntPtr phoneBookPath, IntPtr
rasDialParam, uint NotifierType, IntPtr notifier, ref IntPtr pRasConn);
[DllImport("coredll.dll")]
public static extern uint RasHangUp(IntPtr pRasConn);
#endregion
拨号操作使用 RasDial(…)函数,挂断操作使用 RasHangUp(…)函数。尤其需要注意的
是对 RasDial 函数里面使用的参数 rasDialParam 的赋值过程,即将拨号连接名称、用户
名、
密码
16、
如何获取 wince 系统内存情况
调用:
private void button1_Click(object sender, EventArgs e)
{
MemoryStatus m = new MemoryStatus();
GlobalMemoryStatusCE(out m);
double mm=((m.dwAvailPhys/1024.00)/1024.00);
this.Text = mm.ToString();
}
public struct MemoryStatus
{
/// <summary>
/// MEMORYSTATUS 结构的大小,在调 GlobalMemoryStatus 函数前用
sizeof()函数求得,
/// </summary>
public Int32 dwLength;
/// <summary>
/// 返回一个介于 0~100 之间的值,用来指示当前系统内存的使用率
/// </summary>
public Int32 dwMemoryLoad;
/// <summary>
/// 返回总的物理内存大小,以字节(byte)为单位
/// </summary>
public Int32 dwTotalPhys;
/// <summary>
/// 返回可用的物理内存大小,以字节(byte)为单位
/// </summary>
public Int32 dwAvailPhys;
/// <summary>
/// 显示可以存在页面文件中的字节数。注意这个数值并不表示在页面文件
在磁盘上的真实物理大小
/// </summary>
public Int32 dwTotalPageFile;
/// <summary>
/// 返回可用的页面文件大小,以字节(byte)为单位
/// </summary>
public Int32 dwAvailPageFile;
/// <summary>
/// 返回调用进程的用户模式部分的全部可用虚拟地址空间,以字节(byte)
为单位
/// </summary>
public Int32 dwTotalVirtual;
/// <summary>
/// 返回调用进程的用户模式部分的实际自由可用的虚拟地址空间,以字节
(byte)为单位
/// </summary>
public Int32 dwAvailVirtual;
}
[DllImport("coredll", EntryPoint = "GlobalMemoryStatus", SetLastError = false)]
public static extern void GlobalMemoryStatusCE(out MemoryStatus msce);
17、
如何防止应用程序多次打开
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
IntPtr hDlg=IntPtr.Zero;
hDlg = FindWindow(null, "ThisWindows");//ThisWindows:应用程序主窗体
名称
if (hDlg != IntPtr.Zero)
{
SetForegroundWindow(hDlg);
}
else
{
Application.Run(new Form1());
}
}
[DllImport("coredll.Dll")]
public static extern IntPtr FindWindow(String classname, String title);
[DllImport("coredll.Dll")]
public static extern void SetForegroundWindow(IntPtr hwnd);
18、
不通过 ActiveSync 连接 WinCE 使用 IP 连接
设备端配置
1.
从 PC端系统所在盘符:/Program Files/Common Files/Microsoft
Shared/CoreCon/1.0/Target/wce400/<CPU>.文件夹下选择相应的
cpu,复制一下文件到设备端:/Windows/文件夹
Clientshutdown.exe
ConmanClient2.exe
CMaccept.exe
eDbgTL.dll
TcpConnectionA.dll
2.
在设备端启动 ConmanClient2.exe
3.
4.
在设备端启动 CMaccept.exe
找到设备端 IP地址
Visual Studio 配置
在 Visual Studio中点击“工具”-“选项”-“设备工具”-“设备”
选择相应的设备,点击“属性”,点击“传输”后的“配置”
选择“使用指定的 IP地址”,填入设备端的 IP地址,确定
连接设备
1.
2.
3.
4.
19、如何获取当前应用程序完整路径
String CodePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;