Results 1 to 22 of 22

Thread: Automating Programs

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Automating Programs

    Hey,

    I am trying to get a hold of another application and simply send the print command. But this seems to be hard than I thought it was.

    I know there is the automation name space, but I cant seem to get anything working.
    http://msdn.microsoft.com/en-us/libr...utomation.aspx

    I am still at the very beginning of my test application, I am only able to open the 3rd party application and I am stuck there.

    Any suggestions are appreciated

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Automating Programs

    this is how to start notepad with no window, print the document, then exit:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim psi As New ProcessStartInfo With { _
            .FileName = "atextfile.txt", _
            .Verb = "Print", _
            .CreateNoWindow = True}
            Process.Start(psi)
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by .paul. View Post
    this is how to start notepad with no window, print the document, then exit:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim psi As New ProcessStartInfo With { _
            .FileName = "atextfile.txt", _
            .Verb = "Print", _
            .CreateNoWindow = True}
            Process.Start(psi)
        End Sub
    
    
    End Class
    What if it is not a regular format? It is a 3rd party program with a non standard extension.
    How would I specific what program to open?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Automating Programs

    filename can either be the application or the filename. when it's the application you can pass the filename in the arguments property.
    the only problem I can foresee is that a 3rd party app might not use verbs

    ProcessStartInfo Properties:

    http://msdn.microsoft.com/en-us/libr...roperties.aspx

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by .paul. View Post
    filename can either be the application or the filename. when it's the application you can pass the filename in the arguments property.
    the only problem I can foresee is that a 3rd party app might not use verbs

    ProcessStartInfo Properties:

    http://msdn.microsoft.com/en-us/libr...roperties.aspx
    Name:  DETE.JPG
