實際上使用4.5和更高版本中提供的新標籤緩存方法會更好。這裏是我使用的DLL導入定義。
[DllImport("ihuapi.dll", EntryPoint = "[email protected]")]
public static extern IntPtr CreateTagCacheContext();
[DllImport("ihuapi.dll", EntryPoint = "[email protected]")]
public static extern ErrorCode CloseTagCacheEx2(IntPtr TagCacheContext);
[DllImport("ihuapi.dll", EntryPoint = "[email protected]")]
public static extern ErrorCode FetchTagCacheEx2(IntPtr TagCacheContext, int ServerHandle, string TagMask, ref int NumTagsFound);
[DllImport("ihuapi.dll", EntryPoint = "[email protected]")]
public static extern ErrorCode GetTagnameCacheIndexEx2(IntPtr TagCacheContext, string Tagname, ref int CacheIndex);
[DllImport("ihuapi.dll", EntryPoint = "[email protected]")]
public static extern ErrorCode GetNumericTagPropertyByIndexEx2(IntPtr TagCacheContext, int Index, TagProperty TagProperty, ref double Value);
[DllImport("ihuapi.dll", EntryPoint = "[email protected]")]
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);
}
}
請注意,我不使用由GE提供的提供DLL導入的定義,所以我的代碼可能會略有不同,但差異應主要微不足道。