-
Oct 9th, 2024, 06:51 AM
#1
Thread Starter
New Member
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
-
Oct 9th, 2024, 07:19 AM
#2
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>
-
Oct 9th, 2024, 07:42 AM
#3
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.
-
Oct 9th, 2024, 10:09 AM
#4
Fanatic Member
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
-
Oct 9th, 2024, 10:15 AM
#5
Thread Starter
New Member
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
-
Oct 9th, 2024, 10:37 AM
#6
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.
-
Oct 9th, 2024, 11:13 AM
#7
Thread Starter
New Member
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
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.
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
-
Oct 9th, 2024, 11:31 AM
#8
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
-
Oct 9th, 2024, 12:22 PM
#9
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
-
Oct 9th, 2024, 01:26 PM
#10
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
-
Oct 9th, 2024, 02:41 PM
#11
Fanatic Member
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
Originally Posted by VanGoghGaming
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
-
Oct 9th, 2024, 03:06 PM
#12
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.
-
Oct 9th, 2024, 05:04 PM
#13
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
Originally Posted by VanGoghGaming
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.
-
Oct 9th, 2024, 06:19 PM
#14
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.
-
Oct 9th, 2024, 11:41 PM
#15
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
Originally Posted by fafalone
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.
-
Oct 10th, 2024, 02:52 AM
#16
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
Originally Posted by yokesee
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.
-
Oct 10th, 2024, 03:27 AM
#17
Thread Starter
New Member
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.
-
Oct 10th, 2024, 03:54 AM
#18
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
-
Oct 10th, 2024, 04:16 AM
#19
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)
-
Oct 10th, 2024, 04:53 AM
#20
Thread Starter
New Member
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
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.
-
Oct 10th, 2024, 05:59 AM
#21
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!
-
Oct 10th, 2024, 06:13 AM
#22
Re: SendMessageByString API to dump text to Notepad not working in Windows 11
Originally Posted by VanGoghGaming
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.
-
Oct 10th, 2024, 06:53 AM
#23
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
-
Oct 10th, 2024, 08:23 AM
#24
Fanatic Member
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.
-
Oct 10th, 2024, 09:46 AM
#25
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|