Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pop up the Calculator in XP 1

Status
Not open for further replies.

Golom

Programmer
Sep 1, 2003
5,595
CA
I'm using the following chunk of code to pop up the calculator in Win 98
Code:
Public Sub ShowCalculator()
    
    Dim StringLen           As Long
    Dim strWinPath          As String
    Dim strSysFileName      As String
    Static ProgID           As Long
    Dim WindowHandle        As Long
    Dim FSO                 As FileSystemObject

    On Error Resume Next

    AppActivate ProgID, False
    
    If Err.Number <> 0 Then
        Set FSO = New FileSystemObject
        ' Get the path of the Windows\System directory.
        strWinPath = Space$(MAX_FILENAME_LEN)
        StringLen = GetWindowsDirectory(strWinPath, MAX_FILENAME_LEN)
        strWinPath = Left$(strWinPath, StringLen)
        strSysFileName = strWinPath & "\Calc.exe"
        
        If Not FSO.FileExists(strSysFileName) Then
            StringLen = GetSystemDirectory(strWinPath, MAX_FILENAME_LEN)
            strWinPath = Left$(strWinPath, StringLen)
            strSysFileName = strWinPath & "\Calc.exe"
        End If
        
        ProgID = Shell(strSysFileName, vbNormalFocus)
        WindowHandle = GetForegroundWindow()
        Call SetWindowPos(WindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
        Set FSO = Nothing
    End If
End Sub

But it doesn't work in XP because "Calc.Exe" is in Windows\System32 on XP and the GetWindowsDirectory and GetSystemDirectory API calls both return "C:\Windows". Is there an API call (or some other method) that will find the right directory in XP?
 
Try this instead:

Shell "calc.exe"

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
This is what I do and it works in win2000 and winXP. Add a reference to the 'Windows Script Host Object Model' via the Project-Referenced menu. Then add the following variable to your project in a module or form.
Code:
'// Object used when viewing the help files.
Public execObject As WshExec
Then in the command button or menu use the following code.
Code:
With New WshShell
   Set execObject = .Exec("calc.exe")
End With


zemp
 
zemp

Thanks. That works with a bit of fiddling to force it to be the TOPMOST form. It did seem that, after doing this I had to insert a "DoEvents" to allow windows to add the calculator to the Windows collection so that FindWindow could locate it.

Anyway ... have a star.
 
Thanks Golom. FYI, the reason I like this method, as opposed to Dr J's, is that you can automatically close the other application (calculator) when you close your app with the following code.
Code:
   If Not execObject Is Nothing Then execObject.Terminate
With the Shell command the calculator, or any other called app (such as html help files) will remain open.


zemp
 
Here is another way. 2 short lines

Code: In form only
General Declarations
Public calc As Integer
____________________________________________
cmdCalculator_click()
calc = Shell("calc.exe")
End Sub

this works on Win2k, so it should work on all NT systems including XP.



________________________________________________
Don't get mad!! Get even!!

 
A Public variable for no good reason? Looks nicer???

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top