Search code examples
c#historianproficy

Retrieve Proficy Historian tag names via IHUAPI


Using the c# User API wrapper for Proficy Historian, how can I retrieve all (or a filtered list of) tag names?

I have located the method ihuFetchTagCache, which populates a cache returns a count of tags, but I cannot find a way to access this cache.

My code so far:

string servername = "testServer";
int handle;
ihuErrorCode result;
result = IHUAPI.ihuConnect(servername, "", "", out handle);
if (result != ihuErrorCode.OK)
{//...}

int count;
result = IHUAPI.ihuFetchTagCache(handle, txtFilter.Text, out count);
if (result != ihuErrorCode.OK)
{//...}

How do I read the tag name cache?


Solution

  • It is actually better to use the new tag cache methods provided in 4.5 and above. Here are the DLL import definitions I use.1

    [DllImport("ihuapi.dll", EntryPoint = "ihuCreateTagCacheContext@0")]
    public static extern IntPtr CreateTagCacheContext();
    
    [DllImport("ihuapi.dll", EntryPoint = "ihuCloseTagCacheEX2@4")]
    public static extern ErrorCode CloseTagCacheEx2(IntPtr TagCacheContext);
    
    [DllImport("ihuapi.dll", EntryPoint = "ihuFetchTagCacheEx2@16")]
    public static extern ErrorCode FetchTagCacheEx2(IntPtr TagCacheContext, int ServerHandle, string TagMask, ref int NumTagsFound);
    
    [DllImport("ihuapi.dll", EntryPoint = "ihuGetTagnameCacheIndexEx2@12")]
    public static extern ErrorCode GetTagnameCacheIndexEx2(IntPtr TagCacheContext, string Tagname, ref int CacheIndex);
    
    [DllImport("ihuapi.dll", EntryPoint = "ihuGetNumericTagPropertyByIndexEx2@16")]
    public static extern ErrorCode GetNumericTagPropertyByIndexEx2(IntPtr TagCacheContext, int Index, TagProperty TagProperty, ref double Value);
    
    [DllImport("ihuapi.dll", EntryPoint = "ihuGetStringTagPropertyByIndexEx2@20")]
    public static extern ErrorCode GetStringTagPropertyByIndexEx2(IntPtr TagCacheContext, int Index, TagProperty TagProperty, StringBuilder Value, int ValueLength);
    

    Then you can use the following code.

    IntPtr context = IntPtr.Zero;
    try
    {
        context = IHUAPI.CreateTagCacheContext();
        if (context != IntPtr.Zero)
        {
            int number = 0;
            ihuErrorCode result = IHUAPI.FetchTagCacheEx2(context, Connection.Handle, mask, ref number);
            if (result == ihuErrorCode.OK)
            {
                for (int i = 0; i < number; i++)
                {
                    StringBuilder text = new StringBuilder();
                    IHUAPI.GetStringTagPropertyByIndexEx2(context, i, ihuTagProperties.Tagname, text, 128);
                    Console.WriteLine("Tagname=" + text.ToString());
                }
            }
        }
    }
    finally
    {
        if (context != IntPtr.Zero)
        {
            IHUAPI.CloseTagCacheEx2(context);
        }
    }
    

    1Note that I do not use the provided DLL import definitions provided by GE so my code may look slightly different, but the differences should be mostly trivial.