Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting the text colour of a RichEdit from a different process
    text
    copied!<p>I'm trying to set the background and foreground colour for specific words in a richedit control (class RichEdit20W) from a different process. I have the following C code that I call using python, but it fails (SendMessage returns 0). It works if I change SCF_SELECTION to SFC_ALL or 0, but sadly it doesn't fit my needs as I only need the change to apply to part of the text in the control.</p> <p>Here's the code:</p> <pre><code>#include &lt;Windows.h&gt; #include &lt;stdio.h&gt; #include &lt;limits.h&gt; #include &lt;Richedit.h&gt; __declspec(dllexport) LRESULT SetColour(HWND hWnd, COLORREF textColor, COLORREF bgColor) { CHARRANGE cr; cr.cpMin = 3; cr.cpMax = 8; CHARFORMAT2 cf; cf.cbSize = sizeof(cf); cf.dwMask = CFM_COLOR | CFM_BACKCOLOR; cf.crTextColor = textColor; cf.crBackColor = bgColor; DWORD dwPID; HANDLE hProcess; LPVOID pRemoteCR; LPVOID pRemoteCF; SIZE_T zWritten; LRESULT lResult; GetWindowThreadProcessId(hWnd, &amp;dwPID); hProcess = OpenProcess(PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, dwPID); // Allocate memory on the target process &amp; write the CHARFORMAT2 structure there pRemoteCR = VirtualAllocEx(hProcess, NULL, sizeof cr, MEM_COMMIT, PAGE_READWRITE); WriteProcessMemory(hProcess, pRemoteCR, &amp;cr, sizeof cr, &amp;zWritten); SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM)pRemoteCR); VirtualFreeEx(hProcess, pRemoteCR, 0, MEM_RELEASE); pRemoteCF = VirtualAllocEx(hProcess, NULL, sizeof cf, MEM_COMMIT, PAGE_READWRITE); WriteProcessMemory(hProcess, pRemoteCF, &amp;cf, sizeof cf, &amp;zWritten); lResult = SendMessage(hWnd, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)pRemoteCF); // SCF_SELECTION VirtualFreeEx(hProcess, pRemoteCF, 0, MEM_RELEASE); return lResult; } BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD dwReason,LPVOID lpvReserved) { return TRUE; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload