1

I have simple helper method which allows me to open notepad.exe and fill it by provided text.

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("user32.dll", EntryPoint = "SendMessageW")]
    public static extern IntPtr SendMessageW(IntPtr hWnd, UInt32 msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

    public static void OpenNotepadWithText(string text)
    {
        var process = Process.Start("notepad.exe");
        if (process == null)
            throw new Exception("Unable to run notepad.exe process!");

        process.WaitForInputIdle();
        IntPtr child = FindWindowEx(process.MainWindowHandle, new IntPtr(0), "Edit", null);
        SendMessageW(child, 0x000C, IntPtr.Zero, text);
    }

When I click on close button of notepad, notepad is closed without asking for save. It seems that used approach does not set "need_to_save" flag in notepad and thus notepad thinks that there is no changed content to save. Can someone help me with setting this flag?

user2126375
  • 1,594
  • 12
  • 29
  • 1
    I highly doubt that was a use case they designed notepad for. You could try tricking it by sending some keystrokes, such as any letter and then a backspace. – Chris Jun 30 '15 at 09:31

0 Answers0