WinCE下使用C#来打开一个外部文件

本文介绍了在Windows CE环境下使用C#进行编程的技巧,包括启动外部程序、确保应用程序仅运行一个实例及激活窗口等。提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

使用Windows的开发机上用C#启动一个外部程序的方法有很多,但这些方法用在使用WinCE的目标工控机上都无能为力,现在以打开一个IE为例,介绍如何在WinCE下使用C#来打开一个外部文件:首先添加命名空间

using System.Runtime.InteropServices;,然后调用API函数:

[DllImport("coredll.Dll",  EntryPoint="CreateProcess",  SetLastError=true)] 
extern  static  int  CreateProcess(string  strImageName,  string  strCmdLine,  IntPtr  pProcessAttributes,  IntPtr  pThreadAttributes  ,  int  bInheritsHandle,  int  dwCreationFlags,  IntPtr  pEnvironment,  IntPtr  pCurrentDir,  IntPtr  bArray,  ProcessInfo  oProc); 
  public  class  ProcessInfo 
  { 
   public  Int32  hProcess; 
   public  Int32  hThread; 
   public  Int32  ProcessID; 
   public  Int32  ThreadID; 
  } 
最后就可以编写你需要打开IE的代码了(点击一个按钮打开IE浏览器中相应内容,此例程要求打开目标工控机硬盘上的Readme文件):
 private void button_Click(object sender, System.EventArgs e)
  {
   ProcessInfo  pi  =  new  ProcessInfo(); 
   CreateProcess("//windows//iesample.exe"  ,  "//HardDisk//Readme.htm",  IntPtr.Zero,  IntPtr.Zero,  0,  0,  IntPtr.Zero,  IntPtr.Zero,  IntPtr.Zero,  pi);   
  }
2、有时候我们会希望我们的程式只被执行一次,VB的时代我们会用App.PrevInstance,而.net的时代我们可以用下列方式实现
  [STAThread]
 static void Main()
{
   //如果跟本程式命名的行程只有一个才执行程式
   if (System.Diagnostics.Process.GetProcessesByName(
     Application.ProductName).Length == 1)
  {
    Application.Run(new Form1());
  }
}
但此方法在WinCE下无法实现,所以我们还是要先调用动态链接库,
[DllImport("coredll.Dll")]
private static extern int GetLastError();

[DllImport("coredll.Dll")]
private static extern int ReleaseMutex(IntPtr hMutex);

[DllImport("coredll.Dll")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);

[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
 public int nLength;
 public int lpSecurityDescriptor;
 public int bInheritHandle;
}
const int ERROR_ALREADY_EXISTS = 0183;
然后编写代码
static void Main()
{  
  #region Api_Call CreateMutex;
IntPtr hMutex;
  hMutex=CreateMutex(null,false,"程序名");
  if (GetLastError()!=ERROR_ALREADY_EXISTS)
  {
   Application.Run(new Frmmenu()); 
  }
  else
  {
   MessageBox.Show("本程序只允许同时运行一个");
   ReleaseMutex(hMutex);
 }
 #endregion
}
3、有时我们希望自己的程序能做到激活一个窗体,但在 .NET  Framework 中没有函数可以激活属于另外一个进程或程序的窗体。所以,我们还是要调用API函数来实现:
using System.Runtime.InteropServices;
[DllImport("coredll.Dll")]
 public static extern IntPtr FindWindow(String classname, String title);

[DllImport("coredll.Dll")]
 public static extern void  SetForegroundWindow(IntPtr hwnd);
然后使用下列代码即可
IntPtr hDlg;
hDlg=FindWindow(null, "窗口标题");
SetForegroundWindow( hDlg );
最后,WinCE下的C#里不支持GroupBox控件,建议使用Panel控件代替;不支持Frame控件,如果非要达到那样的效果,可以用Label和TextBox组和起来应付一下。
其实,任何时候,只要.NET Framework无法满足编程者的需要,通常都可以使用托管(interop)机制直接与Windows 交互。大家也许看出调用原有的[DllImport("user32 .Dll")]动态链接库时无法满足WinCE下程序要求,所以我们调用了[DllImport ("coredll.Dll")],并不是小嫚儿自己想到去调用它的,而是在网上看到了一篇“bitsbird”写的例程,很多人将自己的经验无私的与别人分享,所以小嫚儿写了此篇文章,虽然让行家看起来文章很浅显,但希望能给象小嫚儿这样的初学者提供一些捷径。
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功能呢?答案是肯定的,大家可以通过C#中的DllImport直接调用这些功能。 DllImport所在的名字空间 using System.Runtime.InteropServices; MSDN中对DllImportAttribute的解释是这样的:可将该属性应用于方法。DllImportAttribute 属性提供对从非托管 DLL 导出的函数进行调用所必需的信息。作为最低要求,必须提供包含入口点的 DLL 的名称。 DllImport 属性定义如下: namespace System.Runtime.InteropServices {   [AttributeUsage(AttributeTargets.Method)]   public class DllImportAttribute: System.Attribute   {    public DllImportAttribute(string dllName) {...}    public CallingConvention CallingConvention;    public CharSet CharSet;    public string EntryPoint;    public bool ExactSpelling;    public bool PreserveSig;    public bool SetLastError;    public string Value { get {...} }   } }   说明:   1、DllImport只能放置在方法声明上。   2、DllImport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。   3、DllImport具有五个命名参数:    a、CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。    b、CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。    c、EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。    d、ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。    e、PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。    f、SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。   4、它是一次性属性类。   5、此外,用 DllImport 属性修饰的方法必须具有 extern 修饰符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值