Reading text from PowerShell with Win32?

user42 121 Reputation points
2022-03-04T18:13:40.89+00:00

Can anyone please give me a simple example (or a link to it) of how I could read text from a PowerShell window into a buffer in Win32 C++?

Thank you!

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Answer accepted by question author
  1. Castorix31 91,501 Reputation points
    2022-03-05T00:13:59.383+00:00

    A test with IUIAutomation and IUIAutomationTextPattern to get the text =>

    180240-powershell-text.gif

    #include <windows.h>  
    #include <tchar.h>  
      
    #pragma comment(linker,"\"/manifestdependency:type='win32' \  
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \  
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")  
      
    #include <UIAutomation.h>  
    #include <Richedit.h>  
      
    HINSTANCE hInst;  
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);  
    int nWidth = 600, nHeight = 400;  
    #define IDC_RICHEDIT 10  
    #define IDC_BUTTON 11  
      
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)  
    {  
    	hInst = hInstance;  
    	WNDCLASSEX wcex =  
    	{  
    		sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),  
    		LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, TEXT("WindowClass"), NULL,  
    	};  
    	if (!RegisterClassEx(&wcex))  
    		return MessageBox(NULL, TEXT("Cannot register class !"), TEXT("Error"), MB_ICONERROR | MB_OK);  
    	int nX = (GetSystemMetrics(SM_CXSCREEN) - nWidth) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - nHeight) / 2;  
    	HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"), WS_OVERLAPPEDWINDOW, nX, nY, nWidth, nHeight, NULL, NULL, hInst, NULL);  
    	if (!hWnd)  
    		return MessageBox(NULL, TEXT("Cannot create window !"), TEXT("Error"), MB_ICONERROR | MB_OK);  
    	ShowWindow(hWnd, SW_SHOWNORMAL);  
    	UpdateWindow(hWnd);  
    	MSG msg;  
    	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)  
    {  
    	static HWND hWndButton = NULL, hWndRichEdit = NULL;  
    	int wmId, wmEvent;  
    	switch (message)  
    	{  
    	case WM_CREATE:  
    	{  
    		LoadLibrary(L"msftedit.dll");  
    		hWndRichEdit = CreateWindowEx(0, MSFTEDIT_CLASS, NULL, WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_BORDER | WS_VSCROLL, 80, 10, 490, 340, hWnd, (HMENU)IDC_RICHEDIT, hInst, NULL);  
    		hWndButton = CreateWindowEx(0, L"Button", L"Click", WS_CHILD | WS_VISIBLE | BS_PUSHLIKE, 10, 10, 60, 32, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);  
    		return 0;  
    	}  
    	break;  
    	case WM_COMMAND:  
    	{  
    		wmId = LOWORD(wParam);  
    		wmEvent = HIWORD(wParam);  
    		switch (wmId)  
    		{  
    		case IDC_BUTTON:  
    		{  
    			if (wmEvent == BN_CLICKED)  
    			{  
    				IUIAutomation* pUIAutomation = NULL;  
    				HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);  
    				hr = CoCreateInstance(CLSID_CUIAutomation8, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pUIAutomation));  
    				if (SUCCEEDED(hr))  
    				{  
    					// Several Consoles can be opened, can be identified by title too  
    					HWND hWndConsole = FindWindow(L"ConsoleWindowClass", nullptr);  
    					if (hWndConsole)  
    					{  
    						IUIAutomationElement* pWindowElement = NULL;  
    						hr = pUIAutomation->ElementFromHandle(hWndConsole, &pWindowElement);  
    						if (pWindowElement != nullptr)  
    						{  
    							IUIAutomationCacheRequest* pCacheRequest = NULL;  
    							VARIANT varProp;  
    							varProp.vt = VT_I4;  
    							varProp.lVal = UIA_DocumentControlTypeId;  
    							IUIAutomationCondition* pCondition = NULL;  
    							IUIAutomationTextPattern* pTextPattern = NULL;  
    							hr = pUIAutomation->CreatePropertyCondition(UIA_ControlTypePropertyId,varProp, &pCondition);  
    							if (SUCCEEDED(hr))  
    							{  
    								hr = pUIAutomation->CreateCacheRequest(&pCacheRequest);  
    								if (SUCCEEDED(hr))  
    								{  
    									hr = pCacheRequest->AddProperty(UIA_IsTextPatternAvailablePropertyId);  
    									if (SUCCEEDED(hr))  
    									{  
    										hr = pCacheRequest->AddPattern(UIA_TextPatternId);  
    									}  
    								}  
    								IUIAutomationElement* pElement;  
    								hr = pWindowElement->FindFirstBuildCache(TreeScope_Children, pCondition, pCacheRequest, &pElement);  
    								if (SUCCEEDED(hr) && (pElement != NULL))  
    								{  
    									VARIANT varBool;  
    									hr = pElement->GetCachedPropertyValue(UIA_IsTextPatternAvailablePropertyId,	&varBool);  
    									if (SUCCEEDED(hr) && (varBool.vt == VT_BOOL) && (varBool.boolVal))  
    									{										  
    										hr = pElement->GetCachedPatternAs(UIA_TextPatternId, IID_PPV_ARGS(&pTextPattern));  
    									}  
    									pElement->Release();  
    								}  
    								pCondition->Release();  
    								if (pCacheRequest != NULL)  
    									pCacheRequest->Release();									  
    							}  
    							IUIAutomationTextRange* pTextRange;  
    							if (pTextPattern)  
    							{  
    								hr = pTextPattern->get_DocumentRange(&pTextRange);  
    								if (SUCCEEDED(hr) && (pTextRange != NULL))  
    								{  
    									BSTR bstrText = NULL;  
    									hr = pTextRange->GetText(-1, &bstrText);  
    									if (SUCCEEDED(hr) && (bstrText != NULL))  
    									{  
    										//OutputDebugString(bstrText);  
    										SetWindowText(hWndRichEdit, bstrText);  
    										SysFreeString(bstrText);  
    									}  
    									pTextRange->Release();									  
    								}  
    								pTextPattern->Release();  
    							}  
    							pWindowElement->Release();  
    						}  
    					}  
    					else  
    						MessageBox(hWnd, L"No Console found", L"Information", MB_OK | MB_ICONEXCLAMATION);  
    					pUIAutomation->Release();  
    				}  
    				CoUninitialize();  
    			}  
    		}  
    		break;  
    		default:  
    			return DefWindowProc(hWnd, message, wParam, lParam);  
    		}  
    	}  
    	break;	  
    	case WM_DESTROY:  
    	{  
    		PostQuitMessage(0);  
    		return 0;  
    	}  
    	break;  
    	default:  
    		return DefWindowProc(hWnd, message, wParam, lParam);  
    	}  
    	return 0;  
    }  
      
      
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.