Views: 197
Size:  18.9 KB

    I know this is unconventional but is it at all possible to send a file straight to the printer driver and have it deal with the file?


    I need to send this file to the printer but It looks like the only way to do this is through, taking control of the window and the sending key strokes or something?
    Last edited by Crzyrio; May 8th, 2013 at 03:24 PM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Automating Programs

    i'm out of ideas. i'll let someone else answer that...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by .paul. View Post
    i'm out of ideas. i'll let someone else answer that...
    Thank for the help!

    I am thinking I will have to use something like PostMessage(http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx) or SendMessage(http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx).
    Never tried them before but they look tricky.

    And I found this, which looks interisting ---> http://www.autohotkey.com/docs/misc/SendMessageList.htm

    Anyone have any experience with this?

    I would have to find the Right Window and just send a 'Crtl' + 'P' key stroke and then 'Enter'.... Sounds fun :P

  8. #8
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Automating Programs

    im not sure how its done exactly in vb.net
    but you will need to try something like this

    VB.NET-------------------
    start the program anyway you want, if you use process.start() you can put it straight into (<variable> as process) and skip next line
    search through the running processes for the program your looking for and grab the Handle
    <you can do all that in a few lines of code>
    <next>

    WIN API--------------------------
    <i think you need to loop through using these api to find your menu item>
    then you need to use 'GetMenu' 'Getmenuitemid' 'GetMenuCount'
    get the handle of the menu item you need then use 'SendMessage' to start the code behind the menu item


    its a little vague but its a start hope this helps
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  9. #9
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Automating Programs

    just note your going through the hierarchy of handles to get down to the menu items. use the parent handles to find the child handles
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  10. #10
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Automating Programs

    just to ease your mind a little bit with win API its really not complicated, the worst part is thinking about how many handles are running in windows which could be millions, dont think like that, as long as you have a handle your good to go.
    this is a small app that starts a program inside my own program look how easy it is.
    dont forget the declarations

    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    proc = Process.Start("IExplore.exe", "http:\\www.google.com -new")

    System.Threading.Thread.Sleep(1000)

    proc.WaitForInputIdle()

    SetParent(proc.MainWindowHandle, Me.TableLayoutPanel1..Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

    End Sub

    just google sendmessage api to get a list of the commands that you can send
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by GBeats View Post
    just to ease your mind a little bit with win API its really not complicated, the worst part is thinking about how many handles are running in windows which could be millions, dont think like that, as long as you have a handle your good to go.
    this is a small app that starts a program inside my own program look how easy it is.
    dont forget the declarations

    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    proc = Process.Start("IExplore.exe", "http:\\www.google.com -new")

    System.Threading.Thread.Sleep(1000)

    proc.WaitForInputIdle()

    SetParent(proc.MainWindowHandle, Me.TableLayoutPanel1..Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

    End Sub

    just google sendmessage api to get a list of the commands that you can send
    Hey,

    THanks for all the help!

    Just a few clarifications. What does this doo --> Me.TableLayoutPanel1.Handle)

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Automating Programs

    What does this doo --> Me.TableLayoutPanel1.Handle)
    It disnae doo owt! It's a property giving the handle of the control's window. You use it instead of going through the API to find an hWnd value before using it in API functions & subs.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by dunfiddlin View Post
    It disnae doo owt! It's a property giving the handle of the control's window. You use it instead of going through the API to find an hWnd value before using it in API functions & subs.
    Did this: and the program shows up in my panel now. Perty cool :P
    vb.net Code:
    1. SetParent(psi.MainWindowHandle, Panel1.Handle)
    2.  
    3.         SendMessage(psi.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

    Now in order to get menu items, I have gotten this far as of now.

    vb.net Code:
    1. Declare Auto Function GetMenu Lib "user32.dll" (ByVal hWnd As IntPtr) As Integer

    as per http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    and I assume I would use it like so

    vb.net Code:
    1. GetMenu(psi.MainWindowHandle)

    So that will give me the menu handle but then what?
    How do I know what is available in the menu to select

    EDIT: My Code Right Now, just stepping through stuff figuring out whats going on and looking at examples

    vb.net Code:
    1. Imports System.Drawing.Printing
    2. Imports System.Windows.Automation
    3. Imports System.IO
    4.  
    5. Public Class Form1
    6.     Dim psi As New Process
    7.     Dim proc As New Process
    8.  
    9.     Dim ParentWindow As IntPtr
    10.     Dim Menu As IntPtr
    11.  
    12.     Private Const WM_SYSCOMMAND As Integer = 274
    13.     Private Const SC_MAXIMIZE As Integer = 61488
    14.  
    15.     Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    16.     Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    17.     Declare Auto Function GetMenu Lib "user32.dll" (ByVal hWnd As IntPtr) As Integer
    18.  
    19.  
    20.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    21.  
    22.  
    23.  
    24.         psi = Process.Start("C:\Documents and Settings\rdlyma\Desktop\TEST.l5f")
    25.  
    26.  
    27.  
    28.  
    29.     End Sub
    30.  
    31.  
    32.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    33.  
    34.         ParentWindow = SetParent(psi.MainWindowHandle, Panel1.Handle)
    35.  
    36.         SendMessage(psi.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    37.  
    38.         Menu = GetMenu(ParentWindow)
    39.  
    40.     End Sub
    41.  
    42.  
    43. End Class
    Last edited by Crzyrio; May 9th, 2013 at 10:46 AM.

  14. #14
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Automating Programs

    Never do anything API without consulting pinvoke.net (although it's VB declarations do tend to be a bit on the old fashioned side!) Here's there sample for GetMenu

    vb.net Code:
    1. Dim hwndMain as IntPtr = FindWindowEx(intptr.Zero,intptr.Zero,vbNullString,"My Window Title") 'Get the Window Handle
    2. Dim hwndMenu As IntPtr = GetMenu(hwndMain) 'Get the MenuHandle
    3. Dim hwndSubmenu as intptr = GetSubMenu(hwndMenu,1) 'Get the SubMenuHandle to the visible MenuItem, in this case the second one (counting from zero)
    4. Dim intMID as Integer = GetMenuitemID(hwndSubmenu,2)) 'Get the MenuItemID of the third SubMenu under the previous Handle (counting from zero)
    5. Sendmessage(hwndMain, WM_COMMAND, intMID,  0) 'Click the MenuItem!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  15. #15
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Automating Programs

    please note the sample i sent was just a sample, you will be needing different sendmessage values, i think you probably figured out that SC_MAXIMIZE (aka #61488) wont do you much good with a menu,

    like the great dunfiddlin says above, use getsubmenu in a loop to go through each menu item until you find the one your looking for, grab the handle then send the message.

    in your case you know roughly where it is file->print so that will probably be menu item index(0) then however many down that list the print comes in, but like i said best to loop through and check each name to avoid problems, maybe your notepad one day will put print in item 5 not 6 for some reason (unlikely but you get the point).
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by dunfiddlin View Post
    Never do anything API without consulting pinvoke.net (although it's VB declarations do tend to be a bit on the old fashioned side!) Here's there sample for GetMenu

    vb.net Code:
    1. Dim hwndMain as IntPtr = FindWindowEx(intptr.Zero,intptr.Zero,vbNullString,"My Window Title") 'Get the Window Handle
    2. Dim hwndMenu As IntPtr = GetMenu(hwndMain) 'Get the MenuHandle
    3. Dim hwndSubmenu as intptr = GetSubMenu(hwndMenu,1) 'Get the SubMenuHandle to the visible MenuItem, in this case the second one (counting from zero)
    4. Dim intMID as Integer = GetMenuitemID(hwndSubmenu,2)) 'Get the MenuItemID of the third SubMenu under the previous Handle (counting from zero)
    5. Sendmessage(hwndMain, WM_COMMAND, intMID,  0) 'Click the MenuItem!
    Thanks.
    I am playing around with it in notepad and it seems to work, it is quiet cool :P

    But in the program I want to use it with, both GetMenu and GetMenuitemID return 0, does that mean the program does not have access to it?

    Is it possible to send keystrokes to the program instead? so maybe send the equivalent of 'Ctrl + P'

  17. #17

    Re: Automating Programs

    You can use the SendKeys function, but it's flaky and you don't really have a guarantee of anything. Honestly, if you really want simplistic automation of programs, you might want to look into AutoIT. I've used it a few times at work and it is able to help with these types of things better than .NET can.

  18. #18
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Automating Programs

    But in the program I want to use it with, both GetMenu and GetMenuitemID return 0, does that mean the program does not have access to it?
    if its got a normal dropdown menu or even right clickmenu, then it should list with the menu API

    if its a ribbon or some custom control you may have to search through child windows instead of menus, it may get a little tricky or tedious. but rest assured every single component on every single program has a handle, im not sure if they can be hidden from the win API, or whether theres a stronger api to use.

    maybe you could try autoit, but i think ur turning power monger and cant resist the possibilites and temptations of API lol
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  19. #19
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Automating Programs

    I've always found SendKeys adequate for most situations where the application is on the same machine. It's less reliable with web applications and the like. But there's also the key_event API available if it proves less than trustworthy.

    I imagine that the ultimate goal would be to add this program as a context menu choice in Explorer for the filetype. Would AutoIt have that capability?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Quote Originally Posted by formlesstree4 View Post
    You can use the SendKeys function, but it's flaky and you don't really have a guarantee of anything. Honestly, if you really want simplistic automation of programs, you might want to look into AutoIT. I've used it a few times at work and it is able to help with these types of things better than .NET can.
    Thanks! Just downloaded it will give it a try.

    Quote Originally Posted by GBeats View Post
    if its got a normal dropdown menu or even right clickmenu, then it should list with the menu API

    if its a ribbon or some custom control you may have to search through child windows instead of menus, it may get a little tricky or tedious. but rest assured every single component on every single program has a handle, im not sure if they can be hidden from the win API, or whether theres a stronger api to use.

    maybe you could try autoit, but i think ur turning power monger and cant resist the possibilites and temptations of API lol
    If that is the case, then I will keep trying. Here is what the main menu looks like, fairly normal. Even in spyxx nothing shows up under this program

    Name:  DETE.JPG
Views: 164
Size:  24.1 KB

    In case someone knows what this means, screen from spyxx
    Name:  DETE.JPG
Views: 174
Size:  10.6 KB
    Quote Originally Posted by dunfiddlin View Post
    I've always found SendKeys adequate for most situations where the application is on the same machine. It's less reliable with web applications and the like. But there's also the key_event API available if it proves less than trustworthy.

    I imagine that the ultimate goal would be to add this program as a context menu choice in Explorer for the filetype. Would AutoIt have that capability?
    I am just playing around with AutoIt and do not see much need for it. I think SendKeys should be adequate right now, I just have to keep playing with it.

    What if I did something like this :P http://www.youtube.com/watch?v=IyzLjMSvmJ0


    EDIT: How would I set the inputfocus to another application?

    EDIT.EDIT: K figured that out
    vb.net Code:
    1. Dim hWnd As Long
    2.         hWnd = FindWindow(vbNullString, "Untitled - Notepad")
    3.         SetForegroundWindow(hWnd)

    now the tricky question. Can I send an input using

    vb.net Code:
    1. SendKeys.Send("^P")

    to another application without bringing it to the front
    Last edited by Crzyrio; May 9th, 2013 at 02:12 PM.

  21. #21

    Re: Automating Programs

    To answer your last question: No, not with SendKeys. Last I recall (and confirming with the appropriate MSDN article), SendKeys sends to the active window. Checking the documentation yields us with:
    Use SendKeys to send keystrokes and keystroke combinations to the active application.
    as the first sentence under remarks. So there you have it.

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Automating Programs

    Sorry for the late reply but since my start down this road, there was a set of new drivers released for the printer that give me what I need in order to use it.

    So no need for automating anything . Still learned a lot from this thread though and I am sure someone will find it useful

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