#include "windows.h" #include "Psapi.h" #include "resource.h" #define ClassName "panweiguo" //将Psapi.lib添加进来 #pragma comment(lib,"Psapi.lib") #define Title "窗口应用程序" #define MAXPROCESS 100 //定义枚举的最大进程数目为多少 #define MAXMODULE 100 //定义枚举的最大进程数目为多少 //主窗口窗口大小位置 #define MainWindowLeft 250 //主窗口(0,0)坐标距离屏幕左边距离 #define MainWindowTop 100 //主窗口(0,0)左边距离屏幕顶部距离 #define MainWindowWide 800 //主窗口的宽度 #define MainWindowHeight 600 //主窗口的高度 //确定按钮的大小位置(相对) #define OkButtonLeft (350)//(0,0)点距离窗口右边界120 #define OkButtonTop 460 //距离窗口顶部100 #define OkButtonWide 115 //按钮长度为100 #define OkButtonHeight 35 //按钮宽度为35 //编辑框的大小位置(相对) #define EditLeft (100)//(0,0)点距离窗口右边界120 #define EditTop 400 //距离窗口顶部100 #define EditWide 600 //按钮长度为100 #define EditHeight 20 //按钮宽度为35 //列表框1的大小位置(相对) #define ListBox1Left 35//(0,0)点距离窗口右边界120 #define ListBox1Top 25 //距离窗口顶部100 #define ListBox1Wide 350 //按钮长度为100 #define ListBox1Height 350 //按钮宽度为35 //列表框2的大小位置(相对) #define ListBox2Left 400//(0,0)点距离窗口右边界120 #define ListBox2Top 25 //距离窗口顶部100 #define ListBox2Wide 350 //按钮长度为100 #define ListBox2Height 350 //按钮宽度为35 //定义ID #define OkButtonId 1//确定按钮的ID #define Edit1Id 2//编辑框的ID #define ListBox1Id 3//左列表框的ID #define ListBox2Id 4//右列表框的ID //定义时钟ID #define TimerId 0 //窗口的句柄声明 HWND hwnd;//主窗口的句柄 HWND hButton;//Ok按钮的句柄 HWND hEdit;//编辑框的句柄 HWND hListBox1;//左列表框的句柄 HWND hListBox2;//有列表框的句柄 //函数声明 LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);//消息处理函数 bool EnumCurrentPorcess();//枚举所有的进程和模块 bool GetModule(DWORD); bool AdjustToken();//由于我的计算机室win7系统,如果不提升权限的话,打开进程是要失败的,所以这里添加了提升权限的函数 //全局变量申明 DWORD dwProcess[MAXPROCESS];//定义最大的进程数目 HMODULE hModule[MAXMODULE];//定义最大模块数目 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { MSG msg; WNDCLASSEX cls={sizeof(WNDCLASSEX)}; cls.style=CS_HREDRAW|CS_VREDRAW; cls.lpfnWndProc=WindowProc; cls.cbClsExtra=0; cls.cbWndExtra=0; cls.hInstance=hInstance; cls.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1)); cls.hCursor=LoadCursor(NULL,IDC_ARROW); cls.hbrBackground=(HBRUSH)COLOR_WINDOW; cls.lpszClassName=ClassName; cls.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1); cls.hIconSm=NULL; //注册窗口类 ATOM dwReturn=RegisterClassEx(&cls); if (!dwReturn){MessageBox(NULL,"注册窗口类的时候失败","消息中心",MB_OK|MB_ICONERROR);return 0;}//窗口注册失败处理 hwnd=CreateWindow(ClassName,Title,WS_OVERLAPPEDWINDOW|WS_VISIBLE,MainWindowLeft,MainWindowTop,MainWindowWide,MainWindowHeight,NULL,NULL,hInstance,NULL); if (!hwnd){MessageBox(NULL,"创建窗口时错误","消息中心",MB_OK|MB_ICONERROR);return 0;}//创建窗口时失败 ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); //提升特权 AdjustToken(); EnumCurrentPorcess(); //SetTimer(hwnd,TimerId,5000,NULL);//5秒促发一次时钟,最后一个参数为NULL,发送WM_TIMER消息 while(GetMessage(&msg,0,NULL,NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } } LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_CLOSE: PostQuitMessage(0); break; case WM_TIMER: EnumCurrentPorcess(); break; case WM_CREATE://窗口创建时发出的消息 //创建Button按钮 hButton=CreateWindow("button","向窗口发送消息",WS_VISIBLE|WS_CHILD,OkButtonLeft,OkButtonTop,OkButtonWide,OkButtonHeight,hwnd,(HMENU)OkButtonId,NULL,NULL); if (!hButton){MessageBox(NULL,"创建按钮时失败","消息中心",MB_OK|MB_ICONERROR);} else ShowWindow(hButton,SW_SHOW); //创建Edit编辑框 hEdit=CreateWindow("edit",NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,EditLeft,EditTop,EditWide,EditHeight,hwnd,(HMENU)Edit1Id,NULL,NULL); if (!hButton){MessageBox(NULL,"创建按钮时失败","消息中心",MB_OK|MB_ICONERROR);} else ShowWindow(hButton,SW_SHOW); //创建列表框1 hListBox1=CreateWindow("listbox",NULL,WS_VSCROLL|LBS_NOTIFY|WS_VISIBLE|WS_CHILD|WS_BORDER,ListBox1Left,ListBox1Top,ListBox1Wide,ListBox1Height,hwnd,(HMENU)ListBox1Id,NULL,NULL); if (!hListBox1){MessageBox(NULL,"创建按钮时失败","消息中心",MB_OK|MB_ICONERROR);} else ShowWindow(hListBox1,SW_SHOW); //创建列表框2 hListBox2=CreateWindow("listbox",NULL,WS_VSCROLL|LBS_NOTIFY|WS_VISIBLE|WS_CHILD|WS_BORDER,ListBox2Left,ListBox2Top,ListBox2Wide,ListBox2Height,hwnd,(HMENU)ListBox1Id,NULL,NULL); if (!hListBox2){MessageBox(NULL,"创建按钮时失败","消息中心",MB_OK|MB_ICONERROR);} else ShowWindow(hListBox2,SW_SHOW); break; case WM_COMMAND: { WORD hWord=HIWORD(wParam); WORD lWord=LOWORD(wParam); if (lParam==(LPARAM)hListBox1&&hWord==LBN_SELCHANGE)//listbox1发生改变现象 { int sel=SendMessage(hListBox1,LB_GETCURSEL,0,0); GetModule(dwProcess[sel]);//根据当前的选择选择相应的handle进行操作 } switch(lWord) { case ID_MENUITEM40001: PostQuitMessage(0); break; case ID_MENUITEM40002: ShellAbout(hwnd,"C/C++学习","本程序",NULL); break; case OkButtonId://这样写的话判断不出来是否是在双击等等但是对于按钮来说足够了 EnumCurrentPorcess(); char buff[100]={0}; GetWindowText(hEdit,buff,100); if (buff[0]) { //ListBox是通过消息进行控件的添加和删除操作的 SendMessage(hListBox1,LB_ADDSTRING,0,(LPARAM)buff); } break; } } break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } bool EnumCurrentPorcess() { DWORD cbNeeded; HANDLE hProc=0; DWORD cbMod=0; char buff[512]={0}; int i=0;//在此处定义i防止在VS2005中出现重复定义现象 int j=0; SendMessage(hListBox1,LB_RESETCONTENT,0,0); BOOL bSuccess=EnumProcesses(dwProcess,MAXPROCESS*4,&cbNeeded);//这里cbNeeded返回的是字节数目,具体的进程数目是要cbNeeded/4的 if (!bSuccess) return false; for (i=0;i<cbNeeded/4;i++) { hProc=OpenProcess(PROCESS_ALL_ACCESS,false,dwProcess[i]);//打开目标进程 if (hProc==NULL)continue; BOOL bOk=EnumProcessModules(hProc,hModule,MAXMODULE*4,&cbMod); if (!bOk) continue; GetModuleFileNameEx(hProc,hModule[0],buff,512);//获取当前可执行文件的全路径 SendMessage(hListBox1,LB_ADDSTRING,0,(LPARAM)buff); } } bool AdjustToken() { TOKEN_PRIVILEGES TokenPrivileges; BOOL bRet; HANDLE hToken; LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &TokenPrivileges.Privileges[0].Luid); OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken); TokenPrivileges.PrivilegeCount = 1; TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; bRet = AdjustTokenPrivileges(hToken, FALSE, &TokenPrivileges, 0, NULL, NULL); CloseHandle(hToken); return bRet == TRUE; } bool GetModule(DWORD id) { HANDLE hProc=0; DWORD cbMod=0; char buff[512]={0}; int i=0;//在此处定义i防止在VS2005中出现重复定义现象 SendMessage(hListBox2,LB_RESETCONTENT,0,0); hProc=OpenProcess(PROCESS_ALL_ACCESS,false,id);//打开目标进程 if (hProc==NULL)return 0; BOOL bOk=EnumProcessModules(hProc,hModule,MAXMODULE*4,&cbMod); if (!bOk) return 0; for (i=1;i<cbMod/4;i++)//这里从1开始因为0是可执行文件的路径没有必要再列出来了 { GetModuleFileNameEx(hProc,hModule[i],buff,512);//获取当前可执行文件的全路径 SendMessage(hListBox2,LB_ADDSTRING,0,(LPARAM)buff); } return 1; }