CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2012
    Posts
    19

    Question Cannot automate/simulate listview click of another process (C++/Win32)

    I am developing an automation tool. I want to click listview item of another process programatically. I have partially succeeded in doing. But I am getting a slightly different output.I have used NMITEMACTIVATE(I have also used PostMessage Instead of SendMessage in the code below).

    Code:
    NMITEMACTIVATE nmbh;
    nmbh.hdr.code = NM_DBLCLK;
    nmbh.hdr.hwndFrom=g_hWnd;
    nmbh.hdr.idFrom=GetDlgCtrlID(g_hWnd);
    nmbh.iItem=itemval;
    nmbh.iSubItem=0;
    nmbh.uNewState=0;
    nmbh.uOldState=0;
    nmbh.uChanged=0;
    nmbh.uKeyFlags=0;
    SendMessage(GetParent(g_hWnd), WM_NOTIFY,(WPARAM)g_hWnd,(LPARAM)&nmbh);
    Last edited by haroonrulz; October 31st, 2013 at 11:38 PM.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Quote Originally Posted by haroonrulz View Post
    I have used NMITEMACTIVATE(I have also used PostMessage Instead of SendMessage in the code below).

    Code:
    SendMessage(GetParent(g_hWnd), WM_NOTIFY,(WPARAM)g_hWnd,(LPARAM)&nmbh);
    You are not allowed to send or post message which parameter passes pointers over. While valid in the context of your process, the pointer is invalid in the context of another one. Processes do not share their memory spaces. Any Win32 process memory is virtualized and isolated from others.
    Best regards,
    Igor

  3. #3
    Join Date
    Jun 2012
    Posts
    19

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Igor I was expecting your reply. Thanks.
    This is what I have done:
    1.Wrote code to get parent handle,listview handle,listview item to click
    2.shared memory to other process using -"Creating Named Shared Memory"
    3.Used hooking concept
    4.Inside the hook proc code,added the code which is above.
    I tested with winspy++ (another process), its working fine. But while testing on other application application its not working.

    For Testing:
    1.I tested listview code with winspy++ - Working.
    2.I downloaded tested a simple listview application from here --> http://www.codeproject.com/Articles/...nder-Win32-API
    it was not working.

    Can you suggest/help.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Sorry, but I still don't get this:
    Quote Originally Posted by haroonrulz View Post
    I tested with winspy++ (another process), its working fine. But while testing on other application application its not working.
    What with winspy is working fine? What is not working with your hook? Are you sure your hook gets activated in the remote process? Do you fully understand what WM_NOTIFY is and what it's used for?

    You should build some simplistic app including list view, and there you fully implement the automation behavior you want, and make sure it works in local process. Only then you put your code into hook dll and try it with remote process.
    Best regards,
    Igor

  5. #5
    Join Date
    Jun 2012
    Posts
    19

    Question Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Testing Winspy:
    Manually first I tested winspy, by clicking on the second listview item in winspy the listview gets blank.
    So I run my code,then the Listview in winspy became blank.So I confirmed that my Hook code is working properly for remote process. Pic (1.1.jpg)

    Simple Listview Application:
    Then I tested in another application (1.2.jpg), nothing happened.So I downloaded the source code of this application, then I came to know that my code is not getting detected.

    1.If my Hook code is not working,It would not been worked in winspy++ also ?
    Attached Images Attached Images   

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Just forget about winspy. It's critical to test in the app which source code is under control, so it is traceable in debugger where yo can see what really goes on.
    Best regards,
    Igor

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    As for your code. It seems you never noticed my question if you really understand what WM_NOTIFY is.

    Let's see what happens when you do a hardware click. The control window receives WM_LBUTTONDOWN and does its internal processing like changing item selection, modifying internal data structures about control state, doing proper drawing, etc. There's a lot of important things it does prior to sending WM_NOTIFY to control's parent. On the notification the parent does its own part like getting information from notification, possibly getting other information from control's internal data structures, and finally decides what to do next. Like showing message box.

    Now about what your code does. It just makes parent believe the click was done without a real click and all the related internal control's processing. Parent is told that control has changed, but nothing has changed. No surprise it all works not the way you expected.
    Last edited by Igor Vartanov; November 2nd, 2013 at 07:46 AM.
    Best regards,
    Igor

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    If you want to automate the clicking of listcontrol items, use the active accessibility interfaces.

  9. #9
    Join Date
    Jun 2012
    Posts
    19

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    As my ultimate target is to automate MFC based application.I downloaded application/Source code from here : http://www.codeproject.com/Articles/...e-List-Control . As per you Suggestion I debugged the source code, its working good.(my Hook gets activated in another process and SendMessage working good).Thanks for explaining WM_NOTIFY , I do understand it.
    But my program is not working in the application which in which I need to automate. You can see from the picture that the Expected(fig1) and Actual(fig2) are different.Some number is getting displayed before OK,Cancel. If you know any useful link,please let me know.Also let me know if I am doing any mistake in constructing structure below,

    NMITEMACTIVATE nmbh;
    nmbh.hdr.code = NM_DBLCLK;
    nmbh.hdr.hwndFrom=g_hWnd;
    nmbh.hdr.idFrom=GetDlgCtrlID(g_hWnd);
    nmbh.iItem=itemval;
    nmbh.iSubItem=0;
    nmbh.uNewState=0;
    nmbh.uOldState=0;
    nmbh.uChanged=0;
    nmbh.uKeyFlags=0;
    SendMessage(GetParent(g_hWnd), WM_NOTIFY,(WPARAM)g_hWnd,(LPARAM)&nmbh);
    Last edited by haroonrulz; November 6th, 2013 at 12:02 AM.

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Quote Originally Posted by haroonrulz View Post
    ... You can see from the picture that the Expected(fig1) and Actual(fig2) are different.Some number is getting displayed before OK,Cancel.
    What do these pictures have to do with your list control?
    How do you put the buttons' captions?

    Quote Originally Posted by haroonrulz View Post
    ... let me know if I am doing any mistake in constructing structure below,

    NMITEMACTIVATE nmbh;
    nmbh.hdr.code = NM_DBLCLK;
    nmbh.hdr.hwndFrom=g_hWnd;
    nmbh.hdr.idFrom=GetDlgCtrlID(g_hWnd);
    nmbh.iItem=itemval;
    nmbh.iSubItem=0;
    nmbh.uNewState=0;
    nmbh.uOldState=0;
    nmbh.uChanged=0;
    nmbh.uKeyFlags=0;
    SendMessage(GetParent(g_hWnd), WM_NOTIFY,(WPARAM)g_hWnd,(LPARAM)&nmbh);
    See the post#2
    Victor Nijegorodov

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Quote Originally Posted by Igor Vartanov View Post
    You are not allowed to send or post message which parameter passes pointers over. While valid in the context of your process, the pointer is invalid in the context of another one. Processes do not share their memory spaces. Any Win32 process memory is virtualized and isolated from others.
    To read/write memory of another process you use the ReadProcessMemory and WriteProcessMemory API functions.

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

    To use these functions you need the appropriate permissions
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    You might also like to read Windows cia c/c++ by Jeffrey Richter which discusses breaking through process boundary walls
    http://www.amazon.co.uk/Windows-Via-...effrey+richter
    Last edited by 2kaud; November 5th, 2013 at 10:31 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Quote Originally Posted by haroonrulz View Post
    Thanks for explaining WM_NOTIFY , I do understand it.
    If you really did, the first thing you would do is stopping sending it. While you still do send it, to some reason beyond my understanding.

    You had a plan, and you were told that the plan is of no use. A really logical thing would be to get back and re-think the whole idea. Did you ever hear of mouse_event or SendInput?

    But my program is not working in the application which in which I need to automate. You can see from the picture that the Expected(fig1) and Actual(fig2) are different.Some number is getting displayed before OK,Cancel. If you know any useful link,please let me know.Also let me know if I am doing any mistake in constructing structure below,
    Okay, it seems we're going in circles. The snippet you post here over and over must have nothing to do with the numbers, and the root cause is somewhere else, in your code or not, I don't know. Do you really expect anybody would understand the problem having just a description like this, a couple of weird screenshots and a trivial code snippet that makes no sense, especially in the context of your automation story? I tell you again that sending WM_NOTIFY itself is a mistake. If you need a click, you send a click, but not a fake notification about something that never happened.

    And the last thing. Sending WM_NOTIFY is a privilege of common controls. Your code never should do that.

    From MSDN:
    Sent by a common control to its parent window when an event has occurred or the control requires some information.
    Best regards,
    Igor

  13. #13
    Join Date
    Jun 2012
    Posts
    19

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    This is the whole story>
    user has testcase --> writes the script according to my program --> executes the script --> the script simulates the application.similar to QTP

    Did you ever hear of mouse_event or SendInput?
    Yes, I know about these.But I could not use this as user may use this script in different system which has different resolution. This is not generic. If you had worked in QTP this is called "Analog Recording" record and playback.

    but not a fake notification about something that never happened.
    Yes, I am sending fake notification that I had made a click to target application <-- This is generic. If the user uses this script he can run in any system. If you had worked in QTP this is called "Context-sensitive" record and playback.

    I am trying to build a tool which is context-sensitive based and not Analog Based (Mouse_event/sendInput).
    Thanks for being patient and replying to each of my question.

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Cannot automate/simulate listview click of another process (C++/Win32)

    Quote Originally Posted by haroonrulz View Post
    Yes, I know about these.But I could not use this as user may use this script in different system which has different resolution. This is not generic.
    It is absolutely generic. Your user has to write script thinking not in absolute screen positions (that may depend on screen resolution) but having in mind some more generic approach. Like list items and subitems. Your hook dll code then queries the list control for the rectangle to click in (LVM_GETITEMRECT, LVM_GETSUBITEMRECT), and simulates the click with SendInput positioned inside the rect.

    BTW, did you notice the post #8? In fact Arjay gave you another hint on a way to go.

    Yes, I am sending fake notification that I had made a click to target application <-- This is generic.
    What good in being generic without making any sense?

    Thanks for being patient and replying to each of my question.
    You're welcome.
    Last edited by Igor Vartanov; November 6th, 2013 at 02:36 AM.
    Best regards,
    Igor

Tags for this Thread

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

Featured