EDIT
Just to clarify, there is no intention of putting this into production. Purely from the point of view of coding / automation and ignoring the fact that there are modules for carrying out calculations, how to do this with the following query? I'm interested in how VB6 can use the API to interact with other programs.
End edit
Using VB6, I am wondering if it is possible to run CALC.EXE, perform some calculations and then send the value back to the text field in the form.
Below is the code that I'm testing so far:
API:
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 of things come to mind - first of all, if an instance of Calc.exe is already running, I'm not sure which one will be targeted by FindWindow.
Secondly, it would be neat to return a value to Calc when my Calc.exe instance is closed, but I'm open to using a button to get the value.
This is probably the best way to do this in .NET, but I'm locked out in VB6.
Any understanding would be greatly appreciated.