Results 1 to 36 of 36

Thread: [RESOLVED] [2005] Controlling a program without an SDK or command line input

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Resolved [RESOLVED] [2005] Controlling a program without an SDK or command line input

    Hi again, guys. It's been almost four months since I've been here (or done any programming at all). Nice to be back.

    First off, TuneBite (screenshot below) essentialy converts audio files. You add files to the left side, press go, and they show up on the right side after they are done.

    I need to find a way in .NET to get input and output from a program that doesn't really allow easy means to do so. There is no documentation for the command line options that can be run, but I found a one (/add "file_name") just by experimenting.

    Is there a way I can remotely access controls in the program and get simple input or output to/from them.

    I can add files from the command line, but I need to be able to press "go" and tell when it is done converting. Any ideas?
    Attached Images Attached Images  
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Controlling a program without an SDK or command line input

    Interacting with external applications means using the Windows API. Many, many posts dedicated to the subject. Even a whole forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Shba. Cut me some slack. It's been a while. Plus I've never done API in .NET, only in VB6. I'll do some research though.

    Thanks as awlays JMC.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Controlling a program without an SDK or command line input

    Check out gigemboy's signature for various API tools and examples.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    Look into SendMessage, using the WM_LBUTTONUP and WM_LBUTTONDOWN constants in succession in order to simulate a click on a button. You would have to enumerate the controls in order to find the handle to the button you wish to press, and then send those two SendMessage commands in order to "click" the button. The main problem with API in .NET is getting the declarations correct, and APIViewer 2004 (in sig) should help with that. Just a matter of changing it to .NET mode in the options, and looking up the function you wish to use. Then just copy and paste...

    You could also use WinID (in sig) or Spy++ in order to see the properties for that button, like the class and name, and try to retrieve the handle using that, or also find if it is in some sort of container control on the form to better know how far you have to drill down in order to get to it...

    Or if youre feeling really adventurous, you can check out APISpy (in sig), which should autogenerate some code for you if you choose the button. I believe the output would be VB6-style, just be a matter of cleaning it up a little for .NET...

    There is also an example in the .NET codebank on enumerating all windows and children, which might help when trying to find the handle to the button you are wanting...
    Last edited by gigemboy; Oct 4th, 2006 at 08:56 PM.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    As far as your question on waiting for the app to finish, you can experiment with Process.WaitForInputIdle(). It should wait until the app is at an idle state before returning and running the code that you want, though not too sure what exactly designates a particular app as idle, especially if multithreading is involved and all the work is being done in the background with a "idle" and responsive interface. I usually use it to just wait for an application to start up, but you might be able to test it to see if it gets the job done or not. An example of how you can try to test is below:
    VB Code:
    1. 'replace "firefox" with your process name
    2.         Dim Procs() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("firefox")
    3.         Select Case Procs.Length
    4.             Case 0
    5.                 MessageBox.Show("No instances found!")
    6.             Case 1
    7.                 'only one instance, so run our code
    8.                 Procs(0).WaitForInputIdle()
    9.                 'run the code here that you wish to try after the app is done and idle
    10.                 'function won't return until the app is at an idle state...
    11.             Case Is > 1
    12.                 MessageBox.Show("Multiple instances found! What do you want to do?")
    13.         End Select
    Essentially you would do this after you had added your files and clicked the button to start the conversion or encoding or whatever its doing. Then you run that piece after the tunebite app is running to see if you can get it to wait until its done and run the code you want (after youve added something there to test). Just something you can try...

  7. #7

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Thanks gigem. The WaitForInputIdle didn't work, but I thought of another idea: Is there any way to get the value of the CPU column that shows up in the taskmanager? That would make it easy for me to tell if it is working or not.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    Trying to figure out a non API way to do it, and looking at the Process class, I see a PrivilegedProcessorTime property that seems to be the sum of the total time that the process has taken on the processor. If the app is completely idle and not taking any cpu cycles, this property shouldn't increase at all. If it is running and taking processor cycles, you should see it increasing. You could find a way to first, check what the property is for that process before you click the button, and then check the property at a set interval in a timer when the external app is running. You should see it steadily increase each increment, so it would be a matter of stopping the timer when the previous PrivilegedProcessorTime property value is equal to the current PrivilegedProcessorTime property value (or really close to each other, since the accuracy is very small increments of time), and then running the code you want when the timer stops...
    VB Code:
    1. 'sample code that you can add in the post above to check the privilegedprocessortime
    2. MessageBox.Show(Procs(0).PrivilegedProcessorTime.ToString)
    Last edited by gigemboy; Oct 5th, 2006 at 03:37 AM.

  9. #9

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Perfect. I can tell when the program is working or not really easily now. Now I need to get the button part working. I read a few articles and put together the code below. I get an error about Pinvoke and unmanged code when I run it though.

    I get the error on this line: "ptrChild = GetWindow(ParentWindowHandle, GW_CHILD)"

    Any thoughts?

    VB Code:
    1. Public Class Form1
    2.  
    3.     Const GW_CHILD As Integer = 5
    4.     Const GW_HWNDNEXT As Integer = 2
    5.  
    6.     Const WM_GETTEXT As Integer = &HD
    7.     Const WM_GETTEXTLENGTH As Integer = &HE
    8.  
    9.     Const BM_SETSTATE As Integer = &HF3
    10.     Const WM_LBUTTONUP As Integer = &H202
    11.     Const WM_LBUTTONDOWN As Integer = &H201
    12.  
    13.     Declare Auto Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Long) As IntPtr
    14.  
    15.     Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
    16.     ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
    17.  
    18.     Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
    19.         ByVal wParam As Integer, ByRef lParam As IntPtr) As Integer
    20.  
    21.     Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, _
    22.         ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
    23.  
    24.     Public Function GetWindows(ByVal ParentWindowHandle As IntPtr) As IntPtr()
    25.  
    26.         Dim ptrChild As IntPtr
    27.         Dim ptrRet() As IntPtr
    28.         Dim iCounter As Integer
    29.  
    30.         'get first child handle...
    31.         ptrChild = GetWindow(ParentWindowHandle, GW_CHILD)
    32.  
    33.         'loop through and collect all child window handles...
    34.         Do Until ptrChild.Equals(IntPtr.Zero)
    35.             'process child...
    36.             ReDim Preserve ptrRet(iCounter)
    37.             ptrRet(iCounter) = ptrChild
    38.             'get next child...
    39.             ptrChild = GetWindow(ptrChild, GW_HWNDNEXT)
    40.             iCounter += 1
    41.         Loop
    42.  
    43.         'return...
    44.         Return ptrRet
    45.  
    46.     End Function
    47.  
    48.     Public Function GetWindowText(ByVal WindowHandle As IntPtr) As String
    49.  
    50.         Dim ptrRet As IntPtr
    51.         Dim ptrLength As IntPtr
    52.  
    53.         'get length for buffer...
    54.         ptrLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
    55.  
    56.         'create buffer for return value...
    57.         Dim sbText As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)
    58.  
    59.         'get window text...
    60.         ptrRet = SendMessage(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sbText)
    61.  
    62.         'get return value...
    63.         Return sbText.ToString
    64.  
    65.     End Function
    66.  
    67.     Public Sub ClickButton(ByVal ButtonHandle As IntPtr)
    68.  
    69.         'send the left mouse button "down" message to the button...
    70.         Call SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, IntPtr.Zero)
    71.  
    72.         'send the left mouse button "up" message to the button...
    73.         Call SendMessage(ButtonHandle, WM_LBUTTONUP, 0, IntPtr.Zero)
    74.  
    75.         'send the button state message to the button, telling it to handle its events...
    76.         Call SendMessage(ButtonHandle, BM_SETSTATE, 1, IntPtr.Zero)
    77.  
    78.     End Sub
    79.  
    80.     Public Sub LaunchAndCloseRegSvr()
    81.  
    82.         'launch our process, we will see a dialog box with an OK button...
    83.         Dim clsProcess As System.Diagnostics.Process = System.Diagnostics.Process.Start("Regsvr32", " /?")
    84.  
    85.         'get an array containing the handles to each of the child windows of the dialog...
    86.         Dim ptrChildWindows() As IntPtr = GetWindows(clsProcess.MainWindowHandle)
    87.  
    88.         'loop through each child window, looking for the OK button...
    89.         For iCounter As Integer = 0 To ptrChildWindows.Length - 1
    90.             'grab the current handle to process...
    91.             Dim ptrCurrent As IntPtr = ptrChildWindows(iCounter)
    92.             'get the window text...
    93.             Dim sText As String = GetWindowText(ptrCurrent)
    94.             'check to see if this is the button we are looking for...
    95.             If sText = "OK" Then
    96.                 'click the button to close the dialog...
    97.                 ClickButton(ptrCurrent)
    98.                 'done deal...
    99.                 Exit For
    100.             Else
    101.                 Debug.WriteLine(sText)
    102.             End If
    103.         Next
    104.  
    105.     End Sub
    106.  
    107.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    108.         LaunchAndCloseRegSvr()
    109.     End Sub
    110. End Class
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  10. #10
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    In your declaration for getwindow. Change wCmd to an integer.
    Last edited by mpdeglau; Oct 5th, 2006 at 01:15 PM.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  11. #11
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    Well from an immediate glance I see wCmd as Long being used instead of Integer. Switch it to Integer, since that is what it should be in .NET. Also not sure of Auto Function is required. This was pulled from APIViewer 2004 as the declaration:
    VB Code:
    1. Declare Function GetWindow Lib "user32.dll" ( _
    2.      ByVal hwnd As Int32, _
    3.      ByVal wCmd As Int32) As Int32
    Keeping the hwnd as IntPtr like you had should be fine and is actually a little more correct than using Int32....

  12. #12

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    After making that change, I get a NullReferenceException on this line: "For iCounter As Integer = 0 To ptrChildWindows.Length - 1"
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  13. #13
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    Have you stepped through it to see what is happening? I took this from you
    VB Code:
    1. Dim clsProcess As System.Diagnostics.Process = System.Diagnostics.Process.Start("notepad")
    2.  
    3.         'get an array containing the handles to each of the child windows of the dialog...
    4.         Dim ptrChildWindows() As IntPtr = GetWindows(clsProcess.MainWindowHandle)
    The only thing I changed was I used notepad instead and it returned 2 values for me.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  14. #14

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Oddly, when I step through it I no longer get an error on that line, and when I run it with notepad I also get 2 values, but when I continue stepping through it I get an error on this line: "ptrLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)"

    It says EntryPointNotFoundException.

    Here's how I changed GetWindow by the way:
    VB Code:
    1. Declare Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Int32) As IntPtr
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  15. #15
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    It looks like the reason it is comming back null when you don't step through the app is because it doesn't have a MainWindowHandle yet. I added
    VB Code:
    1. Threading.Thread.Sleep(500)
    between starting the process and using the handle and it worked. Note using 100 was not long enough.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  16. #16

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    That worked, but I still get the error that I noted in my last post.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  17. #17
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    YEah I'm still looking into that one. The error as far as I know means that you are using the wrong dll, but you're not. So I'm confused.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  18. #18

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Found it. This:
    VB Code:
    1. Declare Function SendMessage Lib "user32.dll"  ( ... )

    ... Needed to be this ...

    VB Code:
    1. Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA"  ( ... )
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  19. #19
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    The shouldn't alias doesn't matter I got it to work by declaring the apis like this:
    VB Code:
    1. Private Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As _
    2.  IntPtr, ByVal wMsg As Int32, ByVal wParam As IntPtr, ByVal lParam As _
    3. IntPtr) As IntPtr
    4.  
    5.     Private Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As _
    6.  IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As _
    7.  IntPtr) As Integer
    8.  
    9.     Private Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As _
    10.  IntPtr, ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As _
    11.  System.Text.StringBuilder) As IntPtr

    Also I think your buttonup state might be wrong. The button went down for me, but didn't go back up.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  20. #20

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Okay. Now everything works except for the ButtonPress method. I tried walking through it to automatically give it some pause inbetween each SendMessage call, and it still only pushed the button down (I know this because the image of the button is depressed). Something isn't working about WM_LBUTTONUP. Not sure....
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  21. #21

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Nevermind. When I actually added a Sleep to it (instead of walking through it) it worked fine on RegSvr32. Now I just need to find the name of the button on TuneBite. Hopefully it is a button and not an image or something... Not sure what I would do then.

    EDIT: I also had to add this line: "Call SendMessage(ButtonHandle, BM_SETSTATE, 1, IntPtr.Zero)" After the button down (instead of just after the button up). This made it register the button down, then receive the button up message properly.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  22. #22
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    Good luck.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  23. #23

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Sigh... I need something other than the buttons name to identify it. They are all named with a blank string. I guess I could try pressing all of them until I figured out which index number was which button, but that doesn't seem very reliable. Any other identifying marks of a button? Besides just it's hWnd?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  24. #24

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    I just cycled through all 15 of the "buttons" and none of them were anything that responded to the BUTTONUP and BUTTONDOWN commands.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  25. #25
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    I doubt it will work, but try
    [Highlight=VB]
    Const WM_LBUTTONDBLCLK As Int32 = &H203
    [Highlight=VB]
    instead of button up and down
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  26. #26

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Nope. As sloppy as this is, is there any way to control the mouse and send message to the mouse to click?

    Or is there some other way to tell which one of the 15 controls is the image/button I need and then I can experiment with what commands to send to it?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  27. #27

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Okay. I figured out how to move the cursor. My last question is how to change the position of the external application window. I ran across a few APIs that look like they could do the job, but I'm not sure which to use.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  28. #28
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    I couldn't tell you for sure, but I would start with SetWindowPos.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  29. #29

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Okay. I can move the mouse. And the window has a default position that I can work with. The only problem remaining is that I can't simulate a click of the mouse, based on where the mouse is. Is that even possible?

    Is it possible to send message to the cursor to tell it to click? I can move it to the right position using Windows.Forms.Cursor.Position, but there is not method or property to simulate a click as far as I can tell.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  30. #30
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Controlling a program without an SDK or command line input

    Take a look at this thread #2
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  31. #31
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    Well if you have the cursor position over the button you want, you can try the WindowFromPoint API in order to get a handle to the window that the cursor is over. I think it returns the "most childish" handle (if you get my drift), which should be the handle to the button if the cursor is over it, as opposed to returning the parent window handle or something like that, although not 100% sure. I checked an old prog I tried and clicked a button on a form using GetCursorPos and WindowFromPoint, and it seemed to work, retrieving the handle to the button and not the parent container window, but in reading the documentation, I have some doubts, and don't have the prog anymore that the code was ran with to test. You can try it out and see if it works...
    VB Code:
    1. 'pass in the x and y coordinates of your mouse point
    2. Declare Function WindowFromPoint Lib "user32.dll" (ByVal xPoint As Integer, _
    3.                                                        ByVal yPoint As Integer) _
    4.                                                        As IntPtr
    If that doesn't work, you can also try ChildWindowFromPoint
    Last edited by gigemboy; Oct 6th, 2006 at 02:53 PM.

  32. #32

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    This is sloppy, but it works:

    VB Code:
    1. Private Declare Sub mouse_event Lib "user32.dll" ( _
    2.           ByVal dwFlags As Int32, _
    3.           ByVal dx As Int32, _
    4.           ByVal dy As Int32, _
    5.           ByVal cButtons As Int32, _
    6.           ByVal dwExtraInfo As Int32)
    7.     Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
    8.  
    9.     Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
    10.     Const MOUSEEVENTF_LEFTUP As Int32 = &H4
    11.     Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20
    12.     Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40
    13.     Const MOUSEEVENTF_MOVE As Int32 = &H1
    14.     Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000
    15.     Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8
    16.     Const MOUSEEVENTF_RIGHTUP As Int32 = &H10
    17. ....
    18.         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    19.         Threading.Thread.Sleep(500)
    20.         'Mouse Up
    21.         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    22.         Threading.Thread.Sleep(500)
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  33. #33
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    Well its API, not easy to make it look "pretty" So does that wrap everything up?

  34. #34

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    Not quite. I knew I would run into more problems. That's why I left the thread open.

    I can't get the SetFocus API to work. I think it might be because the program has a system tray icon and it's POSSIBLE that it's counting the icon as it's own form and attempting to set focus to that, but here's what I have:

    VB Code:
    1. Declare Function SetFocus Lib "user32.dll" ( _
    2.         ByVal hwnd As Int32) As Int32
    3.  
    4. ...
    5.  
    6.     SetFocus(Procs(0).MainWindowHandle.ToInt32)

    MainWindowHandle returns an IntPtr... I had to convert it to an Integer to pass it to the API. Not sure what I should do.

    Should I jump back to the earlier code about getting all the different handels on the form and set focus to all of them and hopefully one is the actual main window?

    EDIT: And just for the record, Procs(0) does have the program attached to it.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  35. #35
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Controlling a program without an SDK or command line input

    Well there was an API function I found a few days ago in response to another thread on a different forum, and it is SwitchToThisWindow. And also later down in the thread there was an issue with minimized (or hidden windows, something like that), and another person responded what to do in those circumstances, so it could be exactly what you're looking for...

    Check out the thread here: http://www.codeguru.com/forum/showth...93#post1462493
    Last edited by gigemboy; Oct 6th, 2006 at 05:52 PM.

  36. #36

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [2005] Controlling a program without an SDK or command line input

    That worked PERFECTLY. Awesome. Thanks a ton. The rest is easy.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width