Пример #1
0
        public static List<RoadModel> getRoadInfoByDll()
        {
            int roadNum = 10;

            ROAD_INFO[] infos = new ROAD_INFO[roadNum];
            for (int i = 0; i < roadNum; i++)
            {
                infos[i] = new ROAD_INFO();
            }

            IntPtr[] ptArr = new IntPtr[1];
            ptArr[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ROAD_INFO)) * roadNum); //分配包含两个元素的数组
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ROAD_INFO)));
            Marshal.Copy(ptArr, 0, pt, 1); //拷贝指针数组

            getRoadInfo(pt, roadNum);

            List<RoadModel> roadList = new List<RoadModel>();
            for (int i = 0; i < roadNum; i++)
            {
                infos[i] = (ROAD_INFO)Marshal.PtrToStructure((IntPtr)(pt.ToInt32() + i * Marshal.SizeOf(typeof(ROAD_INFO))), typeof(ROAD_INFO));
                RoadModel road = new RoadModel();
                road.RoadID = infos[i].roadNum.ToString();
                road.RoadID = infos[i].roadName;
                roadList.Add(road);
            }

            return roadList;
        }
Пример #2
0
        public static bool setRoadInfoByDll(List<RoadModel> roadList)
        {
            int roadNum = roadList.Count;

            ROAD_INFO[] infos = new ROAD_INFO[roadNum];
            for (int i = 0; i < roadNum; i++)
            {
                infos[i] = new ROAD_INFO();
            }

            IntPtr[] ptArr = new IntPtr[1];
            ptArr[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ROAD_INFO)) * roadNum); //分配包含两个元素的数组
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ROAD_INFO)));
            Marshal.Copy(ptArr, 0, pt, 1); //拷贝指针数组

            bool result = getRoadInfo(pt, roadNum);

            return result;
        }