Page 7 of 7 FirstFirst ... 567
Results 61 to 70 of 70

Thread: clicking a button

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

    Default

    I was actually using cout << szBuffer to display the buffer.

    I rewrote the code for explorer.exe (I threw out alot of the previous code since it wasn't needed). Hopefully this will make it easier to debug the printing and get the treeview working.

    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <commctrl.h>
    #include <iostream>
    #include <tlhelp32.h>
    
    
    #define process "explorer.exe"
    #define sysTreeView "Tree View"
    
    
    #define MAXTEXTLEN 100
    
    
    using namespace std;
    
    
    TCHAR winname[MAX_PATH];
    
    
    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;
    }
    
    
    HTREEITEM GetItemByName(HWND hWnd, HTREEITEM hItem, LPCTSTR szItemName)
    {
    	if (hItem == NULL)
    	{
    		hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
    	}
    
    
    	Sleep(5000);
    
    
    	while(hItem != NULL)
        {
    		char szBuffer[MAXTEXTLEN+1];
            TV_ITEM item;
    	
            item.hItem = hItem;
            item.mask = TVIF_TEXT | TVIF_CHILDREN;
            item.pszText = szBuffer;
            item.cchTextMax = MAXTEXTLEN;
            
    		SendMessage(hWnd, TVM_GETITEM, sizeof(&item), (LPARAM)&item);
    
    
    		//sprintf_s( szBuffA, "szBuffer: %s\nszItemName: %s\n", szBuffer, szItemName );
    		//printf(szBuffA);
    
    
    		cout << szBuffer << endl;
    		Sleep(1000);
    
    
    		// Did we find it?
            if (lstrcmp(szBuffer, szItemName) == 0)
    		{
                return hItem;
    		}
    
    
    		// Check whether we have child items.
    		if (item.cChildren)
            {
                // Recursively traverse child items.
                HTREEITEM hItemFound, hItemChild;
    
    
                hItemChild = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
    			SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItemChild);
                hItemFound = GetItemByName(hWnd, hItemChild, szItemName);
    
    
                // Did we find it?
                if (hItemFound != NULL)
                    return hItemFound;
            }
    
    
            // Go to next sibling item.
            hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
    		SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
    
    
    		Sleep(1000);
        }
    
    
        // Not found.
        return 0;
    }
    
    
    BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) 
    {
    	//This gets the Window Name
    	SendMessage(hwnd, WM_GETTEXT, sizeof(winname), (LPARAM)winname);
    
    
    	//Checks to see if we can see the TreeView
    	if(strcmp(winname, _T(sysTreeView)) == 0)
    	{	
    		HTREEITEM hItem = GetItemByName(hwnd, NULL, "Computer");
        
    		if(hItem != NULL)
    			SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
    		//return FALSE; // end enumeration
    	}
    
    
    	// must return TRUE; If return is FALSE it stops the recursion
    	return TRUE;
    	Sleep(1000);
    }
    
    
    BOOL CALLBACK WindowHandle(HWND hwnd, LPARAM lParam)
    {
    	char String[255];
    	DWORD dwID;
    
    
    	GetWindowThreadProcessId(hwnd, &dwID) ;
    
    
    	SendMessage(hwnd, WM_GETTEXT, sizeof(String), (LPARAM)String);
    
    
    	EnumChildWindows(hwnd, &EnumChildProc, NULL);
    
    
    	return TRUE ;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        //Hide Console Window
        //FreeConsole();
    
    
        //Get Parent Handle of Application
    	unsigned long dwPID1 = GetProcessId( process );	
    	HANDLE hProcess = OpenProcess(PROCESS_VM_READ|PROCESS_VM_OPERATION, FALSE, dwPID1); 
    
    
        if(!hProcess) 
        {
            MessageBox(NULL, "Program Window not found", "error", MB_OK);
    		    
    		//Start our Application if it doesn't exist
    		ShellExecute(NULL, "open", "C:\\Windows\\explorer.exe", NULL, NULL, SW_SHOWNORMAL);
        }
    
    
    	while(hProcess)
    	{
    		EnumWindows((WNDENUMPROC)WindowHandle, (LPARAM)dwPID1);
    	}
    
    
    	CloseHandle(hProcess);
    
    
    	return 0;
    }
    While debugging the application in explorer.exe (you can see psztext is showing a bad pointer??? :S
    Code:
    -		&item	0x0035f980 {mask=4294967294 hItem=0x76ec3cc3 state=1995193582 ...}	tagTVITEMA *
    		mask	4294967294	unsigned int
    		hItem	0x76ec3cc3	_TREEITEM *
    		state	1995193582	unsigned int
    		stateMask	256	unsigned int
    -		pszText	0x00000000 <Bad Ptr>	char *
    			CXX0030: Error: expression cannot be evaluated	
    		cchTextMax	0	int
    		iImage	3537400	int
    		iSelectedImage	0	int
    		cChildren	0	int
    		lParam	201	long
    		hItem	0x00000000	_TREEITEM *
    +		hWnd	0x006b0518 {unused=??? }	HWND__ *
    +		item	{mask=4294967294 hItem=0x76ec3cc3 state=1995193582 ...}	tagTVITEMA
    		item.cchTextMax	0	int
    		item.hItem	0x76ec3cc3	_TREEITEM *
    		item.mask	4294967294	unsigned int
    +		item.pszText	0x00000000 <Bad Ptr>	char *
    +		szBuffer	0x0035f9a8 ""	char [101]

  2. #62

    Default

    in GetItemByName, before setting up the TV_ITEM structure, you should put memset(&item, 0, sizeof(TV_ITEM)); to initialize the structure as well as memset(szBuffer, 0, sizeof(szBuffer)) to initialize the buffer.
    Add TVIF_HANDLE to item.mask, because you are using the hItem member.
    The wParam in SendMessage for TVM_GETITEM must be zero as well.

    PHP Code:
    char szBuffer[MAXTEXTLEN+1];
    TV_ITEM item;

    memset(&item0sizeof(TV_ITEM));
    memset(szBuffer0sizeof(szBuffer));

    item.hItem hItem;
    item.mask TVIF_TEXT TVIF_CHILDREN TVIF_HANDLE;
    item.pszText szBuffer;
    item.cchTextMax MAXTEXTLEN

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

    Default

    Still no go. This has been driving me crazy because I can't see what's wrong based on all the searches I've been doing and trying.
    It seems it only goes through 1 tree item and then goes back to root, but I'm not sure why hItem would be NULL as it's going through the list. Also the print issue is still ongoing (printing random characters).
    Now it intermittently crashes when I put in the memset. And I added the tvif_handle as well as made wParam 0.

    I'm curious, are you getting it to work on your computer?

  4. #64

    Default

    Quote Originally Posted by James View Post
    I'm curious, are you getting it to work on your computer?
    I never tested this code, and I won't because I don't have the required program you "hook".

    What are you trying to do exactly?

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

    Default

    Right, but you can also hook explorer.exe and that is part of Windows, so you can do the systreeView through that because it's part of it.

    Here is what I mean: Click image for larger version. 

Name:	SysTreeView.png 
Views:	4 
Size:	57.1 KB 
ID:	763

    If you need the app though, PM me and I can probably get it for you.

    What are you trying to do exactly?
    Let's take the image I posted above. Assuming that's the application and the treeview I want to go through...

    if I passed this
    Code:
    //Checks to see if we can view the tree view
        if(strcmp(winname, _T(sysTreeView)) == 0)
        {    
            HTREEITEM hItem = GetItemByName(hwnd, NULL, "Program Files");
        
            if(hItem != NULL)
                SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
            //return FALSE; // end enumeration
        }
    I would want it to go to the root item of the tree view, which from this pic would be "Favorites" <-- go through all the children under favorites then go to Libraries and do the same thing
    Then it would go to C:\ and go through it's children till it gets to Program Files.

    I print szBuffer each item it goes through so I see what it's doing and that I know it's going through each item correctly. So szbuffer should be printing

    Favorites
    Desktop
    Downloads
    Recent Places
    Libraries
    Documents
    Music
    Pictures
    Videos
    Computer
    Local Disk ( C: )

    etc... (atleast that's what I would assume it should be doing)

    Then once it lands on c:\Program Files it stops and continues with the rest of the process (Everything else works perfectly, it's just the systree I'm trying to get to work properly)

    Hope that clears it up.

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

    Default

    Another update.

    Rewrote the treeview function. Here is what I have now.
    Code:
    HTREEITEM GetItemByName(HWND hWnd, HTREEITEM hItem, LPCTSTR szItemName)
    {
    	unsigned long dwPID1 = GetProcessId( process );	
    
    
    	GetWindowThreadProcessId(hWnd, &dwPID1);
    	if(dwPID1) 
    	{
    		HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwPID1); 
    
    
    		if(hProcess) 
    		{
    			DWORD remoteBuffer = (DWORD)::VirtualAllocEx(hProcess, NULL, 335, MEM_COMMIT, PAGE_READWRITE);
    				if(remoteBuffer) 
    				{
    					HTREEITEM Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
    					
    					while(Root) 
    					{
    						TVITEM tvItem;
    						tvItem.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_HANDLE;
    						tvItem.hItem = Root;
    						tvItem.pszText = (LPSTR)remoteBuffer + sizeof(TVITEM);
    						tvItem.cchTextMax = 255;
    
    
    						wcout << &tvItem << ' ' << Root << ' ' << remoteBuffer << endl;
    
    
    						SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)Root);
    
    
    						if(WriteProcessMemory(hProcess, (LPVOID)remoteBuffer, (LPCVOID)&tvItem, sizeof(TVITEM), 0)) 
    						{
    							SendMessage(hWnd, TVM_GETITEM, 0, remoteBuffer);
    							wchar_t bytRtn[255];
    							int rAdd = remoteBuffer + sizeof(TVITEM)*2;
    							
    							SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)remoteBuffer);
    
    
    							if(ReadProcessMemory(hProcess, (LPCVOID)rAdd, (LPVOID)&bytRtn, 255, 0)) 
    							{
    								wcout << bytRtn << endl;
    							}
    							Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)Root);
    						}
    					}
    					VirtualFreeEx(hProcess, (LPVOID)remoteBuffer, 335, MEM_RELEASE);
    				}
    			CloseHandle(hProcess);
    		}
    	}
    
    
    	return 0;
    }
    It traverses through all items, HOWEVER the only thing now is that the buffer now prints the address of the text, but I'm trying to figure out how to read text from that pointer? Hope this makes sense.

  7. #67

    Default

    I'm not familiar with C++, but wcout seems to be unicode, and you are trying to print an ANSI string, so the unicode version will show a pointer.
    I don't recommend to use VirtualAlloc and WriteProcessMemory API to allocate memory as they are complicated, you should use malloc or eventually HeapAlloc.
    Last edited by Ley0k; February 21st, 2013 at 05:12 PM.

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

    Default

    I found this code, and it prints correctly, but the issue I'm running in to is it doesn't work with my enumeration method that I'm using in my code above. Part of the problem is I compile in Multibyte, where as this code is UNICODE I believe and so I can't seem to figure out how to make it work with my code above.

    Here is what I currently have:
    Code:
    #include "stdafx.h" 
    #include <windows.h> 
    #include <iostream> 
    #include <tlhelp32.h> 
    #include <commctrl.h> 
    
    
    using namespace std; 
    
    
    #define process "explorer.exe" 
    #define WindowName "Tree View" 
    
    
    HWND sysTree;
    
    
    BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) 
    {
        sysTree = FindWindowEx(hwnd, NULL, L"SysTreeView32", NULL);
    
    
        if(!sysTree)
            return TRUE;
    
    
        return FALSE;
    }
    
    
    TCHAR winname[MAX_PATH]; 
    
    
    bool setDebug() 
    {
    	HANDLE hToken;
    
    
    	if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    	{
    		TOKEN_PRIVILEGES tp;
    		LUID luid;
    		TOKEN_PRIVILEGES tpPrevious;
    		DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
    		
    		if(LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) 
    		{
    			tp.PrivilegeCount = 1;
    			tp.Privileges[0].Luid = luid;
    			tp.Privileges[0].Attributes = 0;
    			
    			if(AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &tpPrevious, &cbPrevious)) 
    			{
    				tpPrevious.PrivilegeCount = 1;
    				tpPrevious.Privileges[0].Luid = luid;
    				tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
    				
    				if(AdjustTokenPrivileges( hToken, FALSE, &tpPrevious, cbPrevious, NULL, NULL )) 
    				{
    					CloseHandle(hToken);
    					return true;
    				}
    			}
    		}
    	}
    	CloseHandle(hToken);
    	return false;
    }
    
    
    void printTreeviewNodes(HWND treeview) 
    {
    	DWORD pID;
    	GetWindowThreadProcessId(treeview, &pID);
    	
    	if(pID) 
    	{
    		if(setDebug()) 
    		{
    			HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pID);
    
    
    			if(hProcess) 
    			{
    				DWORD remoteBuffer = (DWORD)::VirtualAllocEx(hProcess, NULL, 335, MEM_COMMIT, PAGE_READWRITE);
    				
    				if(remoteBuffer) 
    				{
    					HTREEITEM Root = (HTREEITEM)::SendMessage(treeview, TVM_GETNEXTITEM, TVGN_ROOT, 0);
    					
    					while(Root) 
    					{
    						TVITEM tvItem;
    						tvItem.mask = TVIF_TEXT;
    						tvItem.hItem = Root;
    						tvItem.pszText = (LPWSTR)remoteBuffer + sizeof(TVITEM);
    						tvItem.cchTextMax = 255;
    						
    						if(WriteProcessMemory(hProcess, (LPVOID)remoteBuffer, (LPCVOID)&tvItem, sizeof(TVITEM), 0)) 
    						{
    							SendMessage(treeview, TVM_GETITEM, 0, remoteBuffer);
    							
    							wchar_t bytRtn[255];
    							int rAdd = remoteBuffer + sizeof(TVITEM)*2;
    							
    							if(ReadProcessMemory(hProcess, (LPCVOID)rAdd, (LPVOID)&bytRtn, 255, 0)) 
    							{
    								wcout << bytRtn << endl;
    								
    								if(wcscmp(bytRtn, L"Computer") == 0)
    								{
    									SendMessage(treeview, TVM_EXPAND, TVM_EXPAND, (LPARAM)Root);
    								}
    							}
    							Root = (HTREEITEM)::SendMessage(treeview, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)Root);
    						}
    					}
    					VirtualFreeEx(hProcess, (LPVOID)remoteBuffer, 335, MEM_RELEASE);
    				}
    			}
    			CloseHandle(hProcess);
    		}
    	}
    }
    
    
    int main(int argc, char *argv[], char *envp[])  
    { 
        HWND hChild;
        HWND mainApp = FindWindow(L"#32770", L"AD Pictures by Exclaimer");
        hChild = FindWindowEx(mainApp, NULL, L"#32770", L"AD Pictures by Exclaimer");
        hChild = FindWindowEx(hChild, NULL, L"#32770", L"Browse For Folder");
    
    
    	if (!hChild) 
        {
            MessageBox(NULL, L"Program Window not found", L"error", MB_OK);
            return 0;
        }
    
    
        EnumChildWindows(hChild, &EnumChildProc, NULL);
    
    
    	printTreeviewNodes(sysTree);
    
    
        Sleep(15000); 
        return 0; 
    }
    It auto goes to the folder Computer and expands that selection just as I specified it in the code.

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

    Default

    I got this code working, now I just need to figure out how to scroll through all items and not just the ones visible...

    When I change this:
    Code:
    Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)Root);
    to
    Code:
    Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)Root); or make the 3rd parm TVGN_CHILD
    This still doesn't scroll through all the items.

    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <commctrl.h>
    #include <iostream>
    #include <tlhelp32.h>
    
    
    
    
    #define process L"explorer.exe"
    #define sysTreeView L"Tree View"
    
    
    #define MAXTEXTLEN 256
    
    
    using namespace std;
    
    
    WCHAR winname[MAX_PATH];
    
    
    DWORD GetProcessId(const std::wstring& processName)
    {
    	PROCESSENTRY32 processInfo;
    	processInfo.dwSize = sizeof(processInfo);
    
    
    	HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    
    
    	if(processesSnapshot == INVALID_HANDLE_VALUE)
    		return 0;
    
    
    	Process32First(processesSnapshot, &processInfo);
    
    
    	if(!processName.compare(processInfo.szExeFile))
    	{
    		CloseHandle(processesSnapshot);
    		return processInfo.th32ProcessID;
    	}
    
    
    	while(Process32Next(processesSnapshot, &processInfo))
    	{
    		if(!processName.compare(processInfo.szExeFile))
    		{
    			CloseHandle(processesSnapshot);
    			return processInfo.th32ProcessID;
    		}
    	}
    	
    	CloseHandle(processesSnapshot);
    	return 0;
    }
    
    
    HTREEITEM GetItemByName(HWND hWnd, HTREEITEM hItem, LPCWSTR szItemName)
    {
    	if (hItem == NULL)
    	{
    		hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
    	}
    
    
    	Sleep(1000);
    
    
    	while(hItem != NULL)
        {
    		WCHAR szBuffer[MAXTEXTLEN+1];
            TV_ITEM item;
    	
            item.hItem = hItem;
            item.mask = TVIF_TEXT | TVIF_CHILDREN;
            item.pszText = szBuffer;
            item.cchTextMax = MAXTEXTLEN;
            
    		SendMessage(hWnd, TVM_GETITEM, sizeof(&item), (LPARAM)&item);
    
    
    		//sprintf_s( szBuffA, "szBuffer: %s\nszItemName: %s\n", szBuffer, szItemName );
    		//printf(szBuffA);
    
    
    		wcout << szBuffer << endl;
    		Sleep(1000);
    
    
    		// Did we find it?
            if (lstrcmp(szBuffer, szItemName) == 0)
    		{
                return hItem;
    		}
    
    
    		// Check whether we have child items.
    		if (item.cChildren)
            {
                // Recursively traverse child items.
                HTREEITEM hItemFound, hItemChild;
    
    
                hItemChild = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
    			SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItemChild);
                hItemFound = GetItemByName(hWnd, hItemChild, szItemName);
    
    
                // Did we find it?
                if (hItemFound != NULL)
                    return hItemFound;
            }
    
    
            // Go to next sibling item.
            hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
    		SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
    
    
    		Sleep(1000);
        }
    
    
        // Not found.
        return 0;
    }
    
    
    BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) 
    {
    	//This gets the Window Name
    	SendMessage(hwnd, WM_GETTEXT, sizeof(winname), (LPARAM)winname);
    
    
    	//Checks to see if we can see the TreeView
    	if(wcscmp(winname, sysTreeView) == 0)
    	{	
    		HTREEITEM hItem = GetItemByName(hwnd, NULL, L"Computer");
        
    		if(hItem != NULL)
    			SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
    		//return FALSE; // end enumeration
    	}
    
    
    	// must return TRUE; If return is FALSE it stops the recursion
    	return TRUE;
    	Sleep(1000);
    }
    
    
    BOOL CALLBACK WindowHandle(HWND hwnd, LPARAM lParam)
    {
    	char String[255];
    	DWORD dwID;
    
    
    	GetWindowThreadProcessId(hwnd, &dwID) ;
    
    
    	SendMessage(hwnd, WM_GETTEXT, sizeof(String), (LPARAM)String);
    
    
    	EnumChildWindows(hwnd, &EnumChildProc, NULL);
    
    
    	return TRUE;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
     	std::wstring processName = process;
    	
    	DWORD processID = GetProcessId(processName);
    	HANDLE hProcess = OpenProcess(PROCESS_VM_READ|PROCESS_VM_OPERATION, FALSE, processID); 
    
    
    	if(processID == 0)
    		std::wcout << "Could not find " << processName.c_str() << std::endl;
    	else
    		EnumWindows((WNDENUMPROC)WindowHandle, (LPARAM)processID);
    
    
    	CloseHandle(hProcess);
    
    
    	return 0;
    }

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

    Default

    Good news... I got it working FINALLY!!!! It's perfect and completely automated just how I wanted it. The only thing I would like to do at this point is optimize it if possible. Other than that, flawless victory. I'll share the code for anyone interested in the final result.

    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <commctrl.h>
    #include <iostream>
    #include <tlhelp32.h>
    
    
    #define process L"AD Pictures.exe"
    #define procWindow L"AD Pictures by Exclaimer"
    #define sysTreeView L"Tree View"
    #define editButton L"Edit..."
    #define nextButton L"&Next >"
    #define multImages L"Import Multiple Images"
    
    
    #define MAXTEXTLEN 256
    
    
    using namespace std;
    
    
    WCHAR winname1[MAX_PATH];
    WCHAR winname2[MAX_PATH];
    
    
    DWORD GetProcessId(const std::wstring& processName)
    {
    	PROCESSENTRY32 processInfo;
    	processInfo.dwSize = sizeof(processInfo);
    
    
    	HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    
    
    	if(processesSnapshot == INVALID_HANDLE_VALUE)
    		return 0;
    
    
    	Process32First(processesSnapshot, &processInfo);
    
    
    	if(!processName.compare(processInfo.szExeFile))
    	{
    		CloseHandle(processesSnapshot);
    		return processInfo.th32ProcessID;
    	}
    
    
    	while(Process32Next(processesSnapshot, &processInfo))
    	{
    		if(!processName.compare(processInfo.szExeFile))
    		{
    			CloseHandle(processesSnapshot);
    			return processInfo.th32ProcessID;
    		}
    	}
    	
    	CloseHandle(processesSnapshot);
    	return 0;
    }
    
    
    HTREEITEM GetItemByName(HWND hWnd, HTREEITEM hItem, LPCWSTR szItemName)
    {
    	unsigned long dwPID1 = GetProcessId( process );	
    
    
    	GetWindowThreadProcessId(hWnd, &dwPID1);
    	if(dwPID1) 
    	{
    		HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwPID1); 
    
    
    		if(hProcess) 
    		{
    			DWORD remoteBuffer = (DWORD)::VirtualAllocEx(hProcess, NULL, 335, MEM_COMMIT, PAGE_READWRITE);
    				if(remoteBuffer) 
    				{
    					HTREEITEM Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
    					
    					while(Root) 
    					{
    						TVITEM tvItem;
    						tvItem.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_HANDLE;
    						tvItem.hItem = Root;
    						tvItem.pszText = (LPWSTR)remoteBuffer + sizeof(TVITEM);
    						tvItem.cchTextMax = 255;
    
    
    						//SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)Root);
    
    
    						if(WriteProcessMemory(hProcess, (LPVOID)remoteBuffer, (LPCVOID)&tvItem, sizeof(TVITEM), 0)) 
    						{
    							SendMessage(hWnd, TVM_GETITEM, 0, remoteBuffer);
    							wchar_t bytRtn[255];
    							int rAdd = remoteBuffer + sizeof(TVITEM)*2;
    							
    							if(ReadProcessMemory(hProcess, (LPCVOID)rAdd, (LPVOID)&bytRtn, 255, 0)) 
    							{
    								//wcout << bytRtn << endl;
    								if(wcscmp(bytRtn, szItemName) == 0)
    								{
    									SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)Root);
    									Sleep(500);
    									return Root;
    								}
    							}
    							Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)Root);
    						}
    					}
    					VirtualFreeEx(hProcess, (LPVOID)remoteBuffer, 335, MEM_RELEASE);
    				}
    			CloseHandle(hProcess);
    		}
    	}
    
    
    	return 0;
    }
    
    
    BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) 
    {
    	HWND ret = GetAncestor(hwnd, GA_ROOTOWNER);
    
    
    	//This gets the Window Name
    	SendMessage(ret, WM_GETTEXT, sizeof(winname2), (LPARAM)winname2);
    	SendMessage(hwnd, WM_GETTEXT, sizeof(winname1), (LPARAM)winname1);
    
    
    	//Checks to see if we can view the tree view
    	if(wcscmp(winname2, procWindow) == 0)
    	{	
    		//This gets the Window Name
    		SendMessage(hwnd, WM_GETTEXT, sizeof(winname1), (LPARAM)winname1);
    
    
    		if(wcscmp(winname1, sysTreeView) == 0)
    		{
    			HTREEITEM hItem = GetItemByName(hwnd, NULL, L"Computer");
        
    			if(hItem != NULL)
    			{
    				SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
    
    
    				HTREEITEM hItem2 = GetItemByName(hwnd, NULL, L"Corp (H:)");
        
    				if(hItem2 != NULL)
    				{
    					SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem2);
    									
    					HTREEITEM hItem3 = GetItemByName(hwnd, NULL, L"Pics");
        
    					if(hItem3 != NULL)
    					{
    						SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem3);
    					}
    
    
    				}
    			}
    			//return FALSE; // end enumeration
    		}
    	}
    
    
    	//Checks to see if we can view the edit button
    	if(wcscmp(winname1, editButton) == 0)
    	{
    		if(IsWindowVisible(hwnd)) 
    		{
    			//Get Owner Window
    			unsigned long dwPID1 = GetProcessId( process );
    			HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID1);
    
    
    			if(hProcess != NULL)
    			{
    				TerminateProcess(hProcess, 0);
    				CloseHandle(hProcess);
    			}
    			//return FALSE; // end enumeration
    		} 
    	}
    
    
    	//Checks to see if we can view the next button
    	//We don't want the next button triggered if the systreeview is on
    	if((wcscmp(winname1, nextButton) == 0) && (wcscmp(winname1, sysTreeView) != 0))
    	{
    		SendMessage(hwnd, BM_CLICK, 1, 0);
    		Sleep(500);
    		//return FALSE; // end enumeration
    	}
    
    
    	//Checks to see if we can view the next button
    	if((wcscmp(winname1, nextButton) == 0) && (wcscmp(winname1, sysTreeView) == 0))
    	{
    		SendMessage(hwnd, BM_CLICK, 1, 0);
    		Sleep(500);
    		//return FALSE; // end enumeration
    	}
    	
    	//Checks to see if we can view the mult images radio button
    	if(wcscmp(winname1, multImages) == 0)
    	{
    		SendMessage(hwnd, BM_CLICK, 1, 0);
    		Sleep(500);
    		//return FALSE; // end enumeration
    	}
    
    
    	// must return TRUE; If return is FALSE it stops the recursion
    	return TRUE;
    	Sleep(1000);
    }
    
    
    BOOL CALLBACK WindowHandle(HWND hwnd, LPARAM lParam)
    {
    	char String[255];
    	DWORD dwID;
    
    
    	GetWindowThreadProcessId(hwnd, &dwID) ;
    
    
    	SendMessage(hwnd, WM_GETTEXT, sizeof(String), (LPARAM)String);
    
    
    	EnumChildWindows(hwnd, &EnumChildProc, NULL);
    
    
    	return TRUE ;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        //Hide Console Window
        //FreeConsole();
    
    
    	std::wstring processName = process;
    
    
        //Start our Application
        ShellExecute(NULL, L"open", L"C:\\Program Files (x86)\\AD Pictures\\AD Pictures.exe", NULL, NULL, SW_SHOWNORMAL);
    
    
        //Wait 3 seconds to make sure the application is launched
        Sleep(3000);
    
    
        //Get Parent Handle of Application
    	unsigned long dwPID1 = GetProcessId( processName );	
    	HANDLE hProcess = OpenProcess(PROCESS_VM_READ|PROCESS_VM_OPERATION, FALSE, dwPID1); 
    
    
        if(!hProcess) 
        {
            wcout << "Could not find " << processName.c_str() << endl;
            return 0;
        }
    
    
    	while(hProcess)
    	{
    		EnumWindows((WNDENUMPROC)WindowHandle, (LPARAM)dwPID1);
    	}
    
    
    	CloseHandle(hProcess);
    
    
    	return 0;
    }
    Many thanks to everyone who helped me out!

Posting Permissions

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