source: dev/trunk/ab5.0/abdev/ProjectEditor/Attach.cpp@ 477

Last change on this file since 477 was 477, checked in by dai_9181, 17 years ago

構成管理を大幅に改良。

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