Esempio n. 1
0
        /// <summary>
        /// Returns the login token for the specified network name resource (its compute account)
        /// </summary>
        /// <param name="hCluster"></param>
        /// <param name="hResource"></param>
        /// <returns></returns>
        private static IntPtr GetNetworkNameLoginToken(IntPtr hCluster, IntPtr hResource)
        {
            int           length       = Win32API.MAX_COMPUTERNAME_LENGTH + 1;
            StringBuilder computerName = new StringBuilder(length);

            // Get the NETBIOS name of the local physical node
            if (!Win32API.GetComputerNameEx((int)COMPUTER_NAME_FORMAT.ComputerNamePhysicalNetBIOS, computerName, ref length))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Cannot get node's physical computer name");
            }

            // Get a handle to the local node
            IntPtr hClusterNode = Win32API.OpenClusterNode(hCluster, computerName.ToString());

            if (hClusterNode == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Cannot access local failover node");
            }

            try
            {
                // Get login token

                CLUS_NETNAME_VS_TOKEN_INFO tokenInfo = new CLUS_NETNAME_VS_TOKEN_INFO();
                tokenInfo.ProcessID = (uint)Process.GetCurrentProcess().Id;

                int    bytesReturned = 0;
                IntPtr loginToken    = IntPtr.Zero;
                int    ret           = Win32API.ClusterResourceControl_NetNameToken(hResource, hClusterNode,
                                                                                    (int)CLUSCTL_RESOURCE_CODES.CLUSCTL_RESOURCE_NETNAME_GET_VIRTUAL_SERVER_TOKEN,
                                                                                    ref tokenInfo, CLUS_NETNAME_VS_TOKEN_INFO.GetSize(), out loginToken, IntPtr.Size, out bytesReturned);
                if (ret != 0 || bytesReturned != IntPtr.Size)
                {
                    throw new Win32Exception(ret, "Cannot retrieve network name login token");
                }

                return(loginToken);
            }

            finally
            {
                if (hClusterNode != IntPtr.Zero)
                {
                    Win32API.CloseClusterNode(hClusterNode);
                }
            }
        }