3

Im currently using WFAPI to determine the client ip address of a citrix session from C#

[StructLayout(LayoutKind.Sequential)]
internal struct WF_CLIENT_ADDRESS {
    public int AddressFamily;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public byte[] Address;
}

[DllImport("WFAPI.dll", EntryPoint = "WFFreeMemory")]
private static extern void WFFreeMemory(IntPtr pMemory);

[DllImport("WFAPI.dll", EntryPoint = "WFQuerySessionInformationA")]
private static extern bool WFQuerySessionInformation(IntPtr hServer, 
    int iSessionId, int infotype, out IntPtr ppBuffer, out int pBytesReturned);

const int ClientAddress = 14;
const int CurrentSession = -1;
static readonly IntPtr CurrentServer = IntPtr.Zero;

public static string GetClientAddress() {
    IntPtr addr;
    int returned;
    bool ok = WFQuerySessionInformation(CurrentServer, CurrentSession, 
        ClientAddress, out addr, out returned);
    if (!ok) return null;
    WF_CLIENT_ADDRESS obj = new WF_CLIENT_ADDRESS();
    obj = (WF_CLIENT_ADDRESS)Marshal.PtrToStructure(addr, obj.GetType());
    string clientAdress = 
        obj.Address[2] + "." + obj.Address[3] + "." + 
        obj.Address[4] + "." + obj.Address[5];
    WFFreeMemory(addr);
    return clientAdress;
}

WFAPI.DLL/WFAPI64.DLL seems to be available at the citrix environments that I have access to. Does anyone have a better way to do this?
And does anyone know how to determine if the process is in fact running in a citrix environment or not?

1 Answers1

5

No what you're doing is fine. WFAPI is one of the best ways to get this kind of information.

Knowing whether the session is a Citrix session is just an extension of what you're doing. If you look at the WFAPI reference for WFQuerySessionInformation:

http://community.citrix.com/download/attachments/37388956/WFAPI_SDK_Documentation.pdf

UPDATED LINK: WinFrame API SDK

Look at the table of WFInfoClass values. You will see a number of the parameters have the tag "3" indicating they are only available when called inside an ICA session. So you can call WFQuerySessionInformation with one of these, and if it returns false you are not running in an Citrix session. The IP address query you are doing currently is one of these properties, so when your "ok" variable is false you are not in an Citrix session.

Something else of interest, Microsoft provide the WTS api's which are very similar to WFAPI and do mostly the same thing. However WFAPI has the advantage that it will work with XenDesktop and XenApp, whereas the WTS APIs will only work with XenApp.

Regards, Donovan

Chris O
  • 5,017
  • 3
  • 35
  • 42
donovan
  • 1,442
  • 9
  • 18