API Spy Tutorial

By Digital Rampage

The API Spy I will be using for this tutorial is going to be PAT or JK's API Spy. Pat's spy is the best choice for this for three reasons: It's powerful, it's very simple, and it's very user friendly. If you don't have Pat's API Spy, don't worry, you can get it from his web site: http://patorjk.com/.

Go there and then navigate to the "software" section. There you will find his API Spy. Please, follow these directions step by step so you don't have any trouble.

1. Window Information

When you load the Spy, you see some information. Notice how the information changes when you move your mouse over different windows. Lets take a look at this information and explain each part:

Window Handle: The window handle is the unique number that Windows gives the particular window when it is loaded. Just think of it as a social security number for that window.

Window Text: The window text is just that. It's the caption of the window.

Window Class Name: A window does not actually have to be a whole application. It could be just a textbox, or a command button from a application. The Class Name tells you which category (type of window) it is.

Parent Window Handle: First lets define Parent Window. The logic behind Parent Window is actually easy to understand. The Parent Window is just a container that can hold windows inside it. The windows it holds are its Child Windows. AOL is a parent window, and an Instant Message would be considered its child. AOL is the parent window to the instant message. Likewise, your form is the Parent Window to any textbox or control on your form. So this means the Parent Window Handle would be the Window Handle of the current window's parent.

Parent Window Text: The caption of the window's parent.

Parent Window Class Name: Parent Window Class Name is the class name of the window's parent.

2. Code Generator

The Code Generator is where all the magic happens. To get to the code generator, click on the tab that says "Code Generator". The code generator only needs two things to work. It needs a Window Handle, and an event (Code Option). We have one item to sort out before we continue. There are two option buttons in the code generator, one labeled "16 bit code", and the other "32 bit code". Visual Basic 4.0 and up are "32 bit" and use the "32 bit code" option. If you have Visual Basic 3.0 or lower, then you are using a "16 bit" version. If so, then select "16 bit code".

Now we can create some code. First were need to pick a window. For now we're going to use notepad. Load notepad by clicking Start, and selecting "Run". Then type "notepad" into the input box. Now go back to the code generator and click on "Options." We are going to focus on the Code Options. These are events that can be done to a window. Browse through them. The one we are going to use is the "Set window's text", so go down to it and click it. Once it's highlighted, just close the options window because it auto saves. Now were going to use that yellow ring. Click and hold your mouse on the yellow ring and drag it to the white text box in notepad, then release the mouse button. Notice how the API Spy has written some code for us. This is the code we will use in VB.

Now we have the code, but that does not mean it will work. API requires functions and declarations to work. First we need to open a new VB project, so do that. Now we need to add a module. To do this click Project, then select "Add Module." A new window will appear, just click "Open" in that window. Now we have a blank code window. A module is basically just a "Code Bank." It stores functions and pieces of code that you can use throughout your project. So what do we put in the module? We need some declarations. Don't worry, the API Spy has your back. Click the tag that says "Some Help." Then whether you're using 32-bit or 16-bit code, you need to click the button for it. I'm using VB6, so I'm going to click "32 bit code." Now you get a new window with a lot of code in it. This code is required for the API Spy to work. Copy and paste ALL of this code into your module.

3. Making your program

After you have copied the code to your module, you can switch to Form1 in your project. Add a command button, and a textbox. Keep the name of both controls to the default. Change the caption of the button to, "Send Message." Switch back to the API Spy, and go to the Code Generator. The code we created earlier should still be there. Copy it all from the textbox. If the textbox does not contain the code, then go back and recreate it. Now go back to VB, and double click your command button because were putting this code into it. The code should look something like this:

Dim notepad As Long, editx As Long
notepad = FindWindow("notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
Call SendMessageByString(editx, WM_SETTEXT, 0&, "The New Text")

We need to change one line of the code for it to work. The last line of code sends the message to the window and we want to send the text in our textbox to notepad. Change the last line so it looks like this:

Call SendMessageByString(editx, WM_SETTEXT, 0&, Text1.Text)

Give your program a run, because it should work now. What this does is change the text inside of notepad to the text in the textbox on your form. Type something, and click "Send Message".

4. Programming

There are tons more Code Options that do different things. You can change the caption of any window, or get the window's text instead of setting it. Just remember when someone says window, it does not actually mean a full window. It can be just a control on a window. In any event, try some of the other Code Options. Try hiding a window. If you do hide a window, make sure you also have the program set up to make the window visible again. And you may want to send Pat an EMAIL and thank him for all of his hard work in making his API Spy. If you have any questions at all, then don't hesitate to ask me: DigitalRampage@aol.com

Have fun programming!