public static LsaAgentStatus GetLSAMgmtStatus(string hostname)
        {
            LsaAgentStatus agentStatus = null;
            IntPtr pLsaStatus = IntPtr.Zero;

            try
            {
                LSAMgmtAPI.LSA_STATUS LsaStatus = new LSAMgmtAPI.LSA_STATUS();

                int ret = (int)LSAMgmtAPI.LWMGMTQueryLsaStatus(hostname, out pLsaStatus);
                if (ret != 0)
                {
                    //return null;
                    throw new ApplicationException("Failed to query LSA Agent status");
                }

                Marshal.PtrToStructure(pLsaStatus, LsaStatus);

                agentStatus = new LsaAgentStatus();

                agentStatus.MajorVersion = LsaStatus.version.majorVersion;
                agentStatus.MinorVersion = LsaStatus.version.minorVersion;
                agentStatus.BuildVersion = LsaStatus.version.buildVersion;

                agentStatus.Uptime = LsaStatus.dwUptime;

                if (LsaStatus.dwCount > 0)
                {
                    IntPtr pCur = LsaStatus.pAuthProviderStatusArray;

                    for (int index = 0; index < LsaStatus.dwCount; index++)
                    {
                        LsaAuthProviderStatus authProviderStatus = new LsaAuthProviderStatus();
                        LSAMgmtAPI.LSA_AUTH_PROVIDER_STATUS pProviderStatus = new LSAMgmtAPI.LSA_AUTH_PROVIDER_STATUS();

                        Marshal.PtrToStructure(pCur, pProviderStatus);

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszId))
                        {
                            authProviderStatus.Id = Marshal.PtrToStringAuto(pProviderStatus.pszId);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszDomain))
                        {
                            authProviderStatus.Domain = Marshal.PtrToStringAuto(pProviderStatus.pszDomain);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszForest))
                        {
                            authProviderStatus.Forest = Marshal.PtrToStringAuto(pProviderStatus.pszForest);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszSite))
                        {
                            authProviderStatus.Site = Marshal.PtrToStringAuto(pProviderStatus.pszSite);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszCell))
                        {
                            authProviderStatus.Cell = Marshal.PtrToStringAuto(pProviderStatus.pszCell);
                        }

                        authProviderStatus.Mode = (LsaAuthProviderMode)Enum.Parse(typeof(LsaAuthProviderMode), pProviderStatus.mode.ToString());
                        authProviderStatus.Submode = (LsaAuthProviderSubMode)Enum.Parse(typeof(LsaAuthProviderSubMode), pProviderStatus.subMode.ToString());
                        authProviderStatus.State = (LsaAuthProviderState)Enum.Parse(typeof(LsaAuthProviderState), pProviderStatus.status.ToString());

                        GetLSAMgmtTrustedDomainInfo(pProviderStatus, ref authProviderStatus);

                        agentStatus.AuthProviderList.Add(authProviderStatus);

                        pCur = (IntPtr)((int)pCur + Marshal.SizeOf(pProviderStatus));
                    }
                }
            }
            catch (Exception e)
            {
                //Logger.ShowUserError(e.Message.ToString());
                Logger.LogException("LSAMgmtStatus.GetLSAMgmtStatus", e);
                agentStatus = null;
            }
            finally
            {
                if (!IntPtr.Zero.Equals(pLsaStatus))
                {
                    LSAMgmtAPI.LWMGMTFreeLsaStatus(pLsaStatus);
                }
            }

            return agentStatus;
        }
        public static LsaAgentStatus GetLSAMgmtStatus(string hostname)
        {
            LsaAgentStatus agentStatus = null;
            IntPtr         pLsaStatus  = IntPtr.Zero;

            try
            {
                LSAMgmtAPI.LSA_STATUS LsaStatus = new LSAMgmtAPI.LSA_STATUS();

                int ret = (int)LSAMgmtAPI.LWMGMTQueryLsaStatus(hostname, out pLsaStatus);
                if (ret != 0)
                {
                    //return null;
                    throw new ApplicationException("Failed to query LSA Agent status");
                }

                Marshal.PtrToStructure(pLsaStatus, LsaStatus);

                agentStatus = new LsaAgentStatus();

                agentStatus.MajorVersion = LsaStatus.version.majorVersion;
                agentStatus.MinorVersion = LsaStatus.version.minorVersion;
                agentStatus.BuildVersion = LsaStatus.version.buildVersion;

                agentStatus.Uptime = LsaStatus.dwUptime;

                if (LsaStatus.dwCount > 0)
                {
                    IntPtr pCur = LsaStatus.pAuthProviderStatusArray;

                    for (int index = 0; index < LsaStatus.dwCount; index++)
                    {
                        LsaAuthProviderStatus authProviderStatus            = new LsaAuthProviderStatus();
                        LSAMgmtAPI.LSA_AUTH_PROVIDER_STATUS pProviderStatus = new LSAMgmtAPI.LSA_AUTH_PROVIDER_STATUS();

                        Marshal.PtrToStructure(pCur, pProviderStatus);

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszId))
                        {
                            authProviderStatus.Id = Marshal.PtrToStringAuto(pProviderStatus.pszId);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszDomain))
                        {
                            authProviderStatus.Domain = Marshal.PtrToStringAuto(pProviderStatus.pszDomain);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszForest))
                        {
                            authProviderStatus.Forest = Marshal.PtrToStringAuto(pProviderStatus.pszForest);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszSite))
                        {
                            authProviderStatus.Site = Marshal.PtrToStringAuto(pProviderStatus.pszSite);
                        }

                        if (!IntPtr.Zero.Equals(pProviderStatus.pszCell))
                        {
                            authProviderStatus.Cell = Marshal.PtrToStringAuto(pProviderStatus.pszCell);
                        }

                        authProviderStatus.Mode    = (LsaAuthProviderMode)Enum.Parse(typeof(LsaAuthProviderMode), pProviderStatus.mode.ToString());
                        authProviderStatus.Submode = (LsaAuthProviderSubMode)Enum.Parse(typeof(LsaAuthProviderSubMode), pProviderStatus.subMode.ToString());
                        authProviderStatus.State   = (LsaAuthProviderState)Enum.Parse(typeof(LsaAuthProviderState), pProviderStatus.status.ToString());

                        GetLSAMgmtTrustedDomainInfo(pProviderStatus, ref authProviderStatus);

                        agentStatus.AuthProviderList.Add(authProviderStatus);

                        pCur = (IntPtr)((int)pCur + Marshal.SizeOf(pProviderStatus));
                    }
                }
            }
            catch (Exception e)
            {
                //Logger.ShowUserError(e.Message.ToString());
                Logger.LogException("LSAMgmtStatus.GetLSAMgmtStatus", e);
                agentStatus = null;
            }
            finally
            {
                if (!IntPtr.Zero.Equals(pLsaStatus))
                {
                    LSAMgmtAPI.LWMGMTFreeLsaStatus(pLsaStatus);
                }
            }

            return(agentStatus);
        }