-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxBupProcessList.cpp
194 lines (184 loc) · 5.29 KB
/
wxBupProcessList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*-----------------------------------------------------------------
* Name: wxBupProcessList.cpp
* Purpose:
* Author: A. Wiegert
*
* Copyright:
* Licence: wxWidgets licence
*---------------------------------------------------------------- */
/*----------------------------------------------------------------
* Standard wxWidgets headers
*---------------------------------------------------------------- */
// Note __VISUALC__ is defined by wxWidgets, not by MSVC IDE
// and thus won't be defined until some wxWidgets headers are included
#if defined( _MSC_VER )
# if defined ( _DEBUG )
// this statement NEEDS to go BEFORE all headers
# define _CRTDBG_MAP_ALLOC
# endif
#endif
#include "wxBupPreProcDefsh.h" // needs to be first
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
/* For all others, include the necessary headers
* (this file is usually all you need because it
* includes almost all "standard" wxWidgets headers) */
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// ------------------------------------------------------------------
#include "wxBupWizTreebookh.h"
#include <TlHelp32.h>
//??#include <VersionHelpers.h> // only available under MSVC 2015
// ------------------------------------------------------------------
#if defined( _MSC_VER ) // from Autohotkey-hummer.ahk
// this block needs to go AFTER all headers
#include <crtdbg.h>
# ifdef _DEBUG
# define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
# define new DEBUG_NEW
# endif
#endif
// ------------------------------------------------------------------
// from: https://forums.wxwidgets.org/viewtopic.php?t=20145&highlight=getprocesslist
// ------------------------------------------------------------------
// Implementation
void MyFrame::GetProcessList(std::vector<ProcessEntry> &proclist)
{
#ifdef __WXMSW__
#if 0
if (!IsWindowsXPOrGreater())
{
wxMessageBox( _("You need at least Windows XP") ); //, "Version Not Supported", MB_OK);
return;
}
#else
OSVERSIONINFO osver;
// this function has been deprecated
// Check to see if were running under Windows95 or
// Windows NT.
osver.dwOSVersionInfoSize = sizeof( osver ) ;
if ( !GetVersionEx( &osver ) )
{
return;
}
if ( osver.dwPlatformId != VER_PLATFORM_WIN32_NT )
{
return;
}
#endif
//get child processes of this node
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (!hProcessSnap)
{
return;
}
//Fill in the size of the structure before using it.
PROCESSENTRY32 pe;
memset(&pe, 0, sizeof( pe ) );
pe.dwSize = sizeof( PROCESSENTRY32 );
// Walk the snapshot of the processes, and for each process,
// kill it if its parent is pid.
if (!Process32First( hProcessSnap, &pe) )
{
// Can't get first process.
CloseHandle (hProcessSnap);
return;
}
do
{
ProcessEntry entry;
entry.name = pe.szExeFile;
entry.pid = (long)pe.th32ProcessID;
proclist.push_back(entry);
} while ( Process32Next( hProcessSnap, &pe) );
CloseHandle( hProcessSnap );
#else
//GTK and other
wxArrayString output;
#if defined (__WXGTK__)
ExecuteCommand(wxT("ps -A -o pid,command --no-heading"), output);
#elif defined (__WXMAC__)
// Mac does not like the --no-heading...
ExecuteCommand(wxT("ps -A -o pid,command "), output);
#endif
for (size_t i=0; i< output.GetCount(); i++)
{
wxString line = output.Item(i);
//remove whitespaces
line = line.Trim().Trim(false);
//get the process ID
ProcessEntry entry;
wxString spid = line.BeforeFirst(wxT(' '));
spid.ToLong( &entry.pid );
entry.name = line.AfterFirst(wxT(' '));
if (entry.pid == 0 && i > 0)
{
//probably this line belongs to the provious one
ProcessEntry e = proclist.back();
proclist.pop_back();
e.name << entry.name;
proclist.push_back( e );
}
else
{
proclist.push_back( entry );
}
}
#endif
}
// ------------------------------------------------------------------
#if 0
// Implementation
void MyFrame::ExecuteCommand(const wxString &command, wxArrayString &output, long flags)
{
#ifdef __WXMSW__
wxExecute(command, output, flags);
#else
FILE *fp;
char line[512];
memset(line, 0, sizeof(line));
fp = popen(command.mb_str(wxConvUTF8), "r");
while ( fgets( line, sizeof line, fp)) {
output.Add(wxString(line, wxConvUTF8));
memset(line, 0, sizeof(line));
}
pclose(fp);
#endif
}
#endif
// ------------------------------------------------------------------
void MyFrame::DisplayProcessList()
{
#if 1
wxString wsName;
std::vector<ProcessEntry> proclist;
GetProcessList( proclist );
int iSize = proclist.size();
ProcessEntry pe;
bool bFound = false;
int iNClients = 0;
wxString wsTT;
for ( unsigned int i = 0; i < proclist.size(); i++ )
{
pe = proclist.at( i );
if( pe.name.IsSameAs( _T("thunderbird.exe") ) )
{
bFound = true;
iNClients++;
wxLogWarning( _("ThunderBird is running\n") );
}
if( pe.name.IsSameAs( _T("MailClient.exe") ) )
{
bFound = true;
iNClients++;
wxLogWarning( _("eM-Client is running\n") );
}
}
wxLogMessage( _("%d e-mail client(s) running"), iNClients );
#endif
}
// ------------------------------- eof ------------------------------