Inject DLL into a specific application using the hook function: Click different icons on the desktop to play the corresponding notes.

Source: Internet
Author: User

Recently, I checked the core programming and saw the DLL injection chapter. There is a desktop item positon saver(dipsexample. This example uses a window hook to inject a dllto the explorer.exe address space to save and restore the icon location.
So I want to rebuild it based on this example. Some time ago, on the Google homepage, Google made a very interesting DoCoMo to commemorate the 96 th Anniversary of the Birth of Les Paul, the father of the electric guitar, this Google electric guitar logo allows users to play the guitar on it, so they want to use the desktop icon to compile a similar program, click different icons, and generate different guitar sounds.
The following describes the general idea of programming:
The first step is to find the window handle of the desktop listview control. At the beginning, we encountered a problem.
The book on core programming describes a Windows shell window with a class of progman. This progman window has a subwindow with only one class of shelldll_defview. This subwindow also has only one subwindow. The subwindow category is syslistview32, And the syslistview32 window is the listview control window of the desktop.
So I used its code:
Hwnd hwndlv = getfirstchild (
Findwindow (text ("progman"), null )));
But I found that the operation was incorrect, so I used spy ++ to view it. I found that my syslistview32 window is located under the window where the class is workerw. Is this changed in win7? I don't know. Therefore, if you fail to run the source code after downloading it, you 'd better use Spy ++ to view the window information and see who the syslistview32 control is, if it is a subwindow of progman, you need to put the mousehookapp. in the CPP file
Hwnd hwndlv;
Enumwindows (enumwindowsproc, (lparam) & hwndlv );
And
Comment out the enumwindowsproc function,
Cancel hwnd hwndlv = getfirstchild (
Findwindow (text ("progman"), null); Comments of this Code.
I found that the window named workerw is not unique, so I cannot use findwindows. I switched to the enumwindows function.
Hwnd hwndlv;
Enumwindows (enumwindowsproc, (lparam) & hwndlv );

The enumwindowsproc function is implemented as follows:

Code: Bool callback enumwindowsproc (hwnd, lparam) {<br/> If (hwnd) <br/>{< br/> tchar classname [128]; <br/> getclassname (hwnd, classname, 128); <br/> If (! _ Tcscmp (classname, _ T ("workerw") <br/>{< br/> hwnd & hwndlv = * (hwnd *) lparam; <br/> tchar childclassname [128]; <br/> hwnd hchildwnd = getwindow (hwnd, gw_child); <br/> while (hchildwnd) <br/>{< br/> getclassname (hchildwnd, childclassname, 128); <br/> If (! _ Tcscmp (childclassname, _ T ("shelldll_defview") <br/> hwndlv = getfirstchild (hchildwnd); <br/> chassert (iswindow (hwndlv )); <br/> return false; <br/>}</P> <p> return true; <br/>}< br/>Now, you have to write the DLL file in cmd.exe to the end of the listviewcontrol file on the table. There are two main problems:
The first is to register the hook function of the mouse message, how to determine which item of the icon is clicked by the mouse, and find the information in various aspects and experiment to find a feasible method:
 

Code: Lvitem LVI; <br/> lvhittestinfo LVH = {0}; <br/> int iindexitem; <br/> long Lx = pmousehook-> PT. x; // pmousehook is a pointer to the mousehookstruct struct <br/> long Ly = pmousehook-> PT. y; <br/> lvh.pt. X = Lx; <br/> lvh.pt. y = ly; <br/> screentoclient (pmousehook-> hwnd, & (lvh.pt); <br/> listview_hittest (pmousehook-> hwnd, & LVH ); <br/> iindexitem = LVH. iItem; <br/> If (iindexitem! =-1) <br/>{< br/> zeromemory (& LVI, sizeof (LVI); <br/> LVI. mask = lvif_text; <br/> LVI. iItem = iindexitem; <br/> LVI. psztext = szbuff; <br/> LVI. cchtextmax = _ countof (szbuff); </P> <p> If (listview_getitem (pmousehook-> hwnd, & LVI )) <br/>{< br/> wsprintf (szdebug, text ("item text = '% S'"), szbuff); <br/> outputdebugstring (szdebug ); <br/>}< br/>The second problem I encountered was the issue of playing notes. I had no idea at the beginning because I had never touched on this knowledge before. I later found that I should use MIDI knowledge.
The main functions used are midioutopen and midioutshortmsg. For details about the Function usage, refer to msdn.
The main code of the getselecteditem function is as follows: code: Lvhittestinfo LVH = {0 }; <br/> int iindexitem; <br/> int keyno, ioctave, inote; <br/> long Lx = lmhs-> PT. x; <br/> long Ly = lmhs-> PT. y; <br/> lvh.pt. X = Lx; <br/> lvh.pt. y = ly; </P> <p> screentoclient (lmhs-> hwnd, & (lvh.pt); <br/> listview_hittest (lmhs-> hwnd, & LVH ); <br/> iindexitem = LVH. iItem; <br/> If (iindexitem! =-1) <br/>{< br/> point itempt; <br/> listview_getitemposition (lmhs-> hwnd, iindexitem, & itempt ); <br/>/* wsprintf (szdebug, text ("item coordinate = '% d, % d'"), itempt. x, itempt. y); <br/> outputdebugstring (szdebug); */<br/> int lineno = itempt. x/75; <br/> int colno = itempt. y/86; <br/> keyno = lineno * 10 + colno; <br/> If (keyno> = numscans) // No scan codes over 53 <br/> return; </P> <p> If (ioctave = Notes [keyno]. ioctave) =-1) // non-music key <br/> return; <br/> inote = Notes [keyno]. inote; <br/> If (keyno <30) <br/> midinoteon (hmidiout, 0, ioctave, inote, ivelocity ); // note on <br/> else <br/> {<br/> midisetpatch (hmidiout, 9, 0); <br/> midinoteon (hmidiout, 9, ioctave, inote, ivelocity); // note on <br/>}< br/>The main part is this. I will not go into details about how to install hook functions, how to intercept mouse messages, etc. There are a lot of details on the Internet. You can also check my source code.
When you are running the source code, another problem is the screen resolution, because the position and interval of the icon on the desktop may vary with the screen resolution, the code used in my program to determine the icon position,
Int lineno = itempt. X/75;
Int colno = itempt. Y/86;
Among them, 75 and 86 are the distance between an icon (1440x900) and the upper icon and the left icon and the right icon. I am writing it to save time, but I have not decided on the program. Hey hey, I will definitely improve it for the sake of universality. Now let's use it together.
If you want to know the distance between icons under your screen resolution, you can
/* Wsprintf (szdebug, text ("item coordinate = '% d, % d'"), itempt. X, itempt. y );
Outputdebugstring (szdebug );*/
Uncomment, run the program, click the icon, use the debugview software to view the output coordinate information, calculate the distance between adjacent icons, and replace 75 and 86.

After the program runs, click the musichook button. This is the desktop icon, and you can make a music note. Click the unhook button to cancel the hook.
This is the general situation. Finally, attach the collected guitar music records for your reference and play.
Stars: 1155665 4433221 5544332 5544332 1155665 4433221;
Selling songs: 555 555 35653 235 53532 132 332 612 665 365 53235 5323 5323 61231
Above the moon: 3686 3675 53686565326553 3686 89865 36865326656
Tuanzi family: 325 56 67 52 325 565 325 56 67 52 325 565 8753 5635 235 8753 5635 235
Two tigers: 1231 | 1231 | 345 | 345 | 565431 | 565431 | 151 | 151 | (favorite, haha)
Childhood: 35553 6676 66588886865 355536676-66658888668 ~
On the beach: 356666,352222, 356865132 or above is reprinted from my favorite reading Snow forum in the post: here specially given the link: http://bbs.pediy.com/showthread.php? T = 136144 as for the source code, you can go to the source Post Forum to download the source code, or directly ask for it and learn to communicate with you. E-MAIL: 1071545795@qq.com

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.