Global Mapper v25.0

Invalid Layer error in GM_GetPathProfile after using GM_LoadLayerList

Hi,

I have a zip file that contains some .gmg files. In C#, I am using the GM_LoadLayerList function to load the files. The function returns no errors, and the out UInt32 "aNumLoadedLayers" number matches the number of files I have in the zip file. However, when I use the out IntPtr "aLayerList" in the GM_GetPathProfile function to get elevations from these layers specifically, I get the GM_Error_InvalidLayer exception.

Here is an example of my code:
            UInt32 numOfLayers;
            IntPtr aLayerList;

            gmError = GlobalMapperDLL.GM_LoadLayerList(zipFile, out aLayerList, out numOfLayers, 0);     
            gmError = GlobalMapperDLL.GM_GetPathProfile(aLayerList, lon1, lat1, lon2, lat2, ptrToElevArr, numOfPoitns, 0);
 
Any help is much appreciated. Thank you!





Answers

  • aaronc
    aaronc Trusted User
    Answer ✓
    Hello Obada,

    You are using a pointer to a layerhandle as a layerhandle.  You need to Marshal it.  This function will get all of the layer handles into an ArrayList:  

            public static ArrayList convertIntPtrtoLayerList(IntPtr aLayerListPtr, uint aNumLayers)
            {
                ArrayList theArrayList = new ArrayList();
                if ((aPtrListPtr == IntPtr.Zero) || (aNumPoints == 0))
                {
                    return theArrayList;
                }
                IntPtr tmpPoint = new IntPtr();
                for (int i = 0; i < aNumPoints; ++i)
                {
                    IntPtr tmpPointPtr = (IntPtr)((UInt32)aPtrListPtr + (i * IntPtr.Size));
                    tmpPoint = (IntPtr)Marshal.PtrToStructure(tmpPointPtr, typeof(IntPtr));
                    theArrayList.Add(tmpPoint);
                }

                return theArrayList;
            }

    Then you can just reference it as:

    gmError = GlobalMapperDLL.GM_LoadLayerList(zipFile, out aLayerList, out numOfLayers, 0);
    ArrayList theLayerList = convertIntPtrtoLayerList(aLayerList, uint numOfLayers);
    for (int i = 0; i < theLayerList.Count; ++i)
    {
        IntPtr aLayer = (IntPtr)ArrayList[i];
        gmError = GlobalMapperDLL.GM_GetPathProfile(aLayer, lon1, lat1, lon2, lat2, ptrToElevArr, numOfPoitns, 0);
    }

    this should do it for each layer loaded.
  • obada
    edited July 2016
    It worked. Thank you