CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    I'm trying to implement rebooting of a remote computer with InitiateShutdown API using the following code, but it fails with RPC_S_SERVER_UNAVAILABLE or 1722 error code:

    Code:
    //Process is running as administrator
    LPCTSTR pServerName = L"\\\\192.168.42.105";
    
    if(AdjustPrivilege(NULL, L"SeShutdownPrivilege", TRUE) &&
        AdjustPrivilege(pServerName, L"SeRemoteShutdownPrivilege", TRUE))
    {
        int nErrorCode = ::InitiateShutdown(pServerName, NULL, 30,
                                            SHUTDOWN_INSTALL_UPDATES | SHUTDOWN_RESTART, 0);
    
        //Receive nErrorCode == 1722, or RPC_S_SERVER_UNAVAILABLE
    }
    
    
    
    BOOL AdjustPrivilege(LPCTSTR pStrMachine, LPCTSTR pPrivilegeName, BOOL bEnable)
    {
        HANDLE hToken; 
        TOKEN_PRIVILEGES tkp;
        BOOL bRes = FALSE;
    
        if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
            return FALSE; 
    
        if(LookupPrivilegeValue(pStrMachine, pPrivilegeName, &tkp.Privileges[0].Luid))
        {
            tkp.PrivilegeCount = 1;  
            tkp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_REMOVED; 
    
            bRes = AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
            int nOSError = GetLastError();
            if(bRes)
            {
                if(nOSError != ERROR_SUCCESS)
                    bRes = FALSE;
            }
        }
    
        CloseHandle(hToken);
    
        return bRes;
    }
    192.168.42.105 computer has file and printer sharing enabled on the network.

    Note that the following shutdown command seems to work just fine to reboot the remote computer:

    Code:
    shutdown /r /m \\192.168.42.105 /t 30
    What am I missing with my code?

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

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    My only question is whether you are coding in C or C++.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by Arjay View Post
    My only question is whether you are coding in C or C++.
    It is a C++ VS2008 project. But the code itself can be reduced down to plain C. Why would it matter though?

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

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by dc_2000 View Post
    It is a C++ VS2008 project. But the code itself can be reduced down to plain C. Why would it matter though?
    It was in reference to another forum post. I should have used a .

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by Arjay View Post
    It was in reference to another forum post. I should have used a .
    If we run into an issue one of my colleagues may ask the same question in a different location (of his choosing.)

    Still back to the point. Any idea about my actual question?

    Can anyone at least repro it?

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

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by dc_2000 View Post
    If we run into an issue one of my colleagues may ask the same question in a different location (of his choosing.)

    Still back to the point. Any idea about my actual question?

    Can anyone at least repro it?
    Have you looked into the answers on the other forum - it seems to give some idea there? Search bing or google for "InitiateShutdown RPC_S_SERVER_UNAVAILABLE".

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    First thing I would try was dropping install updates flag. Second would be specifying some specific reason code instead of the zero.
    Best regards,
    Igor

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by Arjay View Post
    Have you looked into the answers on the other forum - it seems to give some idea there? Search bing or google for "InitiateShutdown RPC_S_SERVER_UNAVAILABLE".
    Arjay, are you talking about this one? (That's the first one that comes up for me.) I tried all this. Microsoft's shutdown works fine if I do the following from that same machine:

    shutdown /r /m \\192.168.42.105 /t 30

    Quote Originally Posted by Igor Vartanov View Post
    First thing I would try was dropping install updates flag. Second would be specifying some specific reason code instead of the zero.
    Yes, tried it. Dropping SHUTDOWN_INSTALL_UPDATES doesn't change anything, as well as specifying a reason code. I also tried using just "192.168.42.105" without two slashes. Still the same error code RPC_S_SERVER_UNAVAILABLE. Worse still, if I simply use "somebadservername" instead, it also returns RPC_S_SERVER_UNAVAILABLE.

    Further exploration.

    If I use InitiateSystemShutdown(ex) instead, everything works as expected. The problem with that API is that I can't use SHUTDOWN_INSTALL_UPDATES flag, which is the sole reason why I went with InitiateShutdown.

    My colleague put those APIs through a low-level debugger and the problem seems to lie in the way InitiateShutdown makes an RPC call to the server. Are you guys familiar with RPC binding and mostly RPC authentication? Because that's where the root of this error seems to be in.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Well, natural thing to ask about is if the remote server version is high enough to support the InitiateShutdown API. Did you try that with different server platforms? My guess is W2008 Server is minimal platform to be kicked with InitiateShutdown.
    Best regards,
    Igor

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

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by Igor Vartanov View Post
    Well, natural thing to ask about is if the remote server version is high enough to support the InitiateShutdown API. Did you try that with different server platforms? My guess is W2008 Server is minimal platform to be kicked with InitiateShutdown.
    From MSDN, the minimum supported server is 2008 and the client is Vista (but from previous experience this is not always correct!).
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by dc_2000 View Post
    Are you guys familiar with RPC binding and mostly RPC authentication? Because that's where the root of this error seems to be in.
    How RPC Works
    I believe most of the times what you need to know about RPC authentication is that it exists and works properly. Distributed COM builds on top of RPC, and I never had an issue of unreasonable DCOM call failure.

    As for RPC binding, it's the same play with stubs like in COM. Client calls are actually processed by server stubs, and the corresponding APIs get called on behalf of the remote caller. Of course, to be called the remote procedure must exist. That's why I asked about remote Windows server version.
    Best regards,
    Igor

  12. #12
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Thank you, guys. To answer your questions.

    I tried it using the following configuration:

    Server Client
    ------- ---------
    Windows Server 2008 R2 --- Windows 7 Professional
    Windows 7 Professional --- Windows 7 Professional

    They may not have the latest updates installed (both ran in a VM) but would that matter for that API?

    Also question to you -- if you substitute my server name with a loopback address and run my code on your system, can you repro this error?

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

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    I've tried this and can confirm that from a Windows 7 computer trying to shut down a Windows 10 computer I get the error code 1722.
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by 2kaud View Post
    I've tried this and can confirm that from a Windows 7 computer trying to shut down a Windows 10 computer I get the error code 1722.
    Thanks.

    So what's the next step in trying to make this work? Any suggestions?

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

    Re: InitiateShutdown fails for a remote computer with RPC_S_SERVER_UNAVAILABLE error

    Quote Originally Posted by dc_2000 View Post
    Thanks.

    So what's the next step in trying to make this work? Any suggestions?
    Well, if you are sure that this cmd code
    Code:
    shutdown /r /m \\192.168.42.105 /t 30
    worked then the possible workaround would be just spawn the cmd process with this command line!
    Victor Nijegorodov

Page 1 of 2 12 LastLast

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