How To: Creating Software Integrations Part4

Here you will find tutorials that will help you with programming.

How To: Creating Software Integrations Part4

Postby autopilot » Mon Jan 14, 2008 1:26 am


If you have followed along so far, then you should be able to use button1 to open Notepad and button3 to send Hello World to the body of Notepad. The final steps are to save and exit. For this we will use the ResHack tool. Open ResHack and in ResHack, open C:\Windows\system32\notepad.exe. Then expand the menu – 1 and open the 1033.
int14.PNG
Integration Screen Shot14
Make note that “Save As” = 4 and “Exit” = 7. Now we can close ResHack and go back to our VB project. When a Save As is sent, a popup will ask for the name to save as. Let’s dig in and get this saved! To send a command to a window, we will use PostMessage (it is the companion to SendMessage). Just as with SendMessage, we needed a Windows message, PostMessage needs a Windows message. The message to tell PostMessage we are sending a command is defined as
Code: Select all
Const WM_COMMAND As Integer = &H111S
and the PostMessage call will look like:
Code: Select all
        PostMessage(notepad, WM_COMMAND, 4, 0)
Now add a button for the save as function and the code should be:
Code: Select all
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim notepad As Integer
        notepad = FindWindow("Notepad", "Untitled - Notepad")
        PostMessage(notepad, WM_COMMAND, 4, 0)
    End Sub
When button4 is pressed now, a “Save As” dialog box should pop open. See if you can figure out how to use button4 to open the “Save As” dialog and then send the name “Hello.txt” to the file name box.

HINT: Sometimes when dealing with external applications, you must make you program wait or pause to allow for forms to open, commands to process, etc.
Below is what I came up with:
Code: Select all
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim notepad As Integer
        notepad = FindWindow("Notepad", "Untitled - Notepad")
        PostMessage(notepad, WM_COMMAND, 4, 0)
        Dim EditHnd As Integer = 0
        Do While EditHnd = 0
            System.Threading.Thread.Sleep(100)
            EditHnd = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Edit", 1)
        Loop
        System.Threading.Thread.Sleep(500)
        SendMessageAsString(EditHnd, WM_SETTEXT, 0, "Hello.txt")
    End Sub
Now that the document name has been entered, we must send the enter key to the save button. To do this, we will use PostMessage with a Windows message of
Code: Select all
Const WM_KEYDOWN As Integer = &H100
and a key definition of
Code: Select all
Const VK_RETURN As Integer = &HD
So this is what my button4 code looks like to Save As:
Code: Select all
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim notepad As Integer, SaveBtn As Integer, EditHnd As Integer = 0
        notepad = FindWindow("Notepad", "Untitled - Notepad")
        PostMessage(notepad, WM_COMMAND, 4, 0)

        Do While EditHnd = 0
            System.Threading.Thread.Sleep(100)
            EditHnd = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Edit", 1)
        Loop
        System.Threading.Thread.Sleep(500)
        SendMessageAsString(EditHnd, WM_SETTEXT, 0, "Hello.txt")
        SaveBtn = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Button", 2)
        PostMessage(SaveBtn, WM_KEYDOWN, VK_RETURN, 0)
    End Sub
The Last thing left to do now is to close Notepad. Go ahead and add another button to the form and make it close Notepad. You shoud have something like:
Code: Select all
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim notepad As Integer
        notepad = FindWindow("Notepad", "Hello.txt - Notepad")
        PostMessage(notepad, WM_COMMAND, 7, 0)
    End Sub
Did you remember that the window caption changed when you did a Save As? Did you remember that the command for Exit was 7?
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 356
Joined: Sat Sep 23, 2006 7:19 pm

Return to Programming Tutorials

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests