# [.net] how to import .lib files in your C# project(managed C# and unmanaged C/C++)

## Recommended Posts

hi all i am brand new to C# and am trying to do basic stuff like use 3rd party libraries to include in my project. since there is no #include like in C++ i need to know how to be able to use a .lib file and its associating .dll file. i tried the using keyword like this: using glu32.lib;(as just an example) but it tells me that that the namespace name could not be found even though i include the directory where the file resides in the project properties settings. what do you i need to do and how does the .dll play into all this? if i dont want to put the .dll inside win32 does it need to be in the project directory like in C++ or can it be in the same reference directory as the .lib file? thanks for your input! [Edited by - OpenGL_Guru on June 4, 2007 10:49:03 AM]

##### Share on other sites
Quote:
 since there is no #include like in C++ i need to know how to be able to use a .lib file and its associating .dll file.

Nitpick: #include is for textual substitution of other files; it does not have anything to do with linking against .lib files.

You can't just take any random .lib file and link against it. In C#, you reference assemblies, which are contained in .dll files. You have to right-click the References folder in the Solution Explorer, choose Add Reference, and browse to the reference you want to add.

Not all .dll files are assemblies; if you want to make use of OpenGL, for example, you'll have to obtain an assembly that is a managed wrapper around the native GL class. The Tao framework is one example.

##### Share on other sites
Quote:
Original post by jpetrie
Quote:
 since there is no #include like in C++ i need to know how to be able to use a .lib file and its associating .dll file.

Nitpick: #include is for textual substitution of other files; it does not have anything to do with linking against .lib files.

You can't just take any random .lib file and link against it. In C#, you reference assemblies, which are contained in .dll files. You have to right-click the References folder in the Solution Explorer, choose Add Reference, and browse to the reference you want to add.

Not all .dll files are assemblies; if you want to make use of OpenGL, for example, you'll have to obtain an assembly that is a managed wrapper around the native GL class. The Tao framework is one example.

well the openGL was a bad example. i am looking to read in shapefiles which is GIS stuff and there is an API that comes with it. when you build it it creates a .lib file and a .dll file. so how in the world do you get the assembly for that or any other .dll? and forgive me for asking but why did MS have to make it THAT difficult to simply use stuff that should be simple?

by the way - i am using .NET 2003. i dont know if that makes a difference.

##### Share on other sites
Quote:
 well the openGL was a bad example. i am looking to read in shapefiles which is GIS stuff and there is an API that comes with it. when you build it it creates a .lib file and a .dll file. so how in the world do you get the assembly for that or any other .dll?

You don't. Libraries compiled to native code cannot, unless they are COM components, be accessed by managed code directly. You can use Platform Invoke to access specific functions, or you can write a managed wrapper, or you can find an existing managed wrapper.

Quote:
 and forgive me for asking but why did MS have to make it THAT difficult to simply use stuff that should be simple?

Microsoft did no such thing (in fact, they've done an excellent job of making interop to native code very easy); you just think its hard, or should be simple/possible because you don't yet have enough of an understanding of the differences between the two domains you're trying to connect.

What your basically asking is akin to "why can't my square peg fit into this round hole?" C# is a managed language, the code you seem to be compiling is unmanaged code. They don't just play nice by default; effort needs to be put in to make them compatible (typically by using a managed wrapper written in C++/CLI, or by exposing the unmanaged interface via COM, or p/invoke).

##### Share on other sites
Quote:
Original post by jpetrie
Quote:
 well the openGL was a bad example. i am looking to read in shapefiles which is GIS stuff and there is an API that comes with it. when you build it it creates a .lib file and a .dll file. so how in the world do you get the assembly for that or any other .dll?

You don't. Libraries compiled to native code cannot, unless they are COM components, be accessed by managed code directly. You can use Platform Invoke to access specific functions, or you can write a managed wrapper, or you can find an existing managed wrapper.

Quote:
 and forgive me for asking but why did MS have to make it THAT difficult to simply use stuff that should be simple?

Microsoft did no such thing (in fact, they've done an excellent job of making interop to native code very easy); you just think its hard, or should be simple/possible because you don't yet have enough of an understanding of the differences between the two domains you're trying to connect.

What your basically asking is akin to "why can't my square peg fit into this round hole?" C# is a managed language, the code you seem to be compiling is unmanaged code. They don't just play nice by default; effort needs to be put in to make them compatible (typically by using a managed wrapper written in C++/CLI, or by exposing the unmanaged interface via COM, or p/invoke).

would you mind giving a basic example of this?? i keep reading about things like [DllImport("someDLL.dll")] but confused as to where to put it. can you run this in console mode or windows application mode?

##### Share on other sites
the best way is to create C++/Cli library.
In Visual Studio 2005 is extremely easy (I've realised a managed DirectX10 engine with no problem).
Just create managed class in C++ and use lib class.
The only complex part are array and string that required some practice with marshal class.

##### Share on other sites
Quote:
 Original post by robydxthe best way is to create C++/Cli library.In Visual Studio 2005 is extremely easy (I've realised a managed DirectX10 engine with no problem).Just create managed class in C++ and use lib class.The only complex part are array and string that required some practice with marshal class.

#1 i am using VS 2003, so is creating this library still doable?

#2.if #1 is true are there any tutorials on doing this cos i have no idea how to do it. thanks!

EDIT: i found this tutorial here so i am looking at Solution B. the .dll cannot be changed(3rd party .DLL) so this seems like the viable solution. what do you think? THANKS!

[Edited by - OpenGL_Guru on June 1, 2007 1:37:08 PM]

##### Share on other sites
Just a question for the sake of clarity: is the library written in c or c++?

The two will need different solutions.

If it's c, the solution is simple (as jpetrie said, the .net frameworks ability to work with native libraries is impressive. Not only that, but you need to do this with any non-c language, such as pascal). You use the dllimport attribute you mentioned. I won't go into details, since there's a TON of examples of how to use it (as a quick google search showed me). But basically, you provide the name of the dll, the name of the funtion, what it returns, and what arguments it takes. Basically you're telling the compiler how to use the function. That'd be the job of the header in c++, but you have to do it yourself here.

If it's a c++ library, then it's much more complicated, because you can't directly interact with it. You need to use a utility like swig to create a C dll that wraps the c++ library. You can then interact with the C library using the dllimport attribute.

And as was said, you could also create a wrapper with c++/clr, but this requires vs.net 2005 and knowledge of c++. Unfortunately vs.net 2003 doesn't support c++/clr. It has "managed extensions for c++" which is basically ugly .net functionality hacked onto c++. It's also buggy: for example, virtual functions that return a bool always return false. There's other bugs, but I can't think of them at the moment. Steer clear of it. Either use the swig method or upgrade to vs.net 2005. Also, keep in mind that the swig method is crossplatform, while the c++/clr method will only work in windows.

This inconvenience is one of the few major downsides to using a managed language, and as was said, there's no choice due to the fundamentally different way they work.

##### Share on other sites
Quote:
 Original post by gharen2Just a question for the sake of clarity: is the library written in c or c++?The two will need different solutions.If it's c, the solution is simple (as jpetrie said, the .net frameworks ability to work with native libraries is impressive. Not only that, but you need to do this with any non-c language, such as pascal). You use the dllimport attribute you mentioned. I won't go into details, since there's a TON of examples of how to use it (as a quick google search showed me). But basically, you provide the name of the dll, the name of the funtion, what it returns, and what arguments it takes. Basically you're telling the compiler how to use the function. That'd be the job of the header in c++, but you have to do it yourself here.If it's a c++ library, then it's much more complicated, because you can't directly interact with it. You need to use a utility like swig to create a C dll that wraps the c++ library. You can then interact with the C library using the dllimport attribute.And as was said, you could also create a wrapper with c++/clr, but this requires vs.net 2005 and knowledge of c++. Unfortunately vs.net 2003 doesn't support c++/clr. It has "managed extensions for c++" which is basically ugly .net functionality hacked onto c++. It's also buggy: for example, virtual functions that return a bool always return false. There's other bugs, but I can't think of them at the moment. Steer clear of it. Either use the swig method or upgrade to vs.net 2005. Also, keep in mind that the swig method is crossplatform, while the c++/clr method will only work in windows.This inconvenience is one of the few major downsides to using a managed language, and as was said, there's no choice due to the fundamentally different way they work.

gharen. YES the Library is indeed C. here is a link the functions available.. which is here

if i use the dllImport feature does that mean i dont need to create anything in VC++ (VC 2003) and just reference the parameters as per the functions/parameter list etc that you mentioned??

i am looking at this particular function for example from that link:

SHPHandle SHPOpen( const char * pszShapeFile, const char * pszAccess );

here is my C# code:
using System;using System.Runtime.InteropServices;namespace shapeFileReader{    class Class1	{        [DllImport("shapefile.dll")]        static extern SHPHandle SHPOpen(const char *pszShapeFile, const char *pszAccess);		static void Main(string[] args)		{         Console.WriteLine("reading shapefile parser");		}	}}

i get the error though when i try to build it:
Quote:
 c:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs(9,42): error CS1031: Type expectedc:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs(9,67): error CS0145: A const field requires a value to be providedc:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs(9,69): error CS1041: Identifier expected, 'const' is a keywordc:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs(9,92): error CS0145: A const field requires a value to be provided

so for this example what is the compiler really complaining about? i am just telling the compiler what the definition of the function is correct? Thanks for everyones help. when i get this working perhaps i could make a readme on nehe or something if it hasnt been written yet :) anyway look forward to hearing from ya. thanks again.

##### Share on other sites
OK, you are probably going to have to get the header file(s) for your library out and take a look at the types and definitions in them. There are a few problems with your example. First, you should use string types instead of const char *, because that is how C# uses strings. Secondly, SHPHandle definition (I am assuming it is some kind of struct) needs to be rewritten in C#, using the struct identifier. This is how your example should look, keeping in mind the fact that I have no idea what exactly the SHPHandle structure actually looks like:

using System;using System.Runtime.InteropServices;namespace shapeFileReader{    [StructLayout(LayoutKind.Sequential)]    struct SHPHandle    {        // whatever members SHPHandle has go here    }    class Class1    {        [DllImport("shapefile.dll")]        static extern SHPHandle SHPOpen(string pszShapeFile, string pszAccess);	static void Main(string[] args)	{            Console.WriteLine("reading shapefile parser");	}    }}

##### Share on other sites
Quote:
 Original post by ussnewjersey4OK, you are probably going to have to get the header file(s) for your library out and take a look at the types and definitions in them. There are a few problems with your example. First, you should use string types instead of const char *, because that is how C# uses strings. Secondly, SHPHandle definition (I am assuming it is some kind of struct) needs to be rewritten in C#, using the struct identifier. This is how your example should look, keeping in mind the fact that I have no idea what exactly the SHPHandle structure actually looks like:*** Source Snippet Removed ***

first off, according to the definition of the C API it must be char *. i gave the link up above.

the function definitions are in the that C API that you will see.. anything else to add after looking at that link? about the SHPHandle according to that link:

"The contents of the SHPHandle are visible (see shapefile.h) but should be ignored by the application. It is intended that all information be accessed by the API functions." --

thanks for your help so far, its great rating++ to everyone, i was just wondering if you had anything else to add after looking at the link above. thanks...

oh yeah what does this do or mean??

[StructLayout(LayoutKind.Sequential)]

##### Share on other sites
Quote:
Original post by OpenGL_Guru
Quote:
 Original post by ussnewjersey4OK, you are probably going to have to get the header file(s) for your library out and take a look at the types and definitions in them. There are a few problems with your example. First, you should use string types instead of const char *, because that is how C# uses strings. Secondly, SHPHandle definition (I am assuming it is some kind of struct) needs to be rewritten in C#, using the struct identifier. This is how your example should look, keeping in mind the fact that I have no idea what exactly the SHPHandle structure actually looks like:*** Source Snippet Removed ***

first off, according to the definition of the C API it must be char *. i gave the link up above.

the function definitions are in the that C API that you will see.. anything else to add after looking at that link? thanks for your help so far, its great rating++ to everyone, i was just wondering if you had anything else to add after looking at the link above. thanks...

oh yeah what does this do or mean??

[StructLayout(LayoutKind.Sequential)]

OK, a few things here. First of all, they can (and should) be specified as strings, because Microsoft did an amazing job with their Marshaller, which handles the conversion between types. Even though the API does specify a char*, they mean to use it as a string, which is how it should be imported.

Secondly, I am pretty sure that you cannot declare variables inside of functions as const. That keyword is reserved for when you want a constant field inside of a class.

Third, the StructLayout attribute tells the compiler to keep the structure laid out in memory exactly as you have entered it. This keeps the compiler from rearranging things to make them more efficient and memory friendly. Normally, you want the compiler to do this, but when you are importing something, you have no idea what the compiler might do to the structure, so you want to make sure everything stays in order.

Fourth, you will have to find the definition of the SHPHandle object and declare it in C#, as I stated above. I am not going to track the object down and write it for you, just search around and figure out what it is supposed to be. The link you specified does not contain the SHPHandle object, and I am not going to spend my time looking for it. The API manual says that you do not need to know what it is, but that is only in the case that you are using the API in C. If you are writing it in C#, how is the compiler supposed to know what the heck SHPHandle is supposed to be, if you don't tell it?

Finally, I hope you figure out how to make everything work, and good luck with your project. :)

##### Share on other sites
Quote:
Original post by ussnewjersey4
Quote:
Original post by OpenGL_Guru
Quote:
 Original post by ussnewjersey4OK, you are probably going to have to get the header file(s) for your library out and take a look at the types and definitions in them. There are a few problems with your example. First, you should use string types instead of const char *, because that is how C# uses strings. Secondly, SHPHandle definition (I am assuming it is some kind of struct) needs to be rewritten in C#, using the struct identifier. This is how your example should look, keeping in mind the fact that I have no idea what exactly the SHPHandle structure actually looks like:*** Source Snippet Removed ***

first off, according to the definition of the C API it must be char *. i gave the link up above.

the function definitions are in the that C API that you will see.. anything else to add after looking at that link? thanks for your help so far, its great rating++ to everyone, i was just wondering if you had anything else to add after looking at the link above. thanks...

oh yeah what does this do or mean??

[StructLayout(LayoutKind.Sequential)]

OK, a few things here. First of all, they can (and should) be specified as strings, because Microsoft did an amazing job with their Marshaller, which handles the conversion between types. Even though the API does specify a char*, they mean to use it as a string, which is how it should be imported.

Secondly, I am pretty sure that you cannot declare variables inside of functions as const. That keyword is reserved for when you want a constant field inside of a class.

Third, the StructLayout attribute tells the compiler to keep the structure laid out in memory exactly as you have entered it. This keeps the compiler from rearranging things to make them more efficient and memory friendly. Normally, you want the compiler to do this, but when you are importing something, you have no idea what the compiler might do to the structure, so you want to make sure everything stays in order.

Fourth, you will have to find the definition of the SHPHandle object and declare it in C#, as I stated above. I am not going to track the object down and write it for you, just search around and figure out what it is supposed to be. The link you specified does not contain the SHPHandle object, and I am not going to spend my time looking for it. The API manual says that you do not need to know what it is, but that is only in the case that you are using the API in C. If you are writing it in C#, how is the compiler supposed to know what the heck SHPHandle is supposed to be, if you don't tell it?

Finally, I hope you figure out how to make everything work, and good luck with your project. :)

sorry i didnt want or intend for you think i was wanted to be spoonfed. i was just curious more than not about why certain things would work or not. the SHPHandle object is located in the .h file and is described as:

typedef	struct{    FILE        *fpSHP;    FILE	*fpSHX;    int		nShapeType;				/* SHPT_* */        int		nFileSize;				/* SHP file */    int         nRecords;    int		nMaxRecords;    int		*panRecOffset;    int		*panRecSize;    double	adBoundsMin[4];    double	adBoundsMax[4];    int		bUpdated;    unsigned char *pabyRec;    int         nBufSize;} SHPInfo;typedef SHPInfo * SHPHandle;

just FYI. i will take what you have given me and hopefully it all works out. thanks!

##### Share on other sites
Also note that in many cases, you can use IntPtr instead of the actual struct, as a quick and dirty solution. So if you don't need to actually use the SHPInfo struct, you can have the function return an IntPtr:

        [DllImport("shapefile.dll")]        static extern IntPtr SHPOpen(string pszShapeFile, string pszAccess);

In fact I'm pretty sure you'll have to do this anyways, as the compiler won't be smart enough to marshal the pointer to your declaration of SHPInfo. The compiler is good at converting managed types you pass to a function, but not so good at return types. You may need to do something like:

        [DllImport("shapefile.dll", EntryPoint = "SHPOpen")]        private static extern IntPtr internalSHPOpen(string pszShapeFile, string pszAccess);        public static SHPInfo SHPOpen(string pszShapeFile, string pszAccess)        {             IntPtr ptr = internalSHPOpen(pszShapeFile, pszAccess);             return (SHPInfo)Marshal.PtrToStructure(ptr, typeof(SHPInfo));        }

A pain in the butt I know, but that's the way it is.

Also note that in your c# declaration of SHPInfo, you can also replace a lot of the pointers with IntPtr.

##### Share on other sites
Sorry, didn't mean to sound hostile. You have to understand what is going on under the hood in order to realize why some things need to be changed, while others don't. Strings are a native part of the .NET library, and so they have a special Marshal that handles converting the data over from a const char* into a string object.

The SHPHandle object, on the other hand, is not a built in part of the .NET framework. Because you are not including the header file (.H) that the SHPHandle object is declared in, the C# compiler has no way of knowing what is might look like. Therefore, you have to declare it in C#, so that the compiler will be able to correctly import it.

This is how your structure would look in C#. Notice the StructLayout attribute, which ensures that everything will be imported correctly. Also, realize that the names don't have to be the same, you can always change them, at least for structures, but make sure that the types stay the same. For example, you could call nShapeType "shapeType", "SHAPETYPE", or even "SDFKLJSD", it wouldn't make a difference to the compiler.

[StructLayout(LayoutKind.Sequential)]struct SHPHandle{    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;}

Notice the IntPtr reference. Many types in C++ or C, such as the FILE type, are really pointers to integers underneath. Because there are no pointers in C# (well, actually, there are, but you should never use them unless you are really sure about what you are doing), the IntPtr class does the work for you.

This is how I approach a PInvoke project. First, I look at the first type that I need to translate. If it is a built in type, like bool, int, double, etc, then I just copy that over. If it is a different type, I first look to see if it is really just an alias for an integer pointer. If it is, I write the type as IntPtr. Otherwise, it is probably a struct, in which case you need to go about rewriting the struct, translating each type as you go along.

A few other pointers. First, you might notice the MarshalAs attribute, which helps you tell the compiler how certain objects should be marshalled. In the case of the code above, you know the size of the array, so you can specify it in the marshal. Another good candidate for a marshal is the boolean type, which can be represented in many different ways in unmanaged code. For more information on this type of stuff, you can always check Google and MSDN.

EDIT: Doh! Gharen beat me to it. He is right, of course. If you don't ever need to access the SHPHandle structure, don't bother converting it. Just pass around an IntPtr to each function in place of the actual structure.

##### Share on other sites
yes i will need access to the SHPHandle structure, because it will allow me to get information from the .shp files that i will need to manipulate the data from within the file. i will take your last post as a thing to work on and if any errors come up ill throw them your way. talk later and thanks again :)

[Edited by - OpenGL_Guru on June 4, 2007 10:14:20 AM]

##### Share on other sites
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("BG2k_clip.dbf", "rb");         Console.WriteLine(myHandle.nRecords);         Console.WriteLine("reading shapefile parser");		 		}	}}

this is what i have so far. i was trying to see if i could just simply write out the records in the file and it doesnt complain but when i run it i get an exception error that pops up:

Quote:
 Unhandled Exception: The program '[3968] 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

the exception caught occurs at the arrow that typed in in the code. anything i didnt do or am doing wrong here? i wonder if it has to do with something that was mentioned earlier about the C declaration of the struct:

typedef SHPInfo * SHPHandle;
??

##### Share on other sites
The solution to that error is the method I posted. Have the function return an IntPtr, and marshal that to a struct yourself. It's not smart enough to do it for you.

A bit more you may need to know, regarding disposing of the pointer:

You'll need to keep track of the IntPtr still, as it contains the actual pointer, while the struct you marshal from it just contains a representation of the data the pointer points to. Destroying or disposing the struct won't have any effect on the pointer. You need to keep the IntPtr, and pass that to whatever function disposes of it.

##### Share on other sites
Quote:
 Original post by gharen2The solution to that error is the method I posted. Have the function return an IntPtr, and marshal that to a struct yourself. It's not smart enough to do it for you.A bit more you may need to know, regarding disposing of the pointer:You'll need to keep track of the IntPtr still, as it contains the actual pointer, while the struct you marshal from it just contains a representation of the data the pointer points to. Destroying or disposing the struct won't have any effect on the pointer. You need to keep the IntPtr, and pass that to whatever function disposes of it.

