Solved

How do I make a hook to copy a result from windows calculator?

Posted on 2004-09-08
5
365 Views
Last Modified: 2010-05-02
Let me give you an example of what I’m looking for. Lets say I have a button on a vb form that activates the windows calculator. I what to do the math, but When I close the “Calculator App" I want to copy the result to a text box in my vb app.  
 

 
0
Comment
Question by:rcherne1
  • 4
5 Comments
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12015526
To get the value out of calculator, you can use GetWindowText on the text box.  It is the only edit box in calculator, so you should be able to find it pretty easy.  I'll post some code in a minute.


VB6 or .net?

In VB 6, a hack would be to shell the calculator, and keep reading the text box value until the calculator goes away, the last value you get should be the value you want.

In VB.net, you could hook into the windows callbacks that announce when an application is closing, and grab your data then
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12017101
oops, I guess you can't use GetWindowText across apps, but you can use WM_GetText, here is code for the hack version. It's just a form with a button.  The button will launch calculator and get the result when you close the calculator.

Option Explicit
Private Const WM_GETTEXT = &HD
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Sub Command1_Click()
    Dim CalcWindow As Long
    Dim CalcText As String
    Dim erc As Long
    Shell "C:\windows\System32\calc.exe"
    CalcWindow = FindCalculatorWindow
   
    If CalcWindow <> 0 Then
        Do
            erc = GetCalculatorText(CalcWindow, CalcText)
            Sleep (50)
        Loop Until erc <> 0
    End If
    MsgBox CalcText
End Sub
Function FindCalculatorWindow() As Long
    Dim desktop As Long
    Dim calculator As Long
    Dim textbox As Long
    Dim buffer As String * 200
    Dim bufflen As Long
    Dim bytearray(200) As Byte
    desktop = GetDesktopWindow()
   
    calculator = GetWindow(desktop, GW_CHILD)
    Do While calculator <> 0
        bufflen = GetWindowText(calculator, buffer, 200)
        If Left(buffer, 10) = "Calculator" Then
            Exit Do
        End If
        calculator = GetWindow(calculator, GW_HWNDNEXT)
    Loop
    If calculator = 0 Then
        MsgBox "Could not find calculator!"
        Exit Function
    End If
   
    textbox = GetWindow(calculator, GW_CHILD)
    Do While textbox <> 0
        bufflen = GetClassName(textbox, buffer, 200)
        If Left(buffer, 4) = "Edit" Then
            Exit Do
        End If
        textbox = GetWindow(textbox, GW_HWNDNEXT)
    Loop
    If textbox = 0 Then
        MsgBox "Could not find text box in calculator"
        Exit Function
    End If
    FindCalculatorWindow = textbox
End Function
Function GetCalculatorText(hwnd As Long, ByRef result As String) As Long
    Dim bufflen As Long
    Dim bytearray(200) As Byte
    Dim i As Integer
    Dim Answer As String

    bufflen = SendMessage(hwnd, WM_GETTEXT, 200, bytearray(0))

    If bufflen > 0 Then
        For i = 0 To bufflen - 1
            Answer = Answer & Chr(bytearray(i))
        Next
        result = Answer
        GetCalculatorText = 0
    Else
        GetCalculatorText = -1
        'don't touch result
    End If

End Function
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12017136
In theory, you should be able to use FindWindowEx to find the textbox instead of my loops, but for some reason FindWindowEx never works for me.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12018515
To get the edit window handle of calculator using FindWindowEx, use:

'Get the handle of the Edit window of Calc
lhWndEdit = FindWindowEx(lhWnd, 0, "Edit", vbNullString)

where

lhWnd is the main window handle of calculator.
0
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 500 total points
ID: 12018892
Hey, that's why FindWindow never worked for me, I used 0 instead of vbnullstring!!

Points for Erick37 : http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21124943.html

That also simplifies the code a lot to:

Option Explicit
Private Const WM_GETTEXT = &HD
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds 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 Sub Command1_Click()
    Dim CalcWindow As Long
    Dim CalcText As String
    Dim erc As Long
    Shell "C:\windows\System32\calc.exe"
    CalcWindow = FindCalculatorWindow
   
    If CalcWindow <> 0 Then
        Do
            erc = GetCalculatorText(CalcWindow, CalcText)
            Sleep (50)
        Loop Until erc <> 0
    End If
    MsgBox CalcText
End Sub
Function FindCalculatorWindow() As Long
    Dim desktop As Long
    Dim calculator As Long
    Dim textbox As Long
   
    desktop = GetDesktopWindow()
    calculator = FindWindowEx(desktop, 0, vbNullString, "Calculator")
    textbox = FindWindowEx(calculator, 0, "Edit", vbNullString)
    FindCalculatorWindow = textbox
End Function
Function GetCalculatorText(hwnd As Long, ByRef result As String) As Long
    Dim bufflen As Long
    Dim bytearray(200) As Byte
    Dim i As Integer
    Dim Answer As String

    bufflen = SendMessage(hwnd, WM_GETTEXT, 200, bytearray(0))

    If bufflen > 0 Then
        For i = 0 To bufflen - 1
            Answer = Answer & Chr(bytearray(i))
        Next
        result = Answer
        GetCalculatorText = 0
    Else
        GetCalculatorText = -1
        'don't touch result
    End If

End Function
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now