如何在Citrix系统上获得用户的UNIQUE窗口?

时间:2016-08-12 21:28:01

标签: c# winforms citrix

我正在创建一个将在Citrix上运行的应用程序,该应用程序将在多个用户登录的计算机上运行。

以下是场景:用户A登录到机器1.用户B登录到机器1当用户A登录时,我们的程序启动。然后用户B打开任何程序(我们正在监控)并且应该接收提出问题的对话。但是,用户A不是用户B接收对话框,而是获得对话框。

如何向正确的用户显示/显示我们的对话框?

我尝试将对话框托管到桌面,认为每个用户都有自己独特的桌面,但这不起作用。 (见this

非常感谢任何建议/想法/示例。

由于

1 个答案:

答案 0 :(得分:0)

事实证明,在Citrix系统(和Windows操作系统)上,每个进程都与用户SID相关联。但是,在我设置WMI查询以观察特定进程的Citrix系统上,我收到了与该进程交互的任何用户的通知。

因此,我的应用程序不仅需要跟踪我感兴趣的流程,还需要跟踪与该流程交互的USER。

此代码指出谁拥有该流程:(当然缺少的是检测流程活动的代码并调用“GetProcessOwnerInformation'”,但这仍然是读者的练习:-) )

    private ProcessOwnerInformation GetProcessOwnerInformation(uint processId)
    {
        WindowsIdentity _user = WindowsIdentity.GetCurrent();
        string stringSID = string.Empty;
        string process = ExGetProcessInfoByPID((int)processId, out stringSID);

        bool bIgnoreCase = true;

        ProcessOwnerInformation retval = new ProcessOwnerInformation();
        bool bOwnsProcess = string.Compare(stringSID, _user.User.Value, bIgnoreCase) == 0;
        if(bOwnsProcess)
        {
            retval.procID = processId;
            retval.OwnsProcess = bOwnsProcess;
            retval.SID = stringSID;
        }

        return retval;
    }


    public const int TOKEN_QUERY = 0X00000008;

    const int ERROR_NO_MORE_ITEMS = 259;

    enum TOKEN_INFORMATION_CLASS
    {
        TokenUser = 1,
        TokenGroups,
        TokenPrivileges,
        TokenOwner,
        TokenPrimaryGroup,
        TokenDefaultDacl,
        TokenSource,
        TokenType,
        TokenImpersonationLevel,
        TokenStatistics,
        TokenRestrictedSids,
        TokenSessionId
    }

    [StructLayout(LayoutKind.Sequential)]
    struct TOKEN_USER
    {
        public _SID_AND_ATTRIBUTES User;
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct _SID_AND_ATTRIBUTES
    {
        public IntPtr Sid;
        public int Attributes;
    }

    [DllImport("advapi32")]
    static extern bool OpenProcessToken(
        IntPtr ProcessHandle, // handle to process
        int DesiredAccess, // desired access to process
        ref IntPtr TokenHandle // handle to open access token
    );

    [DllImport("kernel32")]
    static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32", CharSet = CharSet.Auto)]
    static extern bool GetTokenInformation(
        IntPtr hToken,
        TOKEN_INFORMATION_CLASS tokenInfoClass,
        IntPtr TokenInformation,
        int tokeInfoLength,
        ref int reqLength
    );

    [DllImport("kernel32")]
    static extern bool CloseHandle(IntPtr handle);

    [DllImport("advapi32", CharSet = CharSet.Auto)]
    static extern bool ConvertSidToStringSid(
        IntPtr pSID,
        [In, Out, MarshalAs(UnmanagedType.LPTStr)] ref string pStringSid
    );

    [DllImport("advapi32", CharSet = CharSet.Auto)]
    static extern bool ConvertStringSidToSid(
        [In, MarshalAs(UnmanagedType.LPTStr)] string pStringSid,
        ref IntPtr pSID
    );


    public static bool DumpUserInfo(IntPtr pToken, out IntPtr SID)
    {
        int Access = TOKEN_QUERY;
        IntPtr procToken = IntPtr.Zero;
        bool ret = false;
        SID = IntPtr.Zero;
        try
        {
            if (OpenProcessToken(pToken, Access, ref procToken))
            {
                ret = ProcessTokenToSid(procToken, out SID);
                CloseHandle(procToken);
            }
            return ret;
        }
        catch (Exception err)
        {
            return false;
        }
    }

    private static bool ProcessTokenToSid(IntPtr token, out IntPtr SID)
    {
        TOKEN_USER tokUser;
        const int bufLength = 256;
        IntPtr tu = Marshal.AllocHGlobal(bufLength);
        bool ret = false;
        SID = IntPtr.Zero;
        try
        {
            int cb = bufLength;
            ret = GetTokenInformation(token, TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb);
            if (ret)
            {
                tokUser = (TOKEN_USER)Marshal.PtrToStructure(tu, typeof(TOKEN_USER));
                SID = tokUser.User.Sid;
            }
            return ret;
        }
        catch (Exception err)
        {
            return false;
        }
        finally
        {
            Marshal.FreeHGlobal(tu);
        }
    }

    public static string ExGetProcessInfoByPID(int PID, out string SID)//, out string OwnerSID)
    {
        IntPtr _SID = IntPtr.Zero;
        SID = String.Empty;
        try
        {
            Process process = Process.GetProcessById(PID);
            if (DumpUserInfo(process.Handle, out _SID))
            {
                ConvertSidToStringSid(_SID, ref SID);
            }
            return process.ProcessName;
        }
        catch
        {
            return "Unknown";
        }
    }