thanks! well here is the code that i have now:

using System;using System.Runtime.InteropServices;using System.Text;namespace shapeFileReader{	[StructLayout(LayoutKind.Sequential)]	struct DBFInfo	{	 public IntPtr fp;	 public int nRecords;	 public int	nRecordLength;         public int	nHeaderLength;         public int	nFields;         public IntPtr	panFieldOffset;         public IntPtr	panFieldSize;         public IntPtr	panFieldDecimals;         public string  pachFieldType;         public string  pszHeader;	 public int	nCurrentRecord;         public int	bCurrentRecordModified;         public string  pszCurrentRecord;         public int	bNoHeader;         public int     bUpdated;	 public IntPtr  pabyRec;	 public int     nBufSize;	};    class Class1	{		[DllImport("shapelib.dll", EntryPoint = "DBFOpen")]		private static extern IntPtr internalDBFOpen(string pszShapeFile, string pszAccess);		public static DBFInfo DBFOpen(string pszShapeFile, string pszAccess)		{  Exception---->>IntPtr ptr = internalDBFInfo(pszShapeFile, pszAccess);		 return (DBFInfo)Marshal.PtrToStructure(ptr, typeof(DBFInfo));		}		static void Main(string[] args)		{         DBFInfo myHandle;         myHandle = DBFOpen("BG2k_clip.dbf", "rb");         Console.WriteLine("reading shapefile parser");		 		}	}}

when i try to run this i get a DLL exception quoted exactly here:

Quote:
 The program '[3024] shapeFileReader.exe' has exited with code 0 (0x0).Unhandled Exception: System.DllNotFoundException: Unable to load DLL (shapelib.dll). at shapeFileReader.Class1.internalDBFOpen(String pszShapeFile, String pszAccess) at shapeFileReader.Class1.DBFOpen(String pszShapeFile, String pszAccess) in c:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs:line 37 at shapeFileReader.Class1.Main(String[] args) in c:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs:line 46

its pretty clear that it cannot open the DLL. any clue as to what i am doing wrong or what i need to do to make it read the .DLL? thanks a lot for your help so far. rating++

[Edited by - OpenGL_Guru on June 5, 2007 12:29:55 PM]

##### Share on other sites
Where is the actual DLL file? It needs to be in the same directory as the executable, or in the C:/Windows/System32 folder.

EDIT: I also just realized that you seem to be changing the names of things between your posts. For example, you are trying to load the DBFOpen function from both "shapelib.dll" and "shapefile.dll". Which one is it? Also, is the name of the actual function DBFOpen, or are you changing it to fit your own scheme? If that is the case, make sure that the EntryPoint attribute is set to the *ACTUAL* name of the function that will be loaded from the DLL, or the runtime won't be able to find it.

##### Share on other sites
yeah i know i have changed a few things around. the .dll is shapelib.dll and i had it in the exe folder but went ahead and put it in winXP/system32. the way the shapefiles work is that you have a .SHP file that has all the coordinate information and then in the .DBF file you have the actual data associated with the data in the .SHP. i was merely trying to open a .DBF file.

this format is about the same as the .SHP and can be viewed here .

DBFInfo in the shapefil.h is defined as:

typedef	struct{    FILE	*fp;    int         nRecords;    int		nRecordLength;    int		nHeaderLength;    int		nFields;    int		*panFieldOffset;    int		*panFieldSize;    int		*panFieldDecimals;    char	*pachFieldType;    char	*pszHeader;    int		nCurrentRecord;    int		bCurrentRecordModified;    char	*pszCurrentRecord;        int		bNoHeader;    int		bUpdated;} DBFInfo;typedef DBFInfo * DBFHandle;

of course that can be changed and looking now it might be different than the struct i have created right now(EDIT: i went ahead and changed everything above for DBFInfo just for readability) but that can easily be changed. i wont need all the members of the struct anyway just 3 or 4. its just not seeing the .DLL file -- and looking at the link above DBFOpen(...) looks correct to me.

I should note that if i dont try to call DBFOpen(...) it runs fine and i get my little console output. no runtime or exception errors. even intellisense recognizes the DBFOpen function when i type in myHandle = DBFOpen(...); but when i do this thats when i get the exception error about not finding the .DLL.

