CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2002
    Posts
    2

    close application using PostMessage

    Hi, All
    I am trying to use the following VB .Net code (originated from VB code) to close applicaiton. The applicaiton is not closing.

    Please let me know what is the problem.

    Thanks /dan

    '*************************************************
    Module Module1
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Const WM_CLOSE = &H10

    Sub Main()
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, "Calculator")
    If winHwnd <> 0 Then
    PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
    MsgBox(winHwnd)
    Else
    MsgBox("The Calculator is not open.")
    End If
    End Sub

    End Module

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    First think I notice is that all your "As Long"s need to be changed to "As Integer"...

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Actually The First thing I notices was that DSJ is wrong.

    VB Long = C++ int = 32 bits
    VB Int = C++ short = 16 bits.

    I am assuming that the MsgBox which simply displays a number is appearing....

    Is the calculator program the same one as was used with the VB6 code? Could there be any reason why the calculator acan NOT close?

    Finally, do the first thing that should be done and check the return code from PostMessage (use GetLast Error as needed).

    It is poor practice to not check the return value of every API call. Check 999 out of 1000 and that will be the one that fails!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    I beg to differ... unless Mircosoft sent me a bad help file. Mine plainly states in the "Integer Data Type Changes" section:

    VB 6 Integer = 16 bits
    VB 6 Long = 32 bits

    .Net Short = 16 bits
    .Net Integer = 32 bits
    .Net Long = 64 bits

    The declarations given are correct for VB6 but NOT .NET!!!

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    aargh, a thousand lashes!!!!!

    I did not notice that this was the .NET forum!!!!!!


    My post applied to the differences between Vc6 and vB6.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Dec 2002
    Posts
    2
    DSJ,

    Thanks, and it's working now./dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured