Hello all,
EDIT
Just to clarify, there is no intent of putting this into production. Purely from a coding/automatiion standpoint, and ignoring the fact that there are modules out there to do calcuations, how would one go about with the following request? I'm interested in how VB6 can use APIs to interact with other programs.
END EDIT
Using VB6, I'm wondering if it's possible to launch CALC.EXE, do some calculations, and then send the value back to a textbox in the form.
Below is the code I'm testing so far:
APIs:
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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Button clicks:
Private Sub cmdStartCalc_Click()
Shell "calc.exe", vbNormalFocus
SendKeys "123"
End Sub
Private Sub cmdRetrieveValue_Click()
Dim hwnd As Long
' Gets set to 266558 when calc.exe is running, so I believe this is working
hwnd = FindWindow("SciCalc", "Calculator")
Dim text As String
text = Space(260)
Dim res As Long
' Res always returns 10 for some reason
res = GetWindowText(hwnd, text, 260)
txtValue.text = CStr(res)
End Sub
A couple things come to mind -- first of all, if an instance of Calc.exe was already running, I'm not sure which one will be targeted by FindWindow.
Second of all, it would be neat to return the value in Calc when my instance of Calc.exe is closed, but I'm open to using the button to retrieve the value.
There's probably a better way to do it in .NET, but I'm locked into VB6 for the time being.
Any insight would be greatly appreciated.