Quote:
 make sure that the EntryPoint attribute is set to the *ACTUAL* name of the function that will be loaded from the DLL, or the runtime won't be able to find it.

i looked inside the shapelib.DEF file and it reads as this:
Quote:

[Edited by - OpenGL_Guru on June 5, 2007 12:20:43 PM]

##### Share on other sites
The error doesn't appear until you actually call DBFOpen because the .net framework doesn't load the dll into memory until it's actually needed. When you see DBFOpen with intellisense, you're just looking at your declaration of it, not the actual function.

I know it sounds simple, but double check the name of the dll and that it is in fact in the same place as the exe. Based on what you've posted, it sounds like the dll was loading when you used "shapefile.dll" (though it was throwing the MarshalDirectiveException, the fact that you reached that point indicates the dll was found and loaded).

For it to suddenly complain about not being able to find it when you change it to "shapelib.dll" sets off alarm bells. If you changed the name, make sure you actually put the new version with the exe.

##### Share on other sites
Quote:
 Original post by gharen2The error doesn't appear until you actually call DBFOpen because the .net framework doesn't load the dll into memory until it's actually needed. When you see DBFOpen with intellisense, you're just looking at your declaration of it, not the actual function.I know it sounds simple, but double check the name of the dll and that it is in fact in the same place as the exe. Based on what you've posted, it sounds like the dll was loading when you used "shapefile.dll" (though it was throwing the MarshalDirectiveException, the fact that you reached that point indicates the dll was found and loaded).For it to suddenly complain about not being able to find it when you change it to "shapelib.dll" sets off alarm bells. If you changed the name, make sure you actually put the new version with the exe.

