开发者

Launch CALC.EXE and retrieve value using VB6

开发者 https://www.devze.com 2023-01-11 17:30 出处:网络
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 wou

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.


Really, all you need is some calculator component you can call? Seems like you're trying to do a terrible hack for a simple functionality. It might be easier to write your own calculator in VB6.


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

(This isn't pleasant, because it will erase whatever the user might have copied to the clipboard previously, but invoking calc.exe to do arithmetic isn't pleasant either.)


I've tried a few more variations of code, but it appears you simply can't read the value of Calc.exe even via an API. I appreciate all the help, however.


VB code with function ExecuteAndReturnHWnd can be found here http://www.vbforums.com/showthread.php?t=144251 (Not very user friendly, but you could kill all calc.exe processes and execute yours)

You don't have to retrieve value when calc.exe closes, take it when you done your calculations.

0

精彩评论

暂无评论...
验证码 换一张
取 消