//Wince 中运行一次程序的方法
//1.执行未能实现。
static class Program
{
[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);
private const int ERROR_ALREADY_EXISTS = 0183;
[StructLayout(LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES { public int nLength; public int lpSecurityDescriptor; public int bInheritHandle; }
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
IntPtr hMutex = CreateMutex(null, false, "TPMofWinCE");
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
System.Windows.Forms.Application.Run(new frmBegin());
return;
}
else
{
MessageBox.Show("程序已启动");
ReleaseMutex(hMutex);
return;
}
}
方法二:第一次就取到了自己的进程名称,不是太明白,先签标,晚点再继续了解。
namespace SmartDeviceProject1
{
static class Program
{
[DllImport("Toolhelp.dll")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("Coredll.dll")]
public static extern int CloseHandle(IntPtr handle);
[DllImport("Toolhelp.dll")]
public static extern int Process32First(IntPtr handle,ref PROCESSENTRY32 pe);
[DllImport("Toolhelp.dll")]
public static extern int Process32Next(IntPtr handle,ref PROCESSENTRY32 pe);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
IntPtr handle = CreateToolhelp32Snapshot((uint)SnapShotFlags.TH32CS_SNAPPROCESS, 0);
if ((int)handle != -1)
{
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
pe32.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
int bMore = Process32First(handle, ref pe32);
PROCESSENTRY32 pe;
while (bMore==1)
{
IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
Marshal.StructureToPtr(pe32, temp, true);
pe = (PROCESSENTRY32)Marshal.PtrToStructure(temp, typeof(PROCESSENTRY32));
Marshal.FreeHGlobal(temp);
MessageBox.Show(pe32.szExeFile);
bMore = Process32Next(handle, ref pe32);
}
}
CloseHandle(handle);
Application.Run(new Form1());
}
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]//注意,此处为宽字符
public string szExeFile;
public uint th32MemoryBase;
public uint th32AccessKey;
}
public enum SnapShotFlags:uint
{
TH32CS_SNAPHEAPLIST = 0x00000001,
TH32CS_SNAPPROCESS = 0x00000002,
TH32CS_SNAPTHREAD = 0x00000004,
TH32CS_SNAPMODULE = 0x00000008,
TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE),
TH32CS_GETALLMODS = 0x80000000
}
}
这里在自己程序中的应用:
namespace TPMofWinCE
{
static class Program
{
[DllImport("Toolhelp.dll")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("Coredll.dll")]
public static extern int CloseHandle(IntPtr handle);
[DllImport("Toolhelp.dll")]
public static extern int Process32First(IntPtr handle, ref PROCESSENTRY32 pe);
[DllImport("Toolhelp.dll")]
public static extern int Process32Next(IntPtr handle, ref PROCESSENTRY32 pe);
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]//注意,此处为宽字符
public string szExeFile;
public uint th32MemoryBase;
public uint th32AccessKey;
}
public enum SnapShotFlags:uint
{
TH32CS_SNAPHEAPLIST = 0x00000001,
TH32CS_SNAPPROCESS = 0x00000002,
TH32CS_SNAPTHREAD = 0x00000004,
TH32CS_SNAPMODULE = 0x00000008,
TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE),
TH32CS_GETALLMODS = 0x80000000
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
IntPtr handle = CreateToolhelp32Snapshot((uint)SnapShotFlags.TH32CS_SNAPPROCESS, 0);
bool profilename=false;
if ((int)handle != -1)
{
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
pe32.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
int bMore = Process32First(handle, ref pe32);
PROCESSENTRY32 pe;
while (bMore==1)
{
IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
Marshal.StructureToPtr(pe32, temp, true);
pe = (PROCESSENTRY32)Marshal.PtrToStructure(temp, typeof(PROCESSENTRY32));
Marshal.FreeHGlobal(temp);
if (pe32.szExeFile.ToString() == "TPMofWinCE.exe")
{
profilename = true;
}
bMore = Process32Next(handle, ref pe32);
}
}
CloseHandle(handle);
if (profilename)
{
MessageBox.Show("程序已启动");
System.Windows.Forms.Application.Exit();
}
else
{
System.Windows.Forms.Application.Run(new frmBegin());
}
}
}
第三种:第三方的DLL来操作。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OpenNETCF.Threading;
namespace TPMofWinCE
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
bool firstInstance;
OpenNETCF.Threading.NamedMutex mutex = new OpenNETCF.Threading.NamedMutex(false, "{50A9DA83-979B-4b6c-A095-DD1880F2B181}", out firstInstance);
if (!firstInstance)
{
MessageBox.Show("程序已启动");
return;
}
Application.Run(new frmBegin());
GC.KeepAlive(mutex);
}
}
}