Search code examples
c#winformsbackgroundworkerbackground-processsetfocus

Send text from NotePad to unfocused Form1


This time I have a reciveText.txt open and with the focal point in this txt file and I want to write some text on it; and simultaneously write the same text in a textBox of a Form that will be in secon plane, I mean, without focal point. How can I send text from the notepad to the textBox in second plane?

It is possible?

PS: In the real situation a BarCode Reader will write strings in a specific NotePad File, but another application that will be running in second plane (like a backgroundprocess or without a focal point) will read those strings and when detect some kind of issue the application will notify us with a visual alert... I just need to know how to send what the bar code reader just read to a unfocused Form...

Please Help!


Solution

  • I would comment, but I can't.

    Anyway, I would do some memory hacking: http://www.codeproject.com/Articles/670373/Csharp-Read-Write-another-Process-Memory

    Access the memory (RAM) of notepad using that. You can see the plaintext.

    Memory hacking is usually overlooked and isn't given much credit. Everyone keylogs or something, but that is unneeded. Just do this:

    Here is from codeplex:

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Text;
    
    public class MemoryRead
    {
    const int PROCESS_WM_READ = 0x0010;
    
    [DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    
    [DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(int hProcess, 
      int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
    
    public static void Main()
    {
        Process process = Process.GetProcessesByName("notepad")[0]; 
        IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id); 
    
        int bytesRead = 0;
        byte[] buffer = new byte[24]; //'Hello World!' takes 12*2 bytes because of Unicode 
    
    
        // 0x0046A3B8 is the address where I found the string, replace it with what you found
        ReadProcessMemory((int)processHandle, 0x0046A3B8, buffer, buffer.Length, ref bytesRead);
    
        Console.WriteLine(Encoding.Unicode.GetString(buffer) + 
           " (" + bytesRead.ToString() + "bytes)");
        Console.ReadLine();
    }
    }
    

    UPDATE Copy and pasted wrong code. Above is the new one