Run CALC.EXE and extract the value using VB6

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.

+4
source share
4 answers

I tried a few more code options, but it seems you just can't read the Calc.exe value even through the API. However, I appreciate all the help.

+1
source

Indeed, all you need is some kind of calculator component that you can name? It looks like you are trying to make a terrible hack for simple functionality. It may be easier to write your own calculator in VB6.

+4
source

Pressing Ctrl + C causes the calculator to copy the displayed value to the clipboard.

(This is unpleasant because it will delete everything that the user could copy to the clipboard earlier, but the call to calc.exe for arithmetic is also not pleasant.)

+2
source

VB code with the function ExecuteAndReturnHWnd can be found here http://www.vbforums.com/showthread.php?t=144251 (Not very convenient, but you can kill all calc. Exe processes and executes yours)

You do not need to retrieve the value when calc.exe closes, take it when you have done your calculations.

0
source

All Articles