C/C ++ provides a simple interface program to send the message source code to a specified window.

Source: Internet
Author: User

 # Include "windows. H "<br/> # include" psapi. H "<br/> # include" resource. H "<br/> # define classname" mingyusoftware "<br/> // set the psapi. lib added in <br/> # pragma comment (Lib, "psapi. lib ") <br/> # define title "process-window message sender" <br/> # define maxprocess 100 // defines the maximum number of processes enumerated <br/> # define maxmodule 100 // define the maximum number of processes in the enumeration <br/> // the size and position of the Main Window <br/> # define mainwindowleft 250 // Main Window (0, 0) coordinate distance to the left of the screen <br/> # define mainwindowtop 100 // Main Window (0, 0) the distance from the left to the top of the screen <br/> # define main1_wwide 800 // The width of the Main Window <br/> # define main1_wheight 600 // The height of the Main Window <br/> // confirm the button size and position (relative) <br/> # define okbuttonleft (330) // (0, 0) points are 120 away from the right boundary of the window <br/> # define okbuttontop 490 // 100 away from the top of the window <br/> # define okbuttonwide 115 // The button length is 100 <br/> # define okbuttonheight 35 // The button width is 35 <br/> // the size and position of the edit box (relative) <br/> # define editleft 100 // (120) points are 400 away from the right boundary of the window <br/> # define edittop 100 // away from the top of the window <br/> # d Efine editwide 600 // The button length is 100 <br/> # define editheight 20 // The button width is 35 <br/> // The size position (relative) of list box 1) <br/> # define listbox1left 35 // (0, 0) points are 120 away from the right boundary of the window <br/> # define listbox1top 25 // 100 away from the top of the window <br/> # define listbox1wide 350 // The button length is 100 <br/> # define listbox1height 350 // The button width is 35 <br/> // The size position (relative) of list box 2) <br/> # define listbox2left 400) 120 <br/> # define listbox2top 25 // 100 from the top of the window <br/> # define listbox2wide 350 // Press The button length is 100 <br/> # define listbox2height 350 // The button width is 35 <br/> // define id <br/> # define okbuttonid 1 // confirm the button ID <br/> # define edit1id 2 // ID of the edit box <br/> # define listbox1id 3 // ID of the Left list <br/> # define listbox2id 4 // right list box <br/> // define the clock id <br/> # define timerid 0 <br/> // The handle declaration of the window <br/> hwnd; // handle of the Main Window <br/> hwnd hbutton; // handle of the OK button <br/> hwnd hedit; // handle of the edit box <br/> hwnd hlistbox1; // handle of the Left list box <br/> hwnd hlistbox2; // handle with the list box <br/> // function declaration <B R/> lresult callback windowproc (hwnd, uint umsg, wparam, lparam); // message processing function <br/> boolenumcurrentprocess (); // enumerate all processes and modules <br/> bool getmodule (DWORD); <br/> bool adjusttoken (); // because of my computer room win7 system, if the permission is not elevated, opening the process will fail, so the function for permission escalation is added here <br/> bool callback enumwindowsproc (hwnd, lparam ); // process of window enumeration Program <br/> bool onbuttonclick1 (); // event handling when a button is pressed <br/> // global variable declaration <br/> DWORD dwprocess [maxprocess]; // Define the maximum number of processes <br/> DWORD dwpro [maxprocess]; <br/> hmodule [maxmodule]; // define the maximum number of modules <br/> char * STR [3] = {"Usage: send a message to the selected process in the left list (if there is a main window )"}, {"Format: MSG, wparam, lparam"}, {"such as sendmessaeg (hwnd, wm_close, null, null) in sendmessage (hwnd, MSG, wparam, lparam) you can enter "16, 0, 0", where 16 is the value represented by the macro wm_close "}}; <br/> char * STRs = "double-click a process or a process module to locate the file"; <br/> int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, Int ncmdshow) <br/>{< br/> MSG; <br/> wndclassex CLS = {sizeof (wndclassex) }; <br/> Cls. style = cs_hredraw | cs_vredraw; <br/> Cls. lpfnwndproc = windowproc; <br/> Cls. cbclsextra = 0; <br/> Cls. cbwndextra = 0; <br/> Cls. hinstance = hinstance; <br/> Cls. hicon = loadicon (hinstance, makeintresource (idi_icon1); <br/> Cls. hcursor = loadcursor (null, idc_arrow); <br/> Cls. hbrbackground = (hbrush) color_window; <br/> Cls. lpszclassname = Classname; <br/> Cls. lpszmenuname = makeintresource (idr_menu1); <br/> Cls. hiconsm = NULL; <br/> // register the window class <br/> atom dwreturn = registerclassex (& CLs); <br/> If (! Dwreturn) {MessageBox (null, "failed to register window class", "message center", mb_ OK | mb_iconerror); Return 0 ;} // handle window registration failure <br/> hwnd = createwindow (classname, title, ws_overlappedwindow | ws_visible, mainwindowleft, mainwindowtop, main1_wwide, main1_wheight, null, null, hinstance, null ); <br/> If (! Hwnd) {MessageBox (null, "window creation error", "message center", mb_ OK | mb_iconerror); Return 0 ;} // failed to create window <br/> showwindow (hwnd, sw_show); <br/> updatewindow (hwnd ); <br/> // Privilege Escalation <br/> adjusttoken (); <br/> enumcurrentprocess (); <br/> // settimer (hwnd, timerid, 5000, null); // trigger a clock once every 5 seconds. If the last parameter is null, send the wm_timer message <br/> while (getmessage (& MSG, 0, null, null )) <br/>{< br/> translatemessage (& MSG); <br/> dispatchmessage (& MSG); <br/>}< br/> lresult Callback windowproc (hwnd, uint umsg, wparam, lparam) <br/>{< br/> switch (umsg) <br/>{< br/> case wm_close: <br/> postquitmessage (0); <br/> break; <br/> case wm_timer: <br/> enumcurrentprocess (); <br/> break; <br/> case wm_paint: <br/>{< br/> HDC = getdc (hwnd); <br/> textout (HDC, 40, 5, "process list ", 8); <br/> textout (HDC, 40,368, "module list", 8); <br/> textout (HDC, STRs, strlen (STRs )); <br/> textout (HDC, 1 90,420, STR [0], strlen (STR [0]); <br/> textout (HDC, 130,440, STR [1], strlen (STR [1]); <br/> textout (HDC, 60,460, STR [2], strlen (STR [2]); <br/> releasedc (hwnd, HDC ); <br/> defwindowproc (hwnd, umsg, wparam, lparam); <br/>}< br/> break; <br/> case wm_keydown: <br/> If (wparam = vk_return) onbuttonclick1 (); <br/> break; </P> <p> case wm_create: // message sent when the window is created <br/>{< br/> // create a button <br/> hbutton = createwindow ("button", "Send a message to the window ", Ws_visible | ws_child, okbuttonleft, okbuttontop, okbuttonwide, okbuttonheight, hwnd, (hmenu) okbuttonid, null, null); <br/> If (! Hbutton) {MessageBox (null, "failed to create button", "message center", mb_ OK | mb_iconerror) ;}< br/> else showwindow (hbutton, sw_show ); <br/> // create the edit box <br/> hedit = createwindow ("edit", null, ws_visible | ws_child | ws_border, editleft, edittop, editwide, editheight, hwnd, (hmenu) edit1id, null, null); <br/> If (! Hbutton) {MessageBox (null, "failed to create button", "message center", mb_ OK | mb_iconerror) ;}< br/> else showwindow (hbutton, sw_show ); <br/> // create ListBox 1 <br/> hlistbox1 = createwindow ("ListBox", null, ws_vscroll | lbs_policy | ws_visible | ws_child | ws_border, listbox1left, listbox1top, listbox1wide, listbox1height, hwnd, (hmenu) listbox1id, null, null); <br/> If (! Hlistbox1) {MessageBox (null, "failed to create button", "message center", mb_ OK | mb_iconerror) ;}< br/> else showwindow (hlistbox1, sw_show ); <br/> // create list box 2 <br/> hlistbox2 = createwindow ("ListBox", null, ws_vscroll | lbs_policy | ws_visible | ws_child | ws_border, listbox2left, listbox2top, listbox2wide, listbox2height, hwnd, (hmenu) listbox2id, null, null); <br/> If (! Hlistbox2) {MessageBox (null, "failed to create button", "message center", mb_ OK | mb_iconerror) ;}< br/> else showwindow (hlistbox2, sw_show ); <br/> // set focus <br/> setfocus (hedit); <br/>}< br/> break; <br/> case wm_command: <br/>{< br/> word hword = hiword (wparam); <br/> word lword = loword (wparam); <br/> switch (lword) <br/>{< br/> case id_menuitem40001: <br/> postquitmessage (0); <br/> break; <br/> case id_menuitem40002: <br/> shellabout (hwnd, title, "Ben The program code is open-source and made by mingyu software studio. The code is pure API. QQ: 362625947,421302939 ", null); <br/> break; <br/> case okbuttonid: // In this case, it cannot be determined whether it is being double-clicked or not, but it is sufficient for the button <br/> onbuttonclick1 (); <br/> break; <br/> case id_menuitem40003: <br/> enumcurrentprocess (); <br/> break; <br/> case listbox1id: // The operation of the first ListBox <br/> switch (hword) <br/>{< br/> case lbn_selchange: // click Change Operation <br/>{< br/> int sel = sendmessage (hlistbox1, lb_getcursel ); <br/> getmodule (dwpro [sel]); // select the corresponding handle based on the current selection for operation <Br/>}< br/> break; <br/> case lbn_dblclk: // double-click <br/> char strpath [512] = {0 }; // accept <br/> char path [512] = {0}; <br/> int sel = sendmessage (hlistbox1, lb_getcursel ); <br/> sendmessage (hlistbox1, lb_gettext, Sel, (lparam) strpath); <br/> strcat (path, "/select ,"); <br/> strcat (path, strpath); <br/> ShellExecute (hwnd, "open", "assumer.exe", path, null, sw_shownormal); <br/> break; </P> <p >}< br/> break; <br/> case listbox2i D: // actions on the first list box <br/> switch (hword) <br/>{< br/> case lbn_dblclk: // double-click the operation <br/> char strpath [512] = {0}; // accept <br/> char path [512] = {0 }; <br/> int sel = sendmessage (hlistbox2, lb_getcursel, 0, 0); <br/> sendmessage (hlistbox2, lb_gettext, Sel, (lparam) strpath ); <br/> strcat (path, "/select,"); <br/> strcat (path, strpath); <br/> ShellExecute (hwnd, "open ", "assumer.exe", path, null, sw_shownormal); <br/> break; <br/>}< br/> break; </P> <P >}</P> <p >}< br/> break; </P> <p> default: <br/> return defwindowproc (hwnd, umsg, wparam, lparam); <br/>}< br/> return 0; <br/>}< br/> bool enumcurrentprocess () <br/>{< br/> DWORD cbneeded; <br/> handle hproc = 0; <br/> DWORD cbmod = 0; <br/> int num = 0; <br/> char buff [512] = {0}; <br/> int I = 0; // define I here to prevent repeated definitions in vs2005 <br/> Int J = 0; <br/> sendmessage (hlistbox1, lb_resetcontent, 0, 0 ); <br/> bool bsuccess = enumprocesses (Dwprocess, maxprocess * 4, & cbneeded); // here, cbneeded returns the number of bytes. The specific number of processes must be cbneeded/4 <br/> If (! Bsuccess) return false; <br/> for (I = 0; I <cbneeded/4; I ++) <br/>{< br/> hproc = OpenProcess (process_all_access, false, dwprocess [I]); // open the target Process <br/> If (hproc = NULL) continue; <br/> bool Bok = enumprocessmodules (hproc, hmodule, maxmodule * 4, & cbmod); <br/> If (! Bok) continue; </P> <p> getmodulefilenameex (hproc, hmodule [0], buff, 512 ); // obtain the full path of the current Executable File <br/> dwpro [num] = dwprocess [I]; <br/> num ++; <br/> sendmessage (hlistbox1, lb_addstring, 0, (lparam) buff); <br/>}</P> <p >}< br/> bool adjusttoken () <br/>{ <br/> token_privileges tokenprivileges; <br/> bool Bret; <br/> handle htoken; </P> <p> lookupprivilegevalue (null, se_debug_name, & tokenprivileges. privileges [0]. luid); <br/> O Penprocesstoken (getcurrentprocess (), token_adjust_privileges, & htoken); </P> <p> tokenprivileges. privilegecount = 1; <br/> tokenprivileges. privileges [0]. attributes = se_privilege_enabled; </P> <p> Bret = adjusttokenprivileges (htoken, false, & tokenprivileges, 0, null, null ); </P> <p> closehandle (htoken); <br/> return Bret = true; <br/>}< br/> bool getmodule (dword id) <br/>{< br/> handle hproc = 0; <br/> dwor D cbmod = 0; <br/> char buff [512] = {0}; <br/> int I = 0; // define I here to prevent repeated definitions in vs2005 <br/> sendmessage (hlistbox2, lb_resetcontent,); <br/> hproc = OpenProcess (process_all_access, false, ID); // open the target Process <br/> If (hproc = NULL) return 0; <br/> bool Bok = enumprocessmodules (hproc, hmodule, maxmodule * 4, & cbmod); <br/> If (! Bok) return 0; <br/> for (I = 1; I <cbmod/4; I ++) // It starts from 1. Because 0 is the path of the executable file, it is unnecessary to list it again. <br/>{< br/> getmodulefilenameex (hproc, hmodule [I], buff, 512); // obtain the full path of the current Executable File <br/> sendmessage (hlistbox2, lb_addstring, 0, (lparam) buff ); <br/>}< br/> return 1; <br/>}< br/> bool onbuttonclick1 () <br/>{< br/> int sel =-1; // defines the currently selected object. The sendmessage function does not modify the SEL value if no selection is made. <br/> sel = sendmessage (hlistbox1, lb_getcursel ); <br/> If (SEL =-1) {messag EBox (null, "select a process and then operate", "warning", mb_ OK | mb_iconwarning); Return 0 ;}< br/> bool Bok = enumwindows (enumwindowsproc, (lparam) dwpro [sel]); <br/> If (! Bok) {MessageBox (null, "messages cannot be sent to the window", "warning", mb_ OK | mb_iconwarning); Return 0 ;}< br/> return 1; <br/>}< br/> bool callback enumwindowsproc (hwnd, lparam) <br/>{< br/> DWORD dwprocid = 0; <br/> char buff [128] = {0}; // content of the edit box <br/> char temp [16] = {0 }; // accept temporary numeric characters <br/> char * curr = 0; <br/> char * temp1 = 0; <br/> int I = 0; <br/> DWORD pa [3]; <br/> getwindowthreadprocessid (hwnd, & dwprocid); // the return value is the thread ID. <br/> hwnd Han = fin Dwindow (null, "cmd.txt-Notepad"); <br/> If (DWORD) lparam = dwprocid) // if found, <br/>{< br/> getwindowtext (hedit, buff, 128); <br/> If (! Buff [0]) return 0; <br/> curr = Buff; temp1 = temp; <br/> for (I = 0; I <3; I ++) <br/>{< br/> while (* curr! = ',' & * Curr! = 0) // when no "," is encountered <br/>{< br/> * temp1 = * curr; temp1 ++; curr ++; <br/>}< br/> pa [I] = atoi (temp); <br/> zeromemory (temp, 16 ); // set the temporary string block to zero <br/> curr ++; temp1 = temp; <br/>}< br/> sendmessage (hwnd, pa [0], pa [1], pa [2]); <br/> return 1; <br/>}< br/> return 1; </P> <p >}< br/> 

 

 

For the compiled result, see my resources:

Http://download.csdn.net/source/1533342

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.