CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    54

    [RESOLVED] SetHandleInformation() - How to use?

    I want to prevent a handle from being closed and at times from being inherited and then closed, but how do you use this API function?

    This code is self explanatory.

    Code:
    #include <iostream>
    #include <windows.h>
    
    
    int main()
    {
       // Get the default console handle.
       HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
       // Options for SetHandleInformation...
       // HANDLE_FLAG_INHERIT (0x01) - HANDLE_FLAG_PROTECT_FROM_CLOSE (0x02);
    
    
       // Display the current handle settings.
       DWORD handleInfo;
       GetHandleInformation(hStdOut, &handleInfo);
       std::cout << "\nHandle Info (default): " << handleInfo << std::endl;
    
    
       // Need to change it here so we can't close the handle.
       SetHandleInformation(hStdOut, HANDLE_FLAG_PROTECT_FROM_CLOSE, 1);
    
    
       // Prove it's been changed.
       GetHandleInformation(hStdOut, &handleInfo);
       std::cout << "\nHandle Info now: " << handleInfo << std::endl;
    
    
       // Close the handle and see if it failed.
       bool result = CloseHandle(hStdOut);
    
    
       // You will NOT see this if the handle was closed!!
       std::cout << "close result = " << result << std::endl;
    
    
       return 0;
    }
    What the mind can conceive it can achieve.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,492

    Re: SetHandleInformation() - How to use?

    According to msdn, it should be

    Code:
       // Need to change it here so we can't close the handle.
       SetHandleInformation(hStdOut, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE);
    Also, check the return of SetHandleInformation() and call GetLastError();

  3. #3
    Join Date
    Jan 2009
    Location
    England
    Posts
    54

    Re: SetHandleInformation() - How to use?

    Quote Originally Posted by Arjay View Post
    According to msdn, it should be

    Code:
       // Need to change it here so we can't close the handle.
       SetHandleInformation(hStdOut, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE);
    Also, check the return of SetHandleInformation() and call GetLastError();

    I tried that but the handle still gets closed?

    Code:
    // Need to change it here so we can't close the handle.
       if(SetHandleInformation(hStdOut, HANDLE_FLAG_PROTECT_FROM_CLOSE,
                            HANDLE_FLAG_PROTECT_FROM_CLOSE))
       {
          std::cout << "Success" << std::endl;
       }
       else
          std::cout << "Failed" << std::endl;
    
    
       // Prove it's been changed.
       GetHandleInformation(hStdOut, &handleInfo);
       std::cout << "\nHandle Info now: " << handleInfo << std::endl;
    
    
       // Close the handle and see if it failed.
       bool result = CloseHandle(hStdOut);
       if(result)
          std::cout << "Success close" << std::endl;
       else
          std::cout << "Failed close" << std::endl;
    
    
       // *** You will NOT see this if the handle was closed!! ***
       std::cout << "close result = " << result << std::endl;
    What the mind can conceive it can achieve.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: SetHandleInformation() - How to use?

    This only works for real handles, and doesn't work for the stdin and stout pseudohandles. If you want to prevent closing, you'll have to obtain the actual handle behind the pseudohandle (I'm unsure how to do that).


    On a real handle, trying to close it while it's protected results in a OS exception, trappable in the debugger (not typically in code unless you install a handler and mask this particular one)

    Either way, CloseHandle will return true with GetLastError() unchanged. SO you cannot use the return from CloseHandle as an indication whether closing actually happened or was prevented. CloseHandle() can only returns false if it did indeed try to close the handle and something went wrong while actually doing the closing.

  5. #5
    Join Date
    Jan 2009
    Location
    England
    Posts
    54

    Re: SetHandleInformation() - How to use?

    Many thanks for the replies. I will look into this further before I close.
    What the mind can conceive it can achieve.

  6. #6
    Join Date
    Jul 2015
    Posts
    17

    Re: SetHandleInformation() - How to use?

    Quote Originally Posted by Gerald Bates View Post
    I want to prevent a handle from being closed and at times from being inherited and then closed, but how do you use this API function?

    This code is self explanatory.

    Code:
    #include <iostream>
    #include <windows.h>
    
    
    int main()
    {
       // Get the default console handle.
       HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
       // Options for SetHandleInformation...
       // HANDLE_FLAG_INHERIT (0x01) - HANDLE_FLAG_PROTECT_FROM_CLOSE (0x02);
    
    
       // Display the current handle settings.
       DWORD handleInfo;
       GetHandleInformation(hStdOut, &handleInfo);
       std::cout << "\nHandle Info (default): " << handleInfo << std::endl;
    
    
       // Need to change it here so we can't close the handle.
       SetHandleInformation(hStdOut, HANDLE_FLAG_PROTECT_FROM_CLOSE, 1);
    
    
       // Prove it's been changed.
       GetHandleInformation(hStdOut, &handleInfo);
       std::cout << "\nHandle Info now: " << handleInfo << std::endl;
    
    
       // Close the handle and see if it failed.
       bool result = CloseHandle(hStdOut);
    
    
       // You will NOT see this if the handle was closed!!
       std::cout << "close result = " << result << std::endl;
    
    
       return 0;
    }
    OK your trying to do the opposite in your code. It seems to me that you set the MASK to delete the value that protects the handle from being closed then you pass 1 to the flags, you should pass it 0 or a constant only.

    the msdn definition for he functions says
    the parameters are

    setHandleInformation(handle, mask(the thing that removes properties), flags(the thing that sets them);

    Heres what should be done
    setHandleInformation(hStdOut, 0, HANDLE_PROTECT_FLAG_FROM_CLOSE);

    now you can't close the handle.

    Or "|" together the bits if you want to add a value into there like this FLAG_1 | FLAG_2
    Last edited by C0deDotLoad; July 14th, 2015 at 09:47 PM.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: SetHandleInformation() - How to use?

    Quote Originally Posted by C0deDotLoad View Post
    It seems to me that you set the MASK to delete the value that protects the handle from being closed then you pass 1 to the flags, you should pass it 0 or a constant only.

    the msdn definition for he functions says
    the parameters are

    setHandleInformation(handle, mask(the thing that removes properties), flags(the thing that sets them);

    Heres what should be done
    setHandleInformation(hStdOut, 0, HANDLE_PROTECT_FLAG_FROM_CLOSE);
    It is wrong.
    MSDN writes it very clear:
    dwMask [in]
    A mask that specifies the bit flags to be changed. Use the same constants shown in the description of dwFlags.
    Besides, there is no such a flag as HANDLE_PROTECT_FLAG_FROM_CLOSE.
    There is the HANDLE_FLAG_PROTECT_FROM_CLOSE instead!
    Victor Nijegorodov

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,561

    Re: SetHandleInformation() - How to use?

    From MSDN https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    "To set or clear the associated bit flag in dwFlags, you must set a change mask bit flag in dwMask."

    So the suggestion from Arjay in post #2 is the right way of coding it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.1)

  9. #9
    Join Date
    Jul 2015
    Posts
    17

    Re: SetHandleInformation() - How to use?

    Quote Originally Posted by VictorN View Post
    It is wrong.
    MSDN writes it very clear:
    Besides, there is no such a flag as HANDLE_PROTECT_FLAG_FROM_CLOSE.
    There is the HANDLE_FLAG_PROTECT_FROM_CLOSE instead!
    Quote Originally Posted by C0deDotLoad View Post
    OK your trying to do the opposite in your code. It seems to me that you set the MASK to delete the value that protects the handle from being closed then you pass 1 to the flags, you should pass it 0 or a constant only.

    the msdn definition for the functions says
    the parameters are

    setHandleInformation(handle, mask(the thing that removes properties), flags(the thing that sets them);

    Heres what should be done
    setHandleInformation(hStdOut, 0, HANDLE_PROTECT_FLAG_FROM_CLOSE);

    now you can't close the handle.

    Or "|" together the bits if you want to add a value into there like this FLAG_1 | FLAG_2
    Ok the way they wrote it "The bits to be changed" sounded liking they were xoring the bits you specified. Maybe I am wrong, but seriously look how they wrote it. In any condition the program is not going to work but neither is this: setting the end parameter to one and when I typed that constant flag wrong I was just being lazy and using a "you get the idea approach" I dont want to be specific on this one when and I am sure he gets the idea as he clearly knows from his comments there are only two FLAGS for the parameter so its certain he would get the idea. Secondly I do not think its possibly to protect the STD handles from being closed because you do not have the rights to change them in your program you never manually created them they are allocated by the system. Calling the function would be of no effect. However if you call the same thing on any handle besides it should work
    Thirdly I was just telling him that I am pretty sure it won't work if you set the third argument to 1 (It should be one of the two constants in ANY condition) flag when your trying to set the value to protect from close or HANDLE_FLAG_PROTECT_FROM_CLOSE however you want me to say it.
    Last edited by C0deDotLoad; July 15th, 2015 at 09:21 PM. Reason: PC freezing typos

  10. #10
    Join Date
    Jan 2009
    Location
    England
    Posts
    54

    Re: SetHandleInformation() - How to use?

    I've decided to take a different route with my project, but this has helped a great deal. Many thanks to everyone that replied, appreciated.
    What the mind can conceive it can achieve.

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