Solved

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

Posted on 2004-09-08
5
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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