I follow this sample
http://trac.osgeo.org/proj/wiki/ProjAPI
but if I try to import this code in C# as follow
unsafe class Program
{
public struct projUV {public double u; public double v; }
[DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CharSet = CharSet.Auto,
EntryPoint = "pj_free")]
static extern void pj_free(void* p);
[DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "pj_init_plus")]
static extern void* pj_init_plus([MarshalAs(UnmanagedType.LPStr)]string m);
[DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto, EntryPoint = "pj_inv")]
static extern projUV pj_inv(projUV px, void* py);
[DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", EntryPoint = "pj_transform")]
static extern int pj_transform(void* src, void* dst, long point_count, int point_offset,
double* x, double* y, double* z);
static void Main(string[] args) {
string proSrc = "+proj=merc +ellps=clrk66 +lat_ts=33";
string proDst = "+proj=latlong +ellps=clrk66";
void* projSrc = pj_init_plus(proSrc);
void* projDst = pj_init_plus(proDst);
double x = 15000;
double y = 10000;
double z = 0.0;
double ris = pj_transform(projSrc, projDst, 1, 0, &x, &y, &z);
}
}
I have this error:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Note:
in the official sample there are this declaration: projPJ pj_merc, pj_latlong;
I use void* when call pj_init_plus but
what I wrong?
thanks
p.s. sorry for my english
G.
It wouldn’t hurt if you’d also mention the line number where the error is generated…
Aside from that, this is a common problem. The information below is taken from:
Original Source
1) Most of the time you will not see this kind of an error with purely .NET code. The CLR was designed to avoid these situations with developer code. However, when you go beyond purely managed code, an invalid pointer is what causes this problem. This can happen when you pass a pointer to an API. If the pointer is null (the address is 0), you will almost always get this error (assuming the unmanaged code doesn’t crash first). If the pointer contains an address value that is outside the allocated memory range for the app, or is in the range reserved for OS usage, then you will also get the error. Additionally, you can map memory with different access levels (actually, as a .NET programmer, you usually don’t, but some other unmanaged code might). If your process doesn’t have “read” access to that mapped memory range, you’ll also get this error.
In short, most of the time it’s a bug with your code trying to use unmanaged code (passing something by value instead of by ref, or passing a null pointer, or something to that effect), or it’s a bug in a 3rd party unmanaged library (even possibly a driver depending on what kind of code we’re interacting with here).
For further information, please take a look at the following thread and the above explanation is provided by Rob Teixeira :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt
2) In Visual Studio 2005, you can consider to set the Debugger as the following steps:
In VS menu, click Tools -> Options. In the Options panel, please choose Debugging -> General. Then please uncheck option “Suppress JIT optimization on module load (Managed Only).