#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="EnumProcess98";
HWND hWndMain;

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
		  ,LPSTR lpszCmdParam,int nCmdShow)
{
	HWND hWnd;
	MSG Message;
	WNDCLASS WndClass;
	g_hInst=hInstance;
	
	WndClass.cbClsExtra=0;
	WndClass.cbWndExtra=0;
	WndClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
	WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	WndClass.hInstance=hInstance;
	WndClass.lpfnWndProc=(WNDPROC)WndProc;
	WndClass.lpszClassName=lpszClass;
	WndClass.lpszMenuName=NULL;
	WndClass.style=CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&WndClass);

	hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
		NULL,(HMENU)NULL,hInstance,NULL);
	ShowWindow(hWnd,nCmdShow);
	hWndMain=hWnd;
	
	while(GetMessage(&Message,0,0,0)) {
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}
	return Message.wParam;
}

#include "tlhelp32.h"

#define ID_LISTPRO 100
#define ID_BTNPROLIST2 106
#define ID_BTNPROLISTKILL2 108
HWND hListPro, hListPro2;

int GetPID2()
{
	char str[256];
	char pid[10];
	int i, idx;

	idx=SendMessage(hListPro2,LB_GETCURSEL,0,0);
	if (idx == LB_ERR)
		return -1;
	SendMessage(hListPro2,LB_GETTEXT,idx,(LPARAM)str);
	for (i=0;str[i]!=' ';i++) 
		pid[i]=str[i];
	pid[i+1]=0;
	return atoi(pid);
}

// 실행중인 응용프로그램 구하기2
BOOL CALLBACK EnumWindowsProc2(HWND hwnd, LPARAM lp)
{
	DWORD process_id; 
	DWORD thread_id = GetWindowThreadProcessId(hwnd, &process_id);
	char str[256];
	char tmp[256];
	SendMessage(hListPro,LB_RESETCONTENT,0,0);
	LONG lnStyle, lnExStyle; 
	GetWindowText(hwnd, str, sizeof(str)); 
	lnStyle   = GetWindowLong(hwnd, GWL_STYLE); 
	lnExStyle = GetWindowLong(hwnd, GWL_EXSTYLE); 
	if((lnStyle & WS_VISIBLE) && (lnStyle & WS_OVERLAPPEDWINDOW) && 
		!(lnExStyle & WS_EX_CONTROLPARENT))
	{
		wsprintf(tmp, "%d %s ", thread_id, str);
		SendMessage(hListPro2, LB_ADDSTRING, 0, (LPARAM)tmp);
	}
	return(TRUE);
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	int pid2;

	switch(iMessage) {
	case WM_CREATE:
		hListPro2=CreateWindow("listbox",NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |
			LBS_NOTIFY,10,100,200,600,hWnd,(HMENU)ID_LISTPRO,g_hInst,NULL);
		CreateWindow("button","응용프로그램 목록",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
			300,20,120,25,hWnd,(HMENU)ID_BTNPROLIST2,g_hInst,NULL);
		CreateWindow("button","응용프로그램 종료",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
			440,20,120,25,hWnd,(HMENU)ID_BTNPROLISTKILL2,g_hInst,NULL);
		return 0;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case ID_BTNPROLIST2:
			SendMessage(hListPro2,LB_RESETCONTENT,0,0);
			EnumWindows(EnumWindowsProc2,NULL);
			break;
		case ID_BTNPROLISTKILL2:
			pid2=GetPID2();
			if (pid2 != -1)
			{
				HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid2);
				TerminateProcess(hProcess, 0);
				SendMessage(hListPro2,LB_RESETCONTENT,0,0);
				EnumWindows(EnumWindowsProc2,NULL);
				CloseHandle (hProcess);
			}
			break;
		}
		return 0;
	case WM_PAINT:
		hdc=BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
