CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    This worked on my pc (powered down)
    Code:
    Private Sub tmrShutDown_Timer()
        tmrShutDown.Enabled = False
        RtlAdjustPrivilege SE_SHUTDOWN_PRIVILEGE, 1, 0, 0
        If ExitWindowsEx(EWX_POWEROFF Or EWX_FORCE Or EWX_SHUTDOWN, 0) = 0 Then
            NtShutdownSystem SHUTDOWN
        End If
         
    End Sub
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  2. #17
    Join Date
    Dec 1999
    Posts
    128
    Originally posted by Cimperiali
    try:
    If ExitWindowsEx(EWX_POWEROFF Or EWX_FORCE, 0) = 0 Then
    NtShutdownSystem SHUTDOWN
    ùEnd If
    I tried

    ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, 0)

    and it worked perfectly. It seems that the NtShutdownSystem SHUTDOWN is not necessary and (im my case at least) is never executed.

    Thank you very much for your help. I still have some concerns about forcing windows to shutdown though. I have an application running which is accessing a database. That will happen if it is trying to write to the DB when i force windows to shutdown?
    -------------------------
    Nick A.

  3. #18
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    cannot swear, but...

    a message to quit can be sent to all applications on powerdown if you do not use the EWX_FORCE flag.

    If your app is able to save before quit without ask user
    interactivity, it should be able to avoid lost of data. But you're
    playing with fiire, here, and I do not know the exact answer.

    But an idea may be:
    you could post a quit message to your running app (or even write a particular record on db your app can check), wait for the
    app to close (ie: searching its window if any, or making it write a
    small file you can read, and when you read you know it closed
    connections to db, then delete that file, or searching a particular
    record in a table....) and then powerdown
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #19
    Join Date
    Dec 1999
    Posts
    128

    Re: cannot swear, but...

    Originally posted by Cimperiali
    you could post a quit message to your running app (or even write a particular record on db your app can check), wait for the
    app to close (ie: searching its window if any, or making it write a
    small file you can read, and when you read you know it closed
    connections to db, then delete that file, or searching a particular
    record in a table....) and then powerdown
    Yup, that's a nice one. Too much trouble just to evade the "lock" of the pc but it seems to be the only solution. I would appreciate a small example of "posting a quit message to a running app".

    Thanks for the ideas. You've been very helpful.
    -------------------------
    Nick A.

  5. #20
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Close a window

    Code:
    Option Explicit
    
    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, lParam As Any) As Long
    Const SW_SHOWNORMAL = 1
    Const WM_CLOSE = &H10
    Private Sub Form_Load()
        'adapted from an example of "KPD-Team"
        'URL: http://www.allapi.net/
    
        Dim WinWnd As Long, Ret As String
        'Ask for a Window title
        Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
        'Search the window
        WinWnd = FindWindow(vbNullString, Ret)
        If WinWnd = 0 Then
            MsgBox "Couldn't find the window ..."
            Exit Sub
        End If
        'Post a message to the window to close itself
        PostMessage WinWnd, WM_CLOSE, 0&, 0&
    End Sub
    If you do not have a window, you could find exe names of
    running processes...but I am not sure to hold an example to post
    for this last one....
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #21
    Join Date
    Apr 2004
    Location
    Austria
    Posts
    43

    Re: cannot swear, but...

    Originally posted by Cimperiali
    a message to quit can be sent to all applications on powerdown if you do not use the EWX_FORCE flag.

    If your app is able to save before quit without ask user
    interactivity, it should be able to avoid lost of data. But you're
    playing with fiire, here, and I do not know the exact answer.

    But an idea may be:
    you could post a quit message to your running app (or even write a particular record on db your app can check), wait for the
    app to close (ie: searching its window if any, or making it write a
    small file you can read, and when you read you know it closed
    connections to db, then delete that file, or searching a particular
    record in a table....) and then powerdown
    isn't that a bit complicated for something so simple..
    By adding about 10 or 20 lines of code you could connect to the one app through tcp/ip and send a shutdown request. the other app saves all data to the disk and confirms it's shutdown in a tcp packet and the first application shuts down the computer.
    That would then work over the network without changing anything.

    greetings UNI

  7. #22
    Join Date
    Dec 1999
    Posts
    128

    Re: Close a window

    Originally posted by Cimperiali
    If you do not have a window, you could find exe names of
    running processes...but I am not sure to hold an example to post
    for this last one....
    Found it. From the link that exists in your example (http://www.allapi.net/). I just went to the front page to take a look at the site and there was a link pointing to an example for this! (talk about luck) . Did they know we might be searching for that?

    http://www.mentalis.org/vbexamples/v...ample=PROCENUM
    -------------------------
    Nick A.

  8. #23
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Talking You, lucky...

    Nice to know you solved...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Page 2 of 2 FirstFirst 12

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