Thread: DebugActiveProcess()

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    DebugActiveProcess()

    This function is way too hard for me to work out. Does anyone have any sample code that uses it? All I want to do is be able to manipulate the list boxes and edit controls of other processes, but DebugActiveProcess() has about a million aspects to it, too many for me the comprehend!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    IF you want to "manipulate list boxes and edit controls of other processes" then you don't need DebugActiveProcess() as thats only required for walking through the assembler code of the program and possibly modifiying it.

    What you want to do is send messages to the controls in question to get the text thats there and place new text (for example) in them.

    Code:
    LRESULT SendMessage(HWND hWnd,   // Target window handle
        UINT Msg,     // message to send and arguments (see MSDN)
        WPARAM wParam,
        LPARAM lParam
    );

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Not really using this function, but here's a little debugging example I did a while ago - http://cboard.cprogramming.com/showt...ghlight=thread

    In my example I created the debugged process with CreateProcess, but you can attach to a live process with DebugActiveProcess if you so wish. The important bit is working with the WaitForDebugEvent and the ContinueDebugEvent functions.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Elixia, the reason I'm asking about DebugActiveProcess() is that Windows doesn't just let you access data from other processes. Fordy, I'll have a look at your code when I get a chance, but it sounds like it goes a bit deeper than I need. But thanks.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Reading you original post again, I think you may be missing an opportunity of simply manipulating these controls with messages (as _Elixia_ pointed out)

    >>Windows doesn't just let you access data from other processes

    Well actually it does.......but what do you want to do exactly....can you give a simple example

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Originally posted by bennyandthejets
    Elixia, the reason I'm asking about DebugActiveProcess() is that Windows doesn't just let you access data from other processes. Fordy, I'll have a look at your code when I get a chance, but it sounds like it goes a bit deeper than I need. But thanks.
    Windows does allow you to access data from other processes if you ask it nicely. Check out the ReadProcessMemory function, for example...

    To just play with edits and listviews on another processes form however, you only need to use messages, like I stated above.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Elixia and Fordy, I've tried to do that, and it just won't work. I located the handle to a window using Spy++, then, without exiting the process of the window, I attempted to latch onto the window through another process. It acted as if the hwnd I was attempting to use didn't exist. Here is the code I used:

    Code:
    HWND hwnd=(HWND)0x0000####; //the hwnd, from Spy++
    
    SendMessage(hwnd,WM_SETTEXT,0,"New Text");
    Nothing happens as a result of this code.

    PS. I am a bit drunk so excuse any differences in behavior.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I have a feeling that the actual values of HWNDs are reletive to the app that has them. SO that number may be only useful to spy++...not you

    Instead of taking this approach...look a little more at the info spy++ is giving.....Go into spy and click Spy->Windows. Then Search->Find Window and select your window as you like.

    When you press OK, it takes you to the TreeView screen and shows the control with respect to it's parent windows. FOr example an edit box might be shown as

    00112233 "Some Main Window" ParentClassName
    |
    |
    00223344 "Some Modal Dialog" ChildClassName
    |
    |
    00334455 "" Edit
    So if I wanted to access this edit, I could use FindWindow to find the First top level window (spy++ has given me the class anme and title). The iterate through the child windows with FindWindowEx until I reach the control I want. At that point I have the HWND I need.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Fordy, that's exactly how they work.

    bennyandthejets: You need to traverse the set of windows yourself (like Fordy says), and then process the child windows as you find them until you reach the window you require. Using the handle from another program is guaranteed to give you problems.

    This should help you:

    Code:
    DWORD dwlp;
    
    EnumWindows((WNDENUMPROC)EnumProc,dwlp);
    Later on in the program...

    Code:
    BOOL __stdcall EnumProc(HWND hWnd,LPARAM lp)
    {
        DWORD * pPid;   
        DWORD result;
        void *hg; 
        DWORD id;
    
       if(hWnd==NULL)
          return FALSE;
    
       hg = GlobalAlloc(GMEM_SHARE,sizeof(DWORD));
       pPid = (DWORD *)GlobalLock(hg);
    
       result = GetWindowThreadProcessId(hWnd,pPid);
    
       if(result){
          char title[110];
          char className[95];
          char totalStr[256];
    
          GetClassName(hWnd,className,95);
          GetWindowText(hWnd,title,110);
          id=*pPid;
          ultoa(id,totalStr,10);
          strcat(totalStr,"\t");
          if(title){
             strcat(totalStr,title);
             strcat(totalStr,"\t");
          }
          strcat(totalStr,className);
          // TODO: Do something with totalStr
       }
       else{
          GlobalUnlock(hg);
          GlobalFree(hg);
          return false;
       }
       GlobalUnlock(hg);
       GlobalFree(hg);
       return true;
    }

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'll give it a shot, it sounds like the real deal. Thanks.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oops, I forgot about this thread.

    No, it doesn't work. I located the desired window using FindWindow() and FindWindowEx(), but I am unable to do anything with it, not even use GetWindowText(). Is there something I'm missing here?

    Code:
    hFind=FindWindow("myWindowClass",NULL);
    hChildFind=FindWindowEx(hFind,NULL,"EDIT",NULL);
    GetWindowText(hChildFind,chName,255);
    GetWindowText() returns 0. Also, in the Win32 Programmer's Reference, it states that GetWindowText() cannot retrieve the text of an edit control of another process. That says it all!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DebugActiveProcess() and hooking
    By bennyandthejets in forum Windows Programming
    Replies: 4
    Last Post: 07-21-2003, 01:35 PM