Page 2 of 7 FirstFirst 1234 ... LastLast
Results 11 to 20 of 70

Thread: clicking a button

  1. #11
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    I'll take a look ty, and btw I did that with the tree view:
    I'll take another look

    //Handle for the SysTreeView32 Form
    HWND TreeView = FindWindowEx(myClass, NULL, "SysTreeView32", NULL);

  2. #12
    Banned
    Join Date
    May 2010
    Location
    fuck off?
    Posts
    1,145

    Default

    what you trying to do?

    do you know about Spy++?

  3. #13
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    I have an application called AD Pictures by Exclaimer. It's a tool that upload staff pictures and syncs it with active directory. The problem is you have to do this manually, and since There are a lot of forms and things, I tried using macros to automate the process, but it wasn't working for me, so then I thought I will write my own tool that will click through automatically.

    Here is what I currently have
    Code:
    // shoreTel.cpp : Defines the entry point for the application.
    //
    #include "stdafx.h"
    #include "MHC-CM.h"
    #include <iostream>
    #include <shellapi.h>
    #include <windows.h>
    #include <Tlhelp32.h>
    #include <cstdlib>
    #include <Commctrl.h>
    #define  MAX_LOADSTRING 100
    #define  TOGGABLE_KEYS 10002
    #define  IDT_TIMER  WM_USER + 200
    
    
    // Global Variables:
    HINSTANCE hInst;                                // current instance
    TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
    
    
    //Needed for the FindWindowClassContains() & FindWindowTitleContains() Functions
    struct result_stru 
    {
        char* text;
        UINT (__stdcall*GET)(HWND,LPSTR,UINT);
        HWND hRet;
    };
    
    
    // Forward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    BOOL                staticDlg(HINSTANCE , int );
    BOOL WINAPI            StruEnumProc( HWND , result_stru* );
    HWND                FindWindowTitleContains( char* );
    HWND                FindWindowClassContains( char* );
    
    
    bool msgbox = true;
    
    
    DWORD dwLastError = GetLastError();
    
    
    VOID CenterWindow(HWND hwnd, HWND hwndParent, int Width, int Height)
    {
        /* Variables */
        RECT rc;
     
        /* If Parent Window Is Set As Null, Get The Desktop Window */
        if(hwndParent == NULL)
        {
            hwndParent = GetDesktopWindow();
        }
     
        /* Get Parent Client Area Measurements */
        GetClientRect(hwndParent, &rc);
     
        /* Center The Window */
        MoveWindow(hwnd, (rc.right - rc.left - Width) / 2, (rc.bottom - rc.top - Height) / 2, Width, Height, TRUE);
     
        return;
    }
    
    
    /* These functions allow to find the correct Window & Class Name */
    BOOL WINAPI StruEnumProc( HWND hwnd, result_stru* stru )
    {
        char loc_buf[128];
        stru->GET( hwnd, loc_buf, 127 );
        if( strstr( loc_buf, stru->text ) ) 
        {
            stru->hRet = hwnd;
            return FALSE;
        }
        return TRUE;
    }
    
    
    //case sensitive!
    HWND FindWindowTitleContains( char* text )
    {
        result_stru res = { text, (UINT (__stdcall *)(HWND,LPSTR,UINT))GetWindowTextA, 0 };
        EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
        return res.hRet;
    }
    
    
    //case sensitive!
    HWND FindWindowClassContains( char* text )
    {
        result_stru res = { text, RealGetWindowClassA, 0 };
        EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
        return res.hRet;
    }
    
    
    void GetDesktopResolution(int& width, int& height)
    {
        RECT desktop;
        // Get a handle to the desktop window
        const HWND hDesktop = GetDesktopWindow();
        // Get the size of screen to the variable desktop
        GetWindowRect(hDesktop, &desktop);
        // The top left corner will have coordinates (0,0)
        // and the bottom right corner will have coordinates
        // (horizontal, vertical)
        width = desktop.right;
        height = desktop.bottom;
    }
    
    
    bool isRunning()
    {
        //This checks if the process is already running so you don't have multiple instances running.
        HANDLE handle = CreateMutex(NULL, true, "desktopOverlay");
        if(GetLastError() != ERROR_SUCCESS)
        {
            MessageBox(0, "Process is already running", "Warning", MB_ICONWARNING);
            return false;
        }
        else
            return true;
    }
    
    
    char* MakeLower( char* strLower )
    {
        size_t size = strlen(strLower);
        char *newstr = (char*)malloc(size);
    
    
        for(int loop = 0; loop < strlen(newstr); loop++)
        {
            newstr[loop] = tolower(strLower[loop]);
        }
    
    
        return newstr;
    }
    
    
    bool CompareData( const unsigned char* pbData, const unsigned char* pbMask, const char* pszString ) 
    { 
        for ( ; *pszString; ++pszString, ++pbData, ++pbMask ) 
        {
            if ( *pszString == 'x' && *pbData != *pbMask ) 
                return FALSE; 
        }
    
    
        return (*pszString) == NULL; 
    }
    
    
    unsigned long FindPattern( HANDLE hProcess, unsigned long dwAddress, unsigned long dwLength, unsigned char* pbMask, char* pszString ) 
    {
        unsigned long dwResult = NULL;
        unsigned char* pbBuffer = (unsigned char*) malloc( dwLength );
    
    
        if ( pbBuffer )
        {
            if ( ReadProcessMemory( hProcess, (void*) dwAddress, pbBuffer, dwLength, NULL ) )
            {
                for ( unsigned long i = 0; i < dwLength; i++ ) 
                {
                    if ( CompareData( (BYTE*) (pbBuffer + i), pbMask, pszString ) ) 
                    {
                        dwResult = (unsigned long)(dwAddress + i);
                        break;
                    }
                }
            }
    
    
            free( pbBuffer );
            pbBuffer = NULL;
        }
    
    
        return dwResult; 
    }
    
    
    unsigned long GetProcessId( char* pszProcessName )
    {
        unsigned long dwResult = 0;
    
    
        HANDLE hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
        if ( hSnapshot )
        {
            PROCESSENTRY32 processEntry = { sizeof( PROCESSENTRY32 ) };
            if ( Process32First( hSnapshot, &processEntry ) )
            {
                do
                {
                    if ( strcmp( processEntry.szExeFile, pszProcessName ) == 0 ) 
                    {
                        dwResult = processEntry.th32ProcessID;
                        break;
                    }
                }
                while ( Process32Next( hSnapshot, &processEntry ) );
            }
    
    
            if ( hSnapshot )
            {
                CloseHandle( hSnapshot );
                hSnapshot = NULL;
            }
        }
    
    
        return dwResult;
    }
    
    
    unsigned long GetModuleBase( unsigned long dwPID, char* pszModuleName, unsigned long* pdwSize )
    {
        unsigned long dwResult = 0;
    
    
        HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
        if ( hSnapshot )
        {
            MODULEENTRY32 moduleEntry = { sizeof( MODULEENTRY32 ) };
            if ( Module32First( hSnapshot, &moduleEntry ) )
            {
                do
                {
                    if ( strcmp( moduleEntry.szModule, pszModuleName ) == 0 ) 
                    {
                        dwResult = (unsigned long) moduleEntry.modBaseAddr;
    
    
                        if ( pdwSize )
                            *pdwSize = moduleEntry.modBaseSize;
    
    
                        break;
                    }
                }
                while ( Module32Next( hSnapshot, &moduleEntry ) );
            }
    
    
            if ( hSnapshot )
            {
                CloseHandle( hSnapshot );
                hSnapshot = NULL;
            }
        }
    
    
        return dwResult;
    }
    
    
    int CheckProcess( char* ProcessName )
    {
        int n_PId=0;
    
    
        HANDLE n_NewSnapshot;
        PROCESSENTRY32 procA;
        n_NewSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
            
        if (n_NewSnapshot==INVALID_HANDLE_VALUE) 
        {
            CloseHandle(n_NewSnapshot);
            return 0;
        }
    
    
        procA.dwSize = sizeof(procA);
    
    
        while (Process32Next(n_NewSnapshot,&procA)) 
        {
            if(_stricmp(MakeLower(procA.szExeFile), MakeLower(ProcessName)) == 0) 
            {
                n_PId = procA.th32ProcessID;
            }
        }
    
    
        CloseHandle(n_NewSnapshot);
        return n_PId;
    }
    
    
    void SetEnterKey( BOOL bState )
    {
        BYTE keyState[256];
    
    
        GetKeyboardState((LPBYTE)&keyState);
        if((bState && !(keyState[VK_RETURN] & 1)) || (!bState && (keyState[VK_RETURN] & 1)))
        {
            // Simulate a key press
            keybd_event(VK_RETURN, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
    
    
            // Simulate a key release
            keybd_event(VK_RETURN, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
        }
    }
    
    
    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    
    
        int width = 0;
        int height = 0;
        LRESULT treeItem = 0;
    
    
        //Check if this process is running
        if(isRunning() == false)
            return 0;
        
        char szWindowClass[] = "MHC-CM";
        MSG msg;
    
    
        WNDCLASSEX wcex            = {0};
        wcex.cbSize                = sizeof(WNDCLASSEX);
        wcex.style                = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc        = WndProc;
        wcex.hInstance            = hInstance;
        wcex.hCursor            = LoadCursor( NULL, IDC_ARROW );
        wcex.hbrBackground        = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wcex.lpszClassName        = szWindowClass;
        wcex.hIcon                = LoadIcon(NULL, IDI_APPLICATION);
        wcex.hIconSm            = LoadIcon(NULL, IDI_APPLICATION);
        RegisterClassEx( &wcex );
    
    
        GetDesktopResolution(width, height);
    
    
        //Start our application "AD Pictures"
        ShellExecute(NULL, "open", "c:\\Program Files\\AD Pictures\\AD Pictures.exe", NULL, NULL, SW_SHOWNORMAL);
    
    
        //Main Application Window
        unsigned long dwPID = GetProcessId( "AD Pictures.exe" );
        unsigned long dwSize = 0;
        unsigned long dwBase = GetModuleBase( dwPID, "AD Pictures.exe", &dwSize );
        HANDLE Process = OpenProcess( PROCESS_VM_READ, false, dwPID );
        HWND hWnd = CreateWindowEx(0, szWindowClass, "", WS_POPUP | WS_CAPTION | WS_VISIBLE, 100, 100, 1, 1, NULL, NULL, hInstance, NULL);
    
    
        //Make sure that AD Pictures is running
        if(!Process)
        {
            MessageBox(0, "AD Pictures is not running.", "Warning", MB_ICONWARNING);
            return FALSE;
        }
    
    
        //Pause for 5 seconds to make sure the program is running
        Sleep(5000);
    
    
        //Get handle for Application
        HWND DialogHandle = FindWindowEx(0, 0, "#32770", "AD Pictures by Exclaimer");
        //Get handle for next button
        HWND DialogButtonHandle = FindWindowEx(DialogHandle, 0, "Button", "&Next >");
        //Get handle for Import Multiple Images radio button
        HWND DialogRadioHandle = FindWindowEx(DialogHandle, 0, "Button", "Import Multiple Images");
        //Get handle for treeview
        HWND TreeView = FindWindowEx(DialogHandle, 0, "SysTreeView32", NULL);
    
    
        Sleep(1000);
    
    
        SendMessage(DialogButtonHandle, BM_CLICK, 1, 0);
        Sleep(500);
    
    
        SendMessage(DialogRadioHandle, BM_CLICK, 1, 0);
        Sleep(500);
        SendMessage(DialogRadioHandle, BM_SETCHECK, BST_CHECKED, 0);
        Sleep(500);
        SendMessage(DialogRadioHandle, BM_CLICK, 0, 0);
        Sleep(500);
        SendMessage(DialogRadioHandle, WM_ACTIVATE, WA_ACTIVE, 0);
        Sleep(2000);
        SendMessage(DialogButtonHandle, BM_CLICK, 1, 0);
        Sleep(500);
    
    
        treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
        treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_NEXT, treeItem);
        treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_CHILD, treeItem);
        SendMessage(TreeView, TVM_SELECTITEM, TVGN_CARET, treeItem);
    
    
    
    
        //Do Stuff
        //Application is running
    
    
        //Main handle to AD Pictures
    //    HWND myTitle = FindWindowTitleContains("AD Pictures by Exclaimer");
    //    HWND myClass = FindWindowClassContains("#32770");
    //    HWND application = FindWindow("#32770", "AD Pictures by Exclaimer");
    //    HWND application2 = FindWindow("AD Pictures by Exclaimer", "#32770");
        //Next button
    //    HWND nextButton = FindWindow("Button", "&Next >");
    
    
        //Import Multiple Pictures
    //    HWND radio1 = FindWindow("Button", "Import Multiple Images");
    
    
        //Handle for the SysTreeView32 Form
    //    HWND TreeView = FindWindowEx(myClass, NULL, "SysTreeView32", NULL);
    /*
        if(application || application2)
        {
            
            if(FindWindowEx(application, NULL, "SysTreeView32", NULL))
            {
                MessageBox(0, "SysTreeView32", "Found Window", MB_OK);
            }
            if(FindWindow("Button", "&Next >"))
            {
                MessageBox(0, "&Next", "Found Window", MB_OK);
            }
            if(FindWindow("&Next >", "Button"))
            {
                MessageBox(0, "&Next2", "Found Window", MB_OK);
            }
            if(FindWindow("Button", "Next"))
            {
                MessageBox(0, "Next", "Found Window", MB_OK);
            }
            if(FindWindow("Next", "Button"))
            {
                MessageBox(0, "Next2", "Found Window", MB_OK);
            }
            /*
            MessageBox(0, "App Running", "Found Window", MB_OK);
    
    
            if(nextButton)
            {
                MessageBox(0, "Test1", "Found Window", MB_OK);
                SendMessage(nextButton, BM_CLICK, 0, 0);
            }
            else if(radio1)
            {
                MessageBox(0, "Test2", "Found Window", MB_OK);
                SendMessage(radio1, BM_CLICK, 0, 0);
                SendMessage(nextButton, BM_CLICK, 0, 0);
            }
            else if(TreeView)
            {
                MessageBox(0, "Test3", "Found Window", MB_OK);
                treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
                treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_NEXT, treeItem);
                treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_CHILD, treeItem);
                SendMessage(TreeView, TVM_SELECTITEM, TVGN_CARET, treeItem);
            }
            
        }
        */
    /*
        //This is the OK button you click after cache maintenance
        else if(hWnd5)
        {
            MessageBox(0, "Test7", "Warning", MB_OK);
            if(Process)
            {
                MessageBox(0, "Test8", "Warning", MB_OK);
                //keybd_event(VK_RETURN, NULL, NULL, NULL);
                //keybd_event(VK_RETURN, NULL, KEYEVENTF_KEYUP, NULL);
                SetEnterKey(TRUE);
    
    
                CloseHandle(Process);
            }
        }
    */
        //Activate our Main Window
        CenterWindow(hWnd, NULL, 100, 100);
        ShowWindow(hWnd, SW_HIDE);
        UpdateWindow(hWnd);
    
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    
    
        return (int) msg.wParam;
    }
    
    
    
    
    
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId = 0;
        int wmEvent = 0;
    
    
        switch (message) 
        {
            case WM_CLOSE:
                //PostQuitMessage(0);
            break;
    
    
            case WM_DESTROY:
                //PostQuitMessage(0);
            break;
    
    
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    The first Next button is automated correctly, however this piece:
    Code:
    HWND DialogRadioHandle = FindWindowEx(DialogHandle, 0, "Button", "Import Multiple Images");
    SendMessage(DialogRadioHandle, BM_CLICK, 1, 0);
    Doesn't seem to click the radio button for me. I checked the tool that RR posted, and I also use a tool called Windowse and I KNOW that the class and window names I'm using to reference are correct. I'm not sure what I'm doing wrong though and why the radio button isn't being clicked.

    After that screen, I need to select the path "\\earth\corp\pics" from the tree view list
    Or My network Places -> corp on earth -> Pics

    I seem to get stuck on those 2 automation processes. The rest is automated fine (The next buttons).

    EDIT: Yes I'm familiar with spy++, similar tool to Windowe. spy++ comes with visual studio and I use that as well. I don't think I have a problem with the class and window names though. I'm probably missing some stupid syntax. hmm

  4. #14
    Banned
    Join Date
    May 2010
    Location
    fuck off?
    Posts
    1,145

    Default

    check it by sending a hide event ?

    or better yet, get its WHandle and check that in spy++ or whatever programme you are using to spy on all windows widgets.


    tried BM_SETCHECK ?

  5. #15
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    I'll try the hide event @ home.

    Here is what I tried:
    Code:
    SendMessage(DialogRadioHandle, BM_CLICK, 1, 0);    
    Sleep(500);    
    SendMessage(DialogRadioHandle, BM_SETCHECK, BST_CHECKED, 0);   
     Sleep(500);    
    SendMessage(DialogRadioHandle, BM_CLICK, 0, 0);    
    Sleep(500);    
    SendMessage(DialogRadioHandle, WM_ACTIVATE, WA_ACTIVE, 0);    
    Sleep(2000);
    And I am getting a window handle via FindWindowEx() is there another method you recommend?

  6. #16
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    Make sure that this Radio Button is not inside of some container like Group Box or something. If it is, you have to get handle of the container first and then get handle of radio button inside of it.

    Some screenshots would be helpful.

  7. #17

  8. #18
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    SendInput won't help you, unless you want to emulate clicks in certain screen spots instead of sending messages to buttons.

  9. #19
    Banned
    Join Date
    May 2010
    Location
    fuck off?
    Posts
    1,145

    Default

    get the handle...FindWindowEx

    and check it with spy++

    then you know what you got...you probably don't have it right...also check buttons may have 2 parts a label and a check box

  10. #20
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Here are 2 screenies.
    Click image for larger version. 

Name:	second screen.png 
Views:	9 
Size:	35.1 KB 
ID:	586Click image for larger version. 

Name:	windowse.png 
Views:	8 
Size:	9.4 KB 
ID:	587

    First screenie is the radio button. The second screen shows you the parent handles of that radio button when I mouse over it.

    So I tried this:

    Code:
    HWND DialogHandle = FindWindowEx(0, 0, "#32770", "AD Pictures by Exclaimer");
    HWND DialogHandle2 = FindWindowEx(DialogHandle, 0, "#32770", "AD Pictures by Exclaimer");
    HWND DialogRadioHandle = FindWindowEx(DialogHandle2, 0, "Button", "Import Multiple Images");
    SendMessage(DialogRadioHandle, BM_CLICK, 1, 0);
    Still nothing. I'll see if spy++ has different info

Posting Permissions

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