[Shapelib] Converting my Shapefile C API reader to C#

C S usmsci at yahoo.com
Mon Jun 4 13:17:29 PDT 2007


hi all. i wrote a simple shapefile reader in C++ and
need to do the same now in C#. i am new to C# and
trying to get used to no include header files and
trying to wrap my head around assemblies. 

i would like to be able read in data from SHP & DBF
files   in C# but the API is in C. i have tried things
like doing DLL imports. i cannot write a C++/CLI
wrapper because i only have VS 2003. With that in mind
i have tried to write a straight C wrapper but am
unfamiliar with marshalling and not really sure how
all that works. most of the code out there that i have
seen doesn't involve 3rd party .DLL's. 

does anyone have any code/links with maybe an example
of how to read in a basic SHP or DBF file using C#? i
assume that since C# doesnt have header files i will
have to create the struct DBFInfo or the pointer to
the handle struct but i am not even sure the C#
compiler will be able to resolve that. here is the
code i have attempted: 

using System;
using System.Runtime.InteropServices;

namespace shapeFileReader
{
	[StructLayout(LayoutKind.Sequential)]
	struct DBFHandle
	{
	 public IntPtr fpSHP;
	 public IntPtr fpSHX;
	 public int nShapeType;
	 public int nFileSize;
	 public int nRecords;
	 public int nMaxRecords;
	 public IntPtr panRecOffset;
	 public IntPtr panRecSize;
 
	 [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	 public double[] adBoundsMin;

	 [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	 public double[] adBoundsMax;
     public int bUpdated;
	 public IntPtr pabyRec;
	 public int nBufSize;
	};

    class Class1
	{
        [DllImport("shapefile.dll")]
        static extern DBFHandle DBFOpen(string
pszShapeFile, string pszAccess);
		static void Main(string[] args)
		{
         DBFHandle myHandle;
         myHandle = DBFOpen("file.dbf", "rb");
         Console.WriteLine(myHandle.nRecords);
         Console.WriteLine("reading shapefile
parser");		 
		}
	}
}

some of this code i have gotten online and tried to
piece things together after reading a few other posts
but have come up short. i dont get any errors but i
get a Marshal Exception quoted here by the compiler:

'DefaultDomain': Loaded
'c:\winxp\microsoft.net\framework\v1.1.4322\mscorlib.dll',
No symbols loaded.
'shapeFileReader': Loaded 'C:\Apps\Microsoft Visual
Studio .NET
2003\VC#\CSharpProjects\shapeFileReader\bin\Debug\shapeFileReader.exe',
Symbols loaded.
An unhandled exception of type
'System.Runtime.InteropServices.MarshalDirectiveException'
occurred in shapeFileReader.exe

Additional information: Method's type signature is not
PInvoke compatible.


Unhandled Exception: The program '[3380]
shapeFileReader.exe' has exited with code 0 (0x0).
System.Runtime.InteropServices.MarshalDirectiveException:
Method's type signature is not PInvoke compatible.
   at shapeFileReader.Class1.DBFOpen(String
pszShapeFile, String pszAccess)
   at shapeFileReader.Class1.Main(String[] args) in
c:\apps\microsoft visual studio .net
2003\vc#\csharpprojects\shapefilereader\class1.cs:line
35


sorry for such a long post but i didnt want to leave
anything out. thanks in advance for your help!!!

CWS


       
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469



More information about the Shapelib mailing list