source: dev/ProjectEditor/Attach.cpp@ 139

Last change on this file since 139 was 3, checked in by dai_9181, 18 years ago
File size: 8.1 KB
Line 
1#ifndef THETEXT
2
3#include "common.h"
4#include <psapi.h>
5#include <tlhelp32.h>
6
7#pragma comment(lib, "psapi.lib")
8
9
10int GetSelectingProcessId(HWND hListView,DWORD *lpdwPlatform){
11 int nCount;
12 nCount=ListView_GetItemCount(hListView);
13
14 char temporary[255];
15 DWORD dwProcessId;
16 int i;
17 for(i=0;i<nCount;i++){
18 if(ListView_GetItemState(hListView,i,LVIS_SELECTED)){
19 ListView_GetItemText(hListView,i,1,temporary,255);
20 dwProcessId=atoi(temporary);
21
22 ListView_GetItemText(hListView,i,2,temporary,255);
23 if(lstrcmpi(temporary,"Win32")==0) *lpdwPlatform=IMAGE_FILE_MACHINE_I386;
24 else if(lstrcmpi(temporary,"Win64")==0) *lpdwPlatform=IMAGE_FILE_MACHINE_AMD64;
25 break;
26 }
27 }
28 if(i==nCount) return 0;
29
30 return dwProcessId;
31}
32
33void InsertProcess_ToListView(HWND hListView,char *lpszAppPath,int id,char *lpszPlatform,char *lpszTitle){
34 char szAppName[MAX_PATH],temp2[MAX_PATH];
35 _splitpath(lpszAppPath,NULL,NULL,szAppName,temp2);
36 lstrcat(szAppName,temp2);
37
38 //イメージリストを取得
39 HIMAGELIST hImageList;
40 hImageList=ListView_GetImageList(hListView,LVSIL_SMALL );
41
42 //アイコンを取得
43 int iImage;
44 SHFILEINFO shfi;
45 SHGetFileInfo(lpszAppPath, FILE_ATTRIBUTE_ARCHIVE, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON );
46 if(shfi.hIcon){
47 iImage=ImageList_AddIcon(hImageList,shfi.hIcon);
48 DestroyIcon(shfi.hIcon);
49 }
50 else iImage=-1;
51
52 char szId[255];
53 sprintf(szId,"%d",id);
54
55 //リストビューに追加
56 LV_ITEM item;
57 if(iImage==-1) item.mask=LVIF_TEXT;
58 else item.mask=LVIF_TEXT|LVIF_IMAGE;
59 item.iItem=0;
60
61 item.pszText=szAppName;
62 item.iSubItem=0;
63 item.iImage=iImage;
64 ListView_InsertItem(hListView,&item);
65
66 item.mask=LVIF_TEXT ;
67 item.pszText=szId;
68 item.iSubItem++;
69 ListView_SetItem(hListView,&item);
70
71 item.pszText=lpszPlatform;
72 item.iSubItem++;
73 ListView_SetItem(hListView,&item);
74
75 item.pszText=lpszTitle;
76 item.iSubItem++;
77 ListView_SetItem(hListView,&item);
78}
79
80void ListupProcesses64(HWND hListView){
81 char temporary[MAX_PATH];
82
83 ListView_DeleteAllItems(hListView);
84
85
86 /////////////////////////////////////////////////////
87 // 64ビット版プロセス列挙モジュールを呼び出す
88 /////////////////////////////////////////////////////
89
90 sprintf(temporary,"%sSubOperation\\enum_process64\\enum_process64.exe",pj_editor_Dir);
91
92 STARTUPINFO si;
93 PROCESS_INFORMATION pi;
94 memset(&si,0,sizeof(STARTUPINFO));
95 si.cb=sizeof(STARTUPINFO);
96 CreateProcess(NULL,temporary,NULL,NULL,0,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi);
97
98 WaitForSingleObject(pi.hProcess,INFINITE);
99
100
101 //////////////////////////////////
102 // 生成されたファイルの読み込み
103 //////////////////////////////////
104
105 sprintf(temporary,"%sSubOperation\\enum_process64\\list.dat",pj_editor_Dir);
106
107 char *buffer;
108 buffer=ReadBuffer_NonErrMsg(temporary);
109 if(!buffer) return;
110
111 int i=0;
112 while(1){
113 //ファイルパス
114 char szAppPath[MAX_PATH];
115 i=GetOneParameter(buffer,i,szAppPath);
116 if(temporary[0]=='\0') break;
117 RemoveStringQuotes(szAppPath);
118
119 //プロセスID
120 DWORD dwProcessId;
121 i=GetOneParameter(buffer,i,temporary);
122 if(temporary[0]=='\0') break;
123 dwProcessId=(DWORD)atoi(temporary);
124
125 //プラットフォーム
126 char szPlatform[255];
127 i=GetOneParameter(buffer,i,szPlatform);
128 if(temporary[0]=='\0') break;
129
130 //タイトル
131 char szTitle[255];
132 int i2;
133 for(i2=0;;i++,i2++){
134 if(IS_RETURN(buffer,i)||buffer[i]=='\0'){
135 szTitle[i2]=0;
136 break;
137 }
138 szTitle[i2]=buffer[i];
139 }
140
141
142 //リストビューへ追加
143 InsertProcess_ToListView(hListView,szAppPath,dwProcessId,szPlatform,szTitle);
144
145 if(buffer[i]=='\r'&&buffer[i+1]=='\n') i+=2;
146 if(buffer[i]=='\0') break;
147 }
148
149 HeapDefaultFree(buffer);
150}
151
152// EnumWindowsProcコールバック関数
153BOOL CALLBACK FindWindowProc(HWND hwnd, LPARAM lParam)
154{
155 HWND* pphWnd = (HWND*)lParam;
156 *pphWnd = hwnd;
157 return FALSE; /* 列挙を中断 */
158}
159void ListupProcesses(HWND hListView){
160 char temporary[MAX_PATH];
161
162 ListView_DeleteAllItems(hListView);
163
164 DWORD *pdwProcessId;
165 pdwProcessId=(DWORD *)HeapAlloc(hHeap,0,8192*sizeof(DWORD));
166
167 //プロセスを列挙
168 DWORD cbNeeded;
169 EnumProcesses(pdwProcessId,8192*sizeof(DWORD),&cbNeeded);
170
171 char szMyAppPath[MAX_PATH];
172 GetModuleFileName(GetModuleHandle(0),szMyAppPath,MAX_PATH);
173
174 int i;
175 for(i=0;i<(int)(cbNeeded/sizeof(DWORD));i++){
176
177 //プロセスIDを元にハンドルを取得
178 HANDLE hProcess;
179 hProcess=OpenProcess(PROCESS_ALL_ACCESS,0,pdwProcessId[i]);
180 if(!hProcess) continue;
181
182 //そのプロセスにおける実行モジュールのインスタンスハンドルを取得
183 HINSTANCE hModule;
184 DWORD cbReturned;
185 if(!EnumProcessModules( hProcess, &hModule, sizeof(HINSTANCE), &cbReturned ))
186 continue;
187
188 //実行ファイル名を取得
189 GetModuleFileNameEx(hProcess,hModule,temporary,MAX_PATH);
190
191
192 //////////////////////////////
193 // ウィンドウタイトルを取得
194 //////////////////////////////
195
196 //スナップショットを取得
197 HANDLE hSnapshot;
198 hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
199
200 THREADENTRY32 te;
201 memset(&te,0,sizeof(THREADENTRY32));
202 te.dwSize=sizeof(THREADENTRY32);
203 if(Thread32First(hSnapshot,&te)){
204 do{
205 if(te.th32OwnerProcessID==pdwProcessId[i]){
206 break;
207 }
208 }while(Thread32Next(hSnapshot,&te));
209 }
210
211 HWND hwnd;
212 hwnd=0;
213 EnumThreadWindows(te.th32ThreadID,FindWindowProc,(LPARAM)&hwnd);
214
215 while(GetParent(hwnd)) hwnd=GetParent(hwnd);
216
217 char szWndTitle[1024];
218 szWndTitle[0]=0;
219 if(hwnd) GetWindowText(hwnd,szWndTitle,1024);
220
221 CloseHandle(hSnapshot);
222
223
224
225 CloseHandle(hProcess);
226
227 if(lstrcmpi(szMyAppPath,temporary)==0){
228 //自分自身(ProjectEditor.exe)は無視
229 continue;
230 }
231
232 //リストビューへ追加
233 InsertProcess_ToListView(hListView,temporary,pdwProcessId[i],"Win32",szWndTitle);
234 }
235
236 HeapDefaultFree(pdwProcessId);
237}
238
239BOOL CALLBACK DlgAttach(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
240 static HWND hListView;
241 static HIMAGELIST hImageList;
242 static DWORD *lpdwPlatform;
243 switch(message){
244 case WM_INITDIALOG:
245 SetPosCenter(hwnd);
246
247 lpdwPlatform=(DWORD *)lParam;
248
249 hListView=GetDlgItem(hwnd,IDC_PROCESSLIST);
250
251 //拡張スタイル
252 DWORD dwStyle;
253 dwStyle=ListView_GetExtendedListViewStyle(hListView);
254 dwStyle|=LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
255 ListView_SetExtendedListViewStyle(hListView,dwStyle);
256
257
258 //イメージリストの生成と設定
259 hImageList=ImageList_Create(16,16,ILC_COLOR24|ILC_MASK,0,0);
260 ListView_SetImageList(hListView,hImageList,LVSIL_SMALL );
261
262
263 ///////////////////////////
264 // カラムをセット
265 ///////////////////////////
266
267 LV_COLUMN ListView_Column;
268 ListView_Column.mask=LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
269 ListView_Column.fmt=LVCFMT_LEFT;
270
271 ListView_Column.cx=150;
272 ListView_Column.pszText="プロセス名";
273 ListView_Column.iSubItem=0;
274 ListView_InsertColumn(hListView,ListView_Column.iSubItem,&ListView_Column);
275
276 ListView_Column.cx=70;
277 ListView_Column.pszText="ID";
278 ListView_Column.iSubItem++;
279 ListView_InsertColumn(hListView,ListView_Column.iSubItem,&ListView_Column);
280
281 ListView_Column.cx=50;
282 ListView_Column.pszText="種別";
283 ListView_Column.iSubItem++;
284 ListView_InsertColumn(hListView,ListView_Column.iSubItem,&ListView_Column);
285
286 ListView_Column.cx=200;
287 ListView_Column.pszText="タイトル";
288 ListView_Column.iSubItem++;
289 ListView_InsertColumn(hListView,ListView_Column.iSubItem,&ListView_Column);
290
291
292 //リスビューを更新
293 if(IsWow64()) ListupProcesses64(hListView);
294 else ListupProcesses(hListView);
295
296 break;
297 case WM_COMMAND:
298 switch(LOWORD(wParam)){
299 case IDOK:
300 EndDialog(hwnd,GetSelectingProcessId(hListView,lpdwPlatform));
301 return 1;
302 case IDCANCEL:
303 EndDialog(hwnd,0);
304 return 1;
305 }
306 return 0;
307
308 case WM_NOTIFY:
309 NMLISTVIEW *nmListView;
310 nmListView=(NMLISTVIEW *)lParam;
311 if(nmListView->hdr.hwndFrom==hListView){
312 if(nmListView->hdr.code==NM_DBLCLK){
313 if(nmListView->iItem==-1) return 1;
314 SendMessage(hwnd,WM_COMMAND,IDOK,0);
315 return 1;
316 }
317 }
318 break;
319
320 case WM_DESTROY:
321 ImageList_Destroy(hImageList);
322 return 1;
323 }
324 return 0;
325}
326
327
328#endif //THETEXT
Note: See TracBrowser for help on using the repository browser.