使用 4.5 及更高版本提供的新标签缓存方法实际上更好。这是我使用的 DLL 导入定义。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);
然后你可以使用下面的代码。
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);
}
}
1请注意,我没有使用 GE 提供的 DLL 导入定义,因此我的代码看起来可能略有不同,但差异应该是微不足道的。