I've using VB.NET 2005 and I want to open notepad and print stuff to it.
How can i do this??
Printable View
I've using VB.NET 2005 and I want to open notepad and print stuff to it.
How can i do this??
Well you would open notepad with Process.Start("notepad")
Then you would use the FindWindow API to get the window handle to notepad.
Then use SendMessage API to send the data to the app.
I've never used an API before, how do I do this???
Download the API Viewer (link in my signature) and set it for vb.net format. The copy the FindWindow and SendMessage definitions and paste into your general declarations section of your vb.net project.
Also, if you search the Forums you will find code examples with these too.
What is a API??
Its a library of core Windows OS functions.
Application Program Interface.
Code:Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
End Function
End Class
Okay I got these easy enough
But can not find any code to go with itCode:Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Private Const WM_SETTEXT = &HC
I wrote one up for you.
Code:Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindowEx")> _
Private Shared Function FindWindowEx(ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
End Function
Private Const WM_SETTEXT As Int32 = &HC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("notepad")
Dim iHwnd As Int32
Dim iHwndChild As Int32
Do While iHwnd = 0
iHwnd = FindWindow("Notepad", "Untitled - Notepad")
Loop
iHwndChild = FindWindowEx(iHwnd, 0, "Edit", vbNullString)
SendMessage(iHwndChild, WM_SETTEXT, 0, "WOOT!")
MessageBox.Show(iHwndChild.ToString)
End Sub
End Class
I just answerd to a similler question in this forum.Quote:
Originally Posted by kiwis
dont blame me for my english.
check wheather you can get an idea from it.
http://www.vbforums.com/showthread.php?t=496268
RobDogg,
what happens in the event that the Process never starts?
The example would hang, because of the unneeded Do While Loop.
I would also .waitforinutidle because occasionally the window isn't ready, which is why i think you wanted to do a loop in the first place.
If you insist on using a do loop, then at least verify the process handle exists first.
Then verify the handle of the child window before sending.
Yes, that is true but its only a quick example; no Try Catch error handling, no trapping if the process doesnt start etc.
So did you put in the DoWhile Loop, to wait for the process?
That's not usually how you would use the FindWindow API, because it implies that there may be multiple passes through the loop, perhaps many on a slow OS. If there were more than one pass, then there's some kind of problem, and this thread will tie up alot of other things, while it loops.
I just dont get why, since it's unneeded anyway.
Same thing right?Code:hwnd = FindWindow("Notepad", "Untitled - Notepad")
Well as the process is started you want to make sure its up and running before attempting to find the window.
Ps, "Untitled"
Its just one short example not intended for production app useage. :rolleyes: