Results 1 to 26 of 26

Thread: [RESOLVED] Shelling around

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Shelling around

    I know how to use the shell command to start a program from within my own. But I'd like do something more complicated:

    1. Start another app.
    2. Select a menu option: open a specific file.
    3. Then use another menu to make a change (edit a textbox)
    4. And finally select the save command in the file menu and give the file a new name.
    5. Exit from the app.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Shelling around

    Most of it can be done using SendMessage / PostMessage API
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    And if you can open the File by supplying a commandline parameter then that would cut down a lot of stuff you'd have to do

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by Shuja Ali
    Most of it can be done using SendMessage / PostMessage API
    Thank you, but I'm afraid I don't quite understand what the (last 2) parameters exactly mean in the function calls.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    the last two parameters (wParam & lParam) are dependent on which message you are sending / posting.

    Search for the message at http://www.msdn.com to find out what values they should be

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Shelling around

    The last two parameters are actually the additional input that you need to send to the application. If you search MSDN you will get more information regarding these API's.

    Also there will be lot of samples here on VBF, just do a search.

    Edit--
    I have become slow again.
    Last edited by Shuja Ali; Jun 7th, 2006 at 06:05 AM.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    the last two parameters (wParam & lParam) are dependent on which message you are sending / posting.

    Search for the message at http://www.msdn.com to find out what values they should be
    I'll stop by MSDN as soon as I can. Thanx.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by Shuja Ali
    The last two parameters are actually the additional input that you need to send to the application. If you search MSDN you will get more information regarding these API's.

    Also there will be lot of samples here on VBF, just do a search.

    Edit--
    I have become slow again.
    All right, I will do a search.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    After all I've found I can possibly do it by shelling the other app and using sendkeys. Is there any reason why I shouldn't go this way or is there any trouble that can be anticipated?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    SendKeys is notoriously unreliable - it only sends key-presses to the active window and you have no idea what might steal focus on your users computers. You might be able to get away one or two key-presses but the more you use then the more likely you are to run into trouble.

  11. #11

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    The thing is I haven't been able to find any decent explanation for this special intended use of SendMessage. Is there any place where all the possible messages and parameters are catalogued?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  12. #12
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    you can send any windows message using SendMessage and PostMessage - the list must run into thousands. You'll find them all at www.msdn.com but you have to have a rough idea at what your looking for as they are catalogued in groups (like messages for the ListBox etc.)

    Try searching for examples of what you want to do on the forum - you'll then find the messages that are used and you can look them up for more information.

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    you can send any windows message using SendMessage and PostMessage - the list must run into thousands. You'll find them all at www.msdn.com but you have to have a rough idea at what your looking for as they are catalogued in groups (like messages for the ListBox etc.)

    Try searching for examples of what you want to do on the forum - you'll then find the messages that are used and you can look them up for more information.
    As you say, it's not easy to find your way to the correct place in msdn. I tried already to search the forums with poor luck so far... but I haven't surrendered yet.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  14. #14
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    well if your trying to change the text in a textbox you'll want the WM_SETTEXT message; To close an app - WM_CLOSE; press a button - BM_CLICK

    you want to try and make this as simple as possible

  15. #15

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    well if your trying to change the text in a textbox you'll want the WM_SETTEXT message; To close an app - WM_CLOSE; press a button - BM_CLICK

    you want to try and make this as simple as possible
    Well, to begin with, how do I get a handle to the external application?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  16. #16

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by krtxmrtz
    Well, to begin with, how do I get a handle to the external application?
    ...and how do I specify which textbox or button I need to type to / click on?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  17. #17
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    To find the top-level window - you'll have to use FindWindow API,

    to find the handles of the child windows (buttons / textboxes etc.) use FindWindowEx

    if you do a search for those (forum/google/msdn - look at AllApi.net too) you'll find lots of information.

  18. #18

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    well if your trying to change the text in a textbox you'll want the WM_SETTEXT message; To close an app - WM_CLOSE; press a button - BM_CLICK
    Btw, how about clicking on a menu? Or perhaps I should rather send Alt/whatever to the form?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  19. #19
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    Check out WM_COMMAND. The GetMenu & GetSubMenu & GetMenuItemInfo APIs may also be of use to you.

  20. #20

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    Check out WM_COMMAND. The GetMenu & GetSubMenu & GetMenuItemInfo APIs may also be of use to you.
    All right, but before I get into the various menu related API calls, isn't it possible to use SendMessage to send text directly to the window, similarly to SendKeys? Looks easier to me sending e.g. "Alt/p" to activate "open file". What's the message in this case? (second parameter for SendMessage)
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  21. #21
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    if you find the ID of the menu you want to press then the SendMessage will just be a single line:
    VB Code:
    1. SendMessage thehWnd, WM_COMMAND, theID, 0&
    If you want to send an Alt+P i guess you'll have to use WM_KEYDOWN and then WM_KEYUP - check out the virtual key constants of the keys too. Alt will be VK_ALT or something like that.

  22. #22

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    if you find the ID of the menu you want to press then the SendMessage will just be a single line:
    VB Code:
    1. SendMessage thehWnd, WM_COMMAND, theID, 0&
    If you want to send an Alt+P i guess you'll have to use WM_KEYDOWN and then WM_KEYUP - check out the virtual key constants of the keys too. Alt will be VK_ALT or something like that.
    I've tried this to get the menu id with no success (nothing happens):
    VB Code:
    1. '...after shelling MyApp:
    2.         hWnd= FindWindow(vbNullString, "MyAppName")
    3.         hmnu = GetMenu(hWnd)
    4.         hsbmnu = GetSubMenu(hMenu, 0)
    5.         SendMessage hWnd, WM_COMMAND, hsbmnu, 0&
    ... I am sort of groping in the API...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  23. #23
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Shelling around

    that code will get the handle of the first menu (presumably 'File').

    You then need to get the MenuID (not handle) of the Open item on that menu. This can be done with GetMenuItemID

    You then pass the ID with the WM_COMMAND message, not the handle

  24. #24

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Shelling around

    Quote Originally Posted by bushmobile
    that code will get the handle of the first menu (presumably 'File').

    You then need to get the MenuID (not handle) of the Open item on that menu. This can be done with GetMenuItemID

    You then pass the ID with the WM_COMMAND message, not the handle
    It finally worked!

    Thanks a lot for patiently teaching me this on-the-fly SendMessage course.
    But stay tuned, I may be soon asking for more, I'm hungry!!!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  25. #25

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Shelling around

    Bushmobile, I tried to rate you up but it seems I must spread reputation elsewhere first. Sorry, some other time...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  26. #26

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