Results 1 to 25 of 25

Thread: SendMessageByString API to dump text to Notepad not working in Windows 11

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    7

    SendMessageByString API to dump text to Notepad not working in Windows 11

    Hi

    I'm using the following code to dump text into an instance of Notepad. It works well on everything up to Windows 10, but in Windows 11 Notepad opens and hangs without the text being dumped.

    I don't have regular access to Windows 11 so wondered if someone here might see something incompatible?

    Standard module
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Private Const WM_SETTEXT = &HC
    
    Sub DumpToNotepad(s As String)
    Dim hwndNotepad  As Long
    Dim HwndNotepadText As Long
    Dim strLogContents As String
    
    If Len(s) = 0 Then Exit Sub
    
    Shell "notepad.exe", vbNormalFocus
    hwndNotepad = FindWindow(vbNullString, "Untitled - Notepad")
    If hwndNotepad > 0 Then HwndNotepadText = FindWindowEx(hwndNotepad, 0&, "edit", vbNullString)
    If HwndNotepadText > 0 Then Call SendMessageByString(HwndNotepadText, WM_SETTEXT, 0&, s)
    End Sub
    Example usage
    Code:
    Sub test()
    DumpToNotepad ("this is a test")
    End Sub

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,540

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    > I don't have regular access to Windows 11 so wondered if someone here might see something incompatible?

    In Win11 notepad opens up with text you left *unsaved* from last session, much the same way notepad++ did for the last couple of decades.

    So there is no "Untitled - Notepad" caption anymore or at least this is very rare.

    cheers,
    </wqw>

  3. #3
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,105

    Talking Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    You need to replace "FindWindow" with "EnumWindows" and find a window with "Notepad" in its title. It can still be tricky to find the correct "edit" window though since Notepad can have multiple tabs now, each with its own "edit" window.

  4. #4
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    573

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    or you can go into notepad settings and turn off the feature to open previous unsaved.

    then just search for untitled

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    7

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Thank you. Makes sense.

    I think Enuming the Windows and trying to find the one that opened would be too tricky.

    I thought maybe writing to a text file, opening it the default app, then deleting it?

    Would this work on W11, do you think?

    Standard module
    Code:
    Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    
    Sub DumpToNotepad2(s As String)
    If Len(s) = 0 Then Exit Sub
    ffile = FreeFile
    p = Environ("appdata") & "\dump.txt"
    Open p For Append As #ffile
    Print #ffile, s
    Close #ffile
    ShellExecute 0&, vbNullString, p, vbNullString, vbNullString, vbNormalFocus
    Kill p
    End Sub
    Usage
    Code:
    Sub test()
    Call DumpToNotepad2("This is a test")
    End Sub

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,871

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    its not tricky. if u need to hack another app u need to use the API available for it.

    another way is to use CreateToolhelp32Snapshot/Process32First/Process32Next
    to get the "exefile" for whatever app running.
    that will not care about the title-text at all. and from there get the window handle.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    7

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by k_zeon
    or you can go into notepad settings and turn off the feature to open previous unsaved.
    Unfortunately I can't control that setting for other users.

    Quote Originally Posted by baka
    its not tricky. if u need to hack another app u need to use the API available for it.
    It needs to work on both W11 and older versions though and my ability to test in W11 is limited.
    Just realised that my suggestion in post #5 will try to delete the file before it's created

  8. #8
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,155

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    i include a copy of old traditional notepad now with some of my tools and start that one specifically, I hate the new app version

  9. #9
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,871

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    that is why I told u about CreateToolhelp32Snapshot, that is looking for the exe.
    Im sure the name is still notepad.exe

  10. #10
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,105

    Talking Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    The Notepad from Win11 uses a "RichEdit" control instead of the classic "Edit" from previous versions. This code works on Windows 11, click on the form to send "Hello Notepad!":

    Form1
    Code:
    Option Explicit
    
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessageW Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_SETTEXT As Long = &HC
    
    Private hWndNotepad As Long, hWndEdit As Long
    
    Private Sub Form_Click()
        If hWndEdit Then SendMessageW hWndEdit, WM_SETTEXT, 0, StrPtr("Hello Notepad!")
    End Sub
    
    Private Sub Form_Load()
        EnumWindows AddressOf FindNotepad, VarPtr(hWndNotepad)
        If hWndNotepad Then EnumChildWindows hWndNotepad, AddressOf FindNotepadEdit, VarPtr(hWndEdit)
    End Sub
    Module1
    Code:
    Option Explicit
    
    Private Declare Sub PutMem4 Lib "msvbvm60" Alias "#307" (Ptr As Any, ByVal NewVal As Long)
    Private Declare Function GetClassNameW Lib "user32" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowTextW Lib "user32" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
    
    Private Const cMaxPath As Long = 260, sNotepad As String = "Notepad", sEdit = "RichEditD2DPT"
    
    Public Function FindNotepad(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sTitle As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sTitle = Left$(sBuffer, GetWindowTextW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
        If Right$(sTitle, Len(sNotepad)) = sNotepad Then
            sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
            If sClassName = sNotepad Then PutMem4 ByVal lParam, hWnd: Exit Function
        End If
        FindNotepad = True
    End Function
    
    Public Function FindNotepadEdit(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
        If sClassName = sEdit Then PutMem4 ByVal lParam, hWnd: Exit Function
        FindNotepadEdit = True
    End Function

  11. #11
    Fanatic Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    550

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by VanGoghGaming View Post
    The Notepad from Win11 uses a "RichEdit" control instead of the classic "Edit" from previous versions. This code works on Windows 11, click on the form to send "Hello Notepad!":

    Form1
    Code:
    Option Explicit
    
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessageW Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_SETTEXT As Long = &HC
    
    Private hWndNotepad As Long, hWndEdit As Long
    
    Private Sub Form_Click()
        If hWndEdit Then SendMessageW hWndEdit, WM_SETTEXT, 0, StrPtr("Hello Notepad!")
    End Sub
    
    Private Sub Form_Load()
        EnumWindows AddressOf FindNotepad, VarPtr(hWndNotepad)
        If hWndNotepad Then EnumChildWindows hWndNotepad, AddressOf FindNotepadEdit, VarPtr(hWndEdit)
    End Sub
    Module1
    Code:
    Option Explicit
    
    Private Declare Sub PutMem4 Lib "msvbvm60" Alias "#307" (Ptr As Any, ByVal NewVal As Long)
    Private Declare Function GetClassNameW Lib "user32" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowTextW Lib "user32" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
    
    Private Const cMaxPath As Long = 260, sNotepad As String = "Notepad", sEdit = "RichEditD2DPT"
    
    Public Function FindNotepad(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sTitle As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sTitle = Left$(sBuffer, GetWindowTextW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
        If Right$(sTitle, Len(sNotepad)) = sNotepad Then
            sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
            If sClassName = sNotepad Then PutMem4 ByVal lParam, hWnd: Exit Function
        End If
        FindNotepad = True
    End Function
    
    Public Function FindNotepadEdit(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
        If sClassName = sEdit Then PutMem4 ByVal lParam, hWnd: Exit Function
        FindNotepadEdit = True
    End Function
    It works fine, but for people who have it in another language it won't work and you'll have to find another way.

    In Spanish these changes.
    Code:
    Private Const cMaxPath As Long = 260, sNotepad As String = "Bloc de notas", sEdit = "RichEditD2DPT"
    
    Public Function FindNotepad(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sTitle As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sTitle = Left$(sBuffer, GetWindowTextW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
    
        If Right$(sTitle, Len(sNotepad)) = sNotepad Then
            sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
            If sClassName = "Notepad" Then PutMem4 ByVal lParam, hWnd: Exit Function
        End If
        FindNotepad = True
    End Function

  12. #12
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,871

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    that is why I dont trust enumwindows.
    first I used it to check for cheatengine and other memory-tools
    but it was not 100%. because of different versions, languages and even the title could be different.
    after I changed to CreateToolhelp32Snapshot its more stable, that can also work if the tool is minimized or just standby.

  13. #13
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,351

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by VanGoghGaming View Post
    The Notepad from Win11 uses a "RichEdit" control instead of the classic "Edit" from previous versions. This code works on Windows 11, click on the form to send "Hello Notepad!":

    Form1
    Code:
    Option Explicit
    
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessageW Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_SETTEXT As Long = &HC
    
    Private hWndNotepad As Long, hWndEdit As Long
    
    Private Sub Form_Click()
        If hWndEdit Then SendMessageW hWndEdit, WM_SETTEXT, 0, StrPtr("Hello Notepad!")
    End Sub
    
    Private Sub Form_Load()
        EnumWindows AddressOf FindNotepad, VarPtr(hWndNotepad)
        If hWndNotepad Then EnumChildWindows hWndNotepad, AddressOf FindNotepadEdit, VarPtr(hWndEdit)
    End Sub
    Module1
    Code:
    Option Explicit
    
    Private Declare Sub PutMem4 Lib "msvbvm60" Alias "#307" (Ptr As Any, ByVal NewVal As Long)
    Private Declare Function GetClassNameW Lib "user32" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowTextW Lib "user32" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
    
    Private Const cMaxPath As Long = 260, sNotepad As String = "Notepad", sEdit = "RichEditD2DPT"
    
    Public Function FindNotepad(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sTitle As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sTitle = Left$(sBuffer, GetWindowTextW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
        If Right$(sTitle, Len(sNotepad)) = sNotepad Then
            sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
            If sClassName = sNotepad Then PutMem4 ByVal lParam, hWnd: Exit Function
        End If
        FindNotepad = True
    End Function
    
    Public Function FindNotepadEdit(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
        If sClassName = sEdit Then PutMem4 ByVal lParam, hWnd: Exit Function
        FindNotepadEdit = True
    End Function

    So basically they ripped out Wordpad and turned Notepad into a shittier version of it... god Win11 just sucks so much. Glad 10 LTSC is supported through 2032.

  14. #14
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,871

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Im using 10 home edition and it feels "ok". not as good as windows 7 as theres irritation here and there, and buggy from time to time. but still works better than the first time I tried it a few years ago. I think u need to allow the OS to mature before using it. sure, it can not mature without people using it, but I don't want to be part of that. right now 10 is quite stable, even if its not as good as 7.
    so, I wouldn't use 11 right now. not even sure I will even use it. maybe in the future it will be windows 12 or whatever number. as I will stick with 10 as long possible.

  15. #15
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,105

    Talking Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by fafalone View Post
    So basically they ripped out Wordpad and turned Notepad into a shittier version of it... god Win11 just sucks so much. Glad 10 LTSC is supported through 2032.
    Not quite, Wordpad still uses the old RichEdit from msftedit.dll while Notepad uses the cutting edge RichEditD2DPT window class with a recent Microsoft 365 RichEdit that supports D2D/DirectWrite for color emoji and other goodies (RichEditD2D Window Controls).

    I'm still mainly using Windows 10 myself but I have a separate Windows 11 for testing stuff. It does contain a lot of new features not present in Windows 10. Probably you'd like to play with new Shell Interfaces and stuff like that. Generally you're missing out when choosing to stick with the past.

  16. #16
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,105

    Wink Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by yokesee View Post
    It works fine, but for people who have it in another language it won't work and you'll have to find another way.

    In Spanish these changes.
    Code:
    Private Const cMaxPath As Long = 260, sNotepad As String = "Bloc de notas", sEdit = "RichEditD2DPT"
    
    Public Function FindNotepad(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String, sTitle As String, sClassName As String
        sBuffer = String$(cMaxPath, vbNullChar)
        sTitle = Left$(sBuffer, GetWindowTextW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
    
        If Right$(sTitle, Len(sNotepad)) = sNotepad Then
            sClassName = Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
            If sClassName = "Notepad" Then PutMem4 ByVal lParam, hWnd: Exit Function
        End If
        FindNotepad = True
    End Function
    You could very well remove the check for the window title and go exclusively with the ClassName which is "Notepad" in all languages.

  17. #17

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    7

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Thanks for all the replies. I hadn't realised there was such a difference between W10/11.

    Including a copy of tradional notepad is an interesting idea, although I suspect it would get flagged by the MDM software.

    The code in Post#10 looks promsing but isn't working in W10.

    Thanks for mentioning CreateToolhelp32Snapshot in post #14. Do you think this could be used for a solution that works on both Windows versions? I'm not familar with this so don't know how to implement it.

  18. #18
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,105

    Wink Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Here's a much shorter version that works on all Windows versions and languages:

    Form1
    Code:
    Option Explicit
    
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessageW Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_SETTEXT As Long = &HC
    
    Private hWndNotepad As Long, hWndEdit As Long
    
    Private Sub Form_Click()
        If hWndEdit Then SendMessageW hWndEdit, WM_SETTEXT, 0, StrPtr("Hello Notepad!")
    End Sub
    
    Private Sub Form_Load()
        EnumWindows AddressOf FindNotepad, VarPtr(hWndNotepad)
        If hWndNotepad Then EnumChildWindows hWndNotepad, AddressOf FindNotepad, VarPtr(hWndEdit)
    End Sub
    Module1
    Code:
    Option Explicit
    
    Private Declare Sub PutMem4 Lib "msvbvm60" Alias "#307" (Ptr As Any, ByVal NewVal As Long)
    Private Declare Function GetClassNameW Lib "user32" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long
    
    Private Const cMaxPath As Long = 260, sNotepadClassName As String = "Notepad", sEditWin10 = "Edit", sEditWin11 = "RichEditD2DPT"
    
    Public Function FindNotepad(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String
        sBuffer = String$(cMaxPath, vbNullChar)
        Select Case Left$(sBuffer, GetClassNameW(hWnd, StrPtr(sBuffer), Len(sBuffer)))
            Case sNotepadClassName, sEditWin10, sEditWin11
                PutMem4 ByVal lParam, hWnd: Exit Function
        End Select
        FindNotepad = True
    End Function

  19. #19
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,122

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    If you use CreateProcess() instead of shell/shellexecute then you can obtain the process and thread handles of the create process. EnumThreadWindows() will then obtain the window handle from the thread handle via the callback function.

    https://learn.microsoft.com/en-us/wi...createprocessa
    https://learn.microsoft.com/en-us/wi...ss_information
    https://learn.microsoft.com/en-us/wi...mthreadwindows
    https://learn.microsoft.com/en-us/pr...33496(v=vs.85)

    Sorry, but I can't provide VB code as I don't use it.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  20. #20

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    7

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by VanGoghGaming
    Here's a much shorter version that works on all Windows versions and languages:
    That works to put text in an existing instance of Notepad (W10), but I really need it to create a new/blank instance and add the text there.

  21. #21
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,105

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    You already know how to shell a new instance of Notepad so just put two and two together!

  22. #22
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,351

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    Quote Originally Posted by VanGoghGaming View Post
    Not quite, Wordpad still uses the old RichEdit from msftedit.dll while Notepad uses the cutting edge RichEditD2DPT window class with a recent Microsoft 365 RichEdit that supports D2D/DirectWrite for color emoji and other goodies (RichEditD2D Window Controls).

    I'm still mainly using Windows 10 myself but I have a separate Windows 11 for testing stuff. It does contain a lot of new features not present in Windows 10. Probably you'd like to play with new Shell Interfaces and stuff like that. Generally you're missing out when choosing to stick with the past.
    What new shell features? They've abandoned standard Win32 controls, not implemented anything new for shell programming besides some high level stuff locked behind WinRT, and blocked older ways of shell programming.

    There appears to be technical and legal barriers to using Rich edit d2d in non-MS apps. And one comment claimed there's no 32bit version so tB is required. Do you have an example of using it in VB/tB? I couldn't find any examples in any language.

    But regardless, Microsoft removed (or will imminently be removing) Wordpad, and Notepad has considerably less features even if the underlying control has more; and the simple, lightning fast, no frills interface and editor is what made Notepad so great for so many things where you didn't need a full blown word processor.

  23. #23
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,871

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    here an example

    Code:
    Private Type PROCESSENTRY32
        dwSize              As Long
        cntUsage            As Long
        th32ProcessID       As Long
        th32DefaultHeapID   As Long
        th32ModuleID        As Long
        cntThreads          As Long
        th32ParentProcessID As Long
        pcPriClassBase      As Long
        dwFlags             As Long
        szExeFile           As String * 260
    End Type
    
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Private Declare Function Process32First Lib "kernel32.dll" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    
    Private Function FindNotepadHwnd() As Long
        Dim processInfo     As PROCESSENTRY32
        Dim hSnapshot       As Long
        Dim exeName         As String
        Dim ProcessID       As Long
        Dim tProcessID      As Long
        Dim thWnd           As Long
        
        hSnapshot = CreateToolhelp32Snapshot(&H2, 0)
        processInfo.dwSize = Len(processInfo)
        If Process32First(hSnapshot, processInfo) Then
            Do
                exeName = LCase(Left(processInfo.szExeFile, InStr(processInfo.szExeFile, vbNullChar) - 1))
                If InStr(exeName, "notepad.exe") Then ProcessID = processInfo.th32ProcessID: Exit Do
            Loop Until Process32Next(hSnapshot, processInfo) = 0
        End If
        CloseHandle hSnapshot
        
        If ProcessID > 0 Then
            thWnd = FindWindow(ByVal 0&, ByVal 0&)
            Do While thWnd <> 0
                If GetParent(thWnd) = 0 Then
                    GetWindowThreadProcessId thWnd, tProcessID
                    If tProcessID = ProcessID Then FindNotepadHwnd = thWnd: Exit Function
                End If
                thWnd = GetWindow(thWnd, 2&)
            Loop
        End If
    End Function
    
    Private Sub Form_Load()
        SendMessageByString FindNotepadHwnd, &HC, 0&, "Hello World"
    End Sub

  24. #24
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    573

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    can you not just save the text to file, then create your own form thats like notepad and then open it.

    or create a textfile, then shell this to open in default text app.

    Is this just to display the text for the user to read or will you be doing something with the text.?
    Last edited by k_zeon; Oct 10th, 2024 at 09:48 AM.

  25. #25
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    846

    Re: SendMessageByString API to dump text to Notepad not working in Windows 11

    VB's Shell function returns the process ID, and GetWindowThreadProcessId() gives the process ID of a giving window by its handle. Air code:

    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessID As Long) As Long

    Dim ProcessID As Long, pid As Long

    ProcessID = Shell("Notepad")

    GetWindowThreadProcessId h, pid

    If ProcessID = pid Then
    ' It's Notepad
    End If
    Last edited by qvb6; Oct 10th, 2024 at 09:50 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width