1+ using System ;
2+ using System . Diagnostics ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Runtime . InteropServices ;
6+ using System . Threading ;
7+
8+ namespace EverythingToolbar . Helpers
9+ {
10+ public static class EverythingProcessHelper
11+ {
12+ private const string Dll64 = "Everything64.dll" ;
13+
14+ [ DllImport ( Dll64 , EntryPoint = "Everything_QueryW" , CharSet = CharSet . Unicode ) ]
15+ private static extern bool Everything64_QueryW ( bool wait ) ;
16+
17+ [ DllImport ( Dll64 , EntryPoint = "Everything_IsDBLoaded" ) ]
18+ private static extern bool Everything64_IsDBLoaded ( ) ;
19+
20+ [ DllImport ( Dll64 , EntryPoint = "Everything_GetLastError" ) ]
21+ private static extern uint Everything64_GetLastError ( ) ;
22+
23+ /// <summary>
24+ /// Checks if Everything is running and DB is loaded.
25+ /// </summary>
26+ public static bool IsRunning ( )
27+ {
28+ try
29+ {
30+ // Try a dummy query (empty string = no results)
31+ if ( ! Everything64_QueryW ( false ) )
32+ return false ;
33+
34+ return Everything64_IsDBLoaded ( ) ;
35+ }
36+ catch
37+ {
38+ return false ;
39+ }
40+ }
41+
42+ /// <summary>
43+ /// Ensures Everything is running.
44+ /// If not, starts Everything.exe in background with -startup.
45+ /// Waits until DB is loaded or timeout expires.
46+ /// </summary>
47+ public static bool EnsureRunning ( int timeoutMs = 5000 )
48+ {
49+ if ( IsRunning ( ) )
50+ {
51+ return true ;
52+ }
53+
54+ string [ ] possiblePaths =
55+ {
56+ ToolbarSettings . User . EverythingPath ,
57+ @"C:\Program Files\Everything\Everything.exe" ,
58+ @"C:\Program Files (x86)\Everything\Everything.exe" ,
59+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "Everything\\ Everything.exe" ) ,
60+ } ;
61+
62+ var everythingPath = possiblePaths . FirstOrDefault ( p => File . Exists ( p ) ) ;
63+ if ( everythingPath == null )
64+ {
65+ return false ;
66+ }
67+
68+ try
69+ {
70+ Process . Start ( new ProcessStartInfo
71+ {
72+ FileName = everythingPath ,
73+ Arguments = "-startup -first-instance" ,
74+ UseShellExecute = false ,
75+ CreateNoWindow = true
76+ } ) ;
77+
78+ var sw = Stopwatch . StartNew ( ) ;
79+ while ( sw . ElapsedMilliseconds < timeoutMs )
80+ {
81+ if ( IsRunning ( ) )
82+ return true ;
83+
84+ Thread . Sleep ( millisecondsTimeout : 10 ) ;
85+ }
86+ }
87+ catch ( Exception )
88+ {
89+ // don't block. User can start Everything on their own as well.
90+ }
91+
92+ return false ;
93+ }
94+ }
95+ }
0 commit comments