In winform, there are many methods to prevent the same program from running for multiple times, such as using findwindow, mutex, and C # To directly traverse through process, but the methods in winform won't work.
Findwindow cannot find the corresponding form handle. The total number of semaphores created by createmutex returns 87 (no matter how many identical applications are running). process has no relevant method.
After several hours of exploration, you can use process snapshots in CE to implement this function. The following is the process traversal function implemented by C ++ and C #.
The C ++ console application is
// Test2.cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <windows. h> # include "tlhelp32.h" # include "stdio. H "# pragma comment (Lib," toolhelp. lib ") // CE system class library int _ tmain (INT argc, _ tchar * argv []) {processentry32 pe32; pe32.dwsize = sizeof (pe32); handle hprocesssnap = :: createconlhelp32snapshot (th32cs_snapprocess, 0); If (hprocesssnap = invalid_handle_value) {printf ("createconlhelp32snapshot" Call failed. \ n "); Return-1;} bool bmore =: process32first (hprocesssnap, & pe32); While (bmore) {printf (" process name: % s \ n ", pe32.szexefile); printf ("process ID: % u \ n", pe32.th32processid); bmore =: process32next (hprocesssnap, & pe32) ;}: closehandle (hprocesssnap ); return 0 ;}
C #
Namespace smartdeviceproject1 {static class program {[dllimport ("toolhelp. DLL ")] public static extern intptr createconlhelp32snapshot (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 in T process32next (intptr handle, ref processentry32 PE); // <summary> // main entry point of the application. /// </Summary> [mtathread] Static void main () {intptr handle = createconlhelp32snapshot (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 identifier; Public uint th32moduleid; Public uint cntthreads; Public uint identifier; Public int identifier; public uint dwflags; [financialas (unmanagedtype. byvaltstr, sizeconst = 260)] // note that this parameter is the wide character Public String szexefile; Public uint th32memorybase; Public uint th32accesskey;} public Enum snapshotflags: uint {Signature = 0x00000001, th32cs_snapprocess = 0x00000002, th32cs_snapthread = 0x00000004, th32cs_snapmodule = 0x00000008, timeout = (Bytes | th32cs_snapprocess | th32cs_snapthread | th32cs_snapmodule), timeout = 0x80000000}
}