the .DLL was never shapefile.dll. that was my mistake. it is in fact shapelib.dll. i think i just saw shapefil.h(yes it is without the 'e') and just put shapefile.dll. just to tell you that i did it though i changed it back to shapefile.dll and it gave me the same error -- couldnt find the .dll file. i have never changed the contents or name of the dll, since its 3rd party and comes as is.

##### Share on other sites
hey gharen and uss(and anyone else)

here is the code that i have:

using System;//using System.Collections.Generic;using System.Runtime.InteropServices;using System.Text;//using MapTools;namespace shapeFileReader{	[StructLayout(LayoutKind.Sequential)]	struct DBFInfo	{		public IntPtr fp;		public int      nRecords;		public int	    nRecordLength;		public int	    nHeaderLength;		public int	    nFields;		public IntPtr	panFieldOffset;		public IntPtr	panFieldSize;		public IntPtr	panFieldDecimals;		public string   pachFieldType;		public string   pszHeader;		public int	    nCurrentRecord;		public int	    bCurrentRecordModified;		public string   pszCurrentRecord;		public int	    bNoHeader;		public int      bUpdated;		public IntPtr   pabyRec;		public int      nBufSize;	};    class Class1	{		[DllImport("shapelib.dll", EntryPoint = "DBFOpen")]		private static extern IntPtr internalDBFOpen(string pszShapeFile, string pszAccess);		public static DBFInfo DBFOpen(string pszShapeFile, string pszAccess)		{			IntPtr ptr = internalDBFOpen(pszShapeFile, pszAccess);			--->>>return (DBFInfo)Marshal.PtrToStructure(ptr, typeof(DBFInfo));		}		static void Main(string[] args)		{          DBFInfo myHandle;          myHandle = DBFOpen("BG2k_clip.dbf", "rb");          Console.WriteLine("reading shapefile parser");		 		}	}}

here is the ORIGINAL DBF struct along with an enum definition that i will probably need as well.

typedef	struct{    FILE	*fp;    int         nRecords;    int		nRecordLength;    int		nHeaderLength;    int		nFields;    int		*panFieldOffset;    int		*panFieldSize;    int		*panFieldDecimals;    char	*pachFieldType;    char	*pszHeader;    int		nCurrentRecord;    int		bCurrentRecordModified;    char	*pszCurrentRecord;        int		bNoHeader;    int		bUpdated;} DBFInfo;typedef DBFInfo * DBFHandle;typedef enum {  FTString,  FTInteger,  FTDouble,  FTLogical,  FTInvalid} DBFFieldType;

and get a NEW exception for some reason(?????), not the usual cant find .DLL exception i was getting. the error refers the arrow by the code above and the error is:

Quote:
 The program '[3808] shapeFileReader.exe' has exited with code 0 (0x0).Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at shapeFileReader.Class1.DBFOpen(String pszShapeFile, String pszAccess) in c:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs:line 39 at shapeFileReader.Class1.Main(String[] args) in c:\apps\microsoft visual studio .net 2003\vc#\csharpprojects\shapefilereader\class1.cs:line 44

when i was reading these files in C++ the handles always had to be initialized to NULL because its a pointer to DBFInfo. since i dont have a pointer to the struct that i re-defined i dont know how to work around this. this might be what its complaining about i am not sure. any thoughts?

##### Share on other sites
That error indicates that the return value is IntPtr.Zero (0, in other words a NULL pointer).

Then the exception is thrown because you're trying to create a struct from a NULL pointer.

Usually when a C function returns NULL, it means there was an error. Perhaps you're not passing the right values to it? Does this library have the means to obtain error messages?

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978614

• 13
• 12
• 10
• 12
• 22