How To: Creating Software Integrations Part4

This topic contains 0 replies, has 1 voice, and was last updated by  autopilot 11 years, 8 months ago.

  • Author
    Posts
    1 Star2 Stars3 Stars4 Stars5 Stars Rate it First!
  • #14273
    Administrator
    Posts: 432

    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:Windowssystem32notepad.exe. Then expand the menu – 1 and open the 1033.

    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 Const WM_COMMAND As Integer = &H111Sand the PostMessage call will look like:PostMessage(notepad, WM_COMMAND, 4, 0)Now add a button for the save as function and the code should be:` 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:` 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 SubNow 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 ofConst WM_KEYDOWN As Integer = &H100and a key definition ofConst VK_RETURN As Integer = &HDSo this is what my button4 code looks like to Save As: 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 SubThe 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: 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?

You must be logged in to reply to this topic.