参考http://www.168com.org/article.asp?id=38

解决方案 »

  1.   

    public bool AddGroup(string groupName, int bActive, int updateRate, out string error)
            {
                error = "";
                int dwLCID = 0x407; //本地语言为英语
                int pRevUpdateRate;
                float deadband = 0;
                // 处理非托管COM内存
                GCHandle hDeadband;
                IntPtr pTimeBias = IntPtr.Zero;
                IntPtr pDeadband = IntPtr.Zero;
                hDeadband = GCHandle.Alloc(deadband, GCHandleType.Pinned);
                try
                {
                    pIOPCServer.AddGroup(groupName, //组名
                    bActive, //创建时,组是否被激活
                    updateRate, //组的刷新频率,以ms为单位
                    hClientGroup, //客户号
                    pTimeBias, //这里不使用
                        //(IntPtr)hDeadband,
                    pDeadband,
                    dwLCID, //本地语言
                    out nSvrGroupID, //移去组时,用到的组ID号
                    out pRevUpdateRate, //返回组中的变量改变时的最短通知时间间隔
                    ref iidRequiredInterface,
                    out pobjGroup1); //指向要求的接口
                    hClientGroup = hClientGroup + 1;
                    int groupID = nSvrGroupID;
                    //groupsID.Add(groupName, groupID);
                    groups.Add(groupName, pobjGroup1);
                    groupsID.Add(groupName, groupID);
                }
                catch (System.Exception err) //捕捉失败信息
                {
                    error = "错误信息:" + err.Message;
                }
                finally
                {
                    if (hDeadband.IsAllocated) hDeadband.Free();
                }
                if (error == "")
                    return true;
                else
                    return false;
            }