How to get Client IP and hostname using Citrix WFAPI SDK

Added by Vishal Ganeriwala , last edited by Vishal Ganeriwala on Mar 06, 2008  (view change)
Tags: 

The following example uses C# .NET and uses WFAPI SDK to list all sessions and  IP addresses on a Citrix server.

Description

WFAPI exposes WFEnumerateSession function which retrieves a list of sessions on a given Citrix Server.

Calling Convention

BOOL WINAPI WFEnumerateSessions(
    IN HANDLE hServer,
    IN DWORD Reserved,
    IN DWORD Version,
    OUT PWF_SESSION_INFOW * ppSessionInfo,
    OUT DWORD * pCount
    );

The sample code enumerates all sessions on a given Citrix server and then uses WFQuerySessionInformation which retrieves session information for the specified session on the Citrix server.
Calling Convention

BOOL WINAPI WFQuerySessionInformationW(
    IN HANDLE hServer,
    IN DWORD SessionId,
    IN WF_INFO_CLASS WFInfoClass,
    OUT LPWSTR * ppBuffer,
    OUT DWORD * pBytesReturned
    );

We can then get client IP address from WFClientAddress which is a one of the enum values of WF_INFO_CLASS.

Public Enum WF_INFO_CLASS
WFVersion
WFInitialProgram
WFWorkingDirectory
WFOEMId
WFSessionId
WFUserName
WFWinstationName
WFDomainName
WFConnectState
WFClientBuildNumber
WFClientName
WFClientDirectory
WFClientProductID
WFClientHardwareId
WFClientAddress
WFClientDisplay
WFClientCache
WFClientDrives
WFICABufferLength
RESERVED2
WFApplicationName
WFAppInfo
WFClient Info
WFUserInfo
WFClientLatency
WFSessionTime
End Enum

Download

Download ClientIP example in Csharp and WFAPI

Code Snippet

public static List<String> ListSessions(String ServerName)
        {
            IntPtr server = IntPtr.Zero;
            List<String> ret = new List<string>();
            server = OpenServer(ServerName);

            try
            {
                IntPtr ppSessionInfo = IntPtr.Zero;

                Int32 count = 0;
                Int32 retval = WFEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
                Int32 dataSize = Marshal.SizeOf(typeof(WF_SESSION_INFO));

                Int32 current = (int)ppSessionInfo;

                if (retval != 0)
                {

                    for (int i = 0; i < count; i++)
                    {

                     WF_SESSION_INFO si = (WF_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current,
                      typeof(WF_SESSION_INFO));
                        current += dataSize;

                        #region Session
                        int returned = 0; ;
                        WFSession wfSession = new WFSession();
                        //IP address
                        IntPtr addr = IntPtr.Zero;
                        if (WFQuerySessionInformation(server, si.SessionID, WF_INFO_CLASS.WFClientAddress
                             , out addr, out returned) == true)
                        {
                            WF_CLIENT_ADDRESS obj = new WF_CLIENT_ADDRESS();
                            obj = (WF_CLIENT_ADDRESS)Marshal.PtrToStructure(addr, obj.GetType());
                            wfSession.IpAddress = obj.Address[2] + "." + obj.Address[3] + "."
                                                  + obj.Address[4]    + "." + obj.Address[5];
                        }

                        #endregion

                        ret.Add(si.SessionID  + " " + si.State + " " + si.pWinStationName + "  " +
                                 wfSession.IpAddress);

                    }

                    WFFreeMemory(ppSessionInfo);
                }
            }
            finally
            {
                CloseServer(server);
            }

            return ret;
        }

User Rating?

Disclaimer

These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.

  • Add to Bookmarks