VC/C#中使用相同代码的结果不同 [英] Different result with same code in VC/C#

查看:70
本文介绍了VC/C#中使用相同代码的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



VC ++中的这段代码返回了正确的结果

    char proSrc [] ="+ proj = cass + ellps = bessel + lat_0 = 43d19 \ '05 .727 \" N"+ lon_0 = 11d19 \ '55 .9583 \"E" + towgs84 [-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68]";
    char proDst [] ="+ proj = latlong + datum = WGS84 + towgs84 [-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68]";
  
   项目空间;
    space.v = 150000;
    space.u = -150000;

    projPJ projSrc = pj_init_plus(proSrc);
    projPJ projDst = pj_init_plus(proDst);  

    space = pj_inv(space,projSrc); 

    double x = space.u;
    double y = space.v;  
    double z = 0;

    double ris = pj_transform(projSrc,projDst,1,0,& x,& y,& z);

...现在我已将代码导入C#中,如下所示:

   不安全的类程序
    {
      公共结构项目
       {
          公共双人u;
          公共双重v;
       }

       [DllImport(@"C:\ Users \ t \ Desktop \ Lib \ proj.dll",CallingConvention = CallingConvention.Cdecl,         EntryPoint ='_ plus )]
      静态外部void * pj_init_plus(string m);

       [DllImport(@"C:\ Users \ t \ Desktop \ Lib \ proj.dll",CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Auto,&EntbPoint = ="pj_inv"))]
      静态外部projUV pj_inv(projUV px,void * py);

       [DllImport(@"C:\ Users \ t \ Desktop \ Lib \ proj.dll",EntryPoint ="pj_transform",CallingConvention = CallingConvention.Cdecl)]
      静态外部int pj_transform(void * src,void * dst,long point_count,int point_offset,        b ;         double * x,double * y,double * z);
    
      静态void Main(string [] args)
       {

          字符串proSrc = @"+ proj = cass + ellps = bessel + lat_0 = 43d19'05.727"" + lon_0 = 11d19'55.9583"""E";

          字符串proDst ="+ proj = latlong + datum = WGS84 + towgs84 [-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68]";
  
           void * projSrc = pj_init_plus(proSrc);
           void * projDst = pj_init_plus(proDst);

          项目空间;
           space.v = 15000;
           space.u = -15000;

           space = pj_inv(space,projSrc);
         
           double x = space.u;
           y = space.v;
          双z = 0.0;

           double ris = pj_transform(projSrc,projDst,1,0,& x,& y,& z);

       }
    }

但现在最后调用的函数是"ris = pj_transform(projSrc,projDst,1,0,& x,& y,& z);"返回一个错误:尝试读取或写入受保护的内存.这通常表明其他内存已损坏.

我认为pj_init_plus(proSrc)中传递的字符串的编组可能是问题的原因,但我不知道如何解决...

有人可以帮助我吗?

提前谢谢,

古顿

这是程序原型

projLP pj_inv(projXY,projPJ);
int pj_transform(projPJ src,projPJ dst,long point_count,int point_offset,
] double * x,double * y,double * z);

projPJ pj_init_plus(const char *);


p.s.对不起,我的英语


Hi,

this code in VC++ return me correct result

    char proSrc[] = "+proj=cass +ellps=bessel +lat_0=43d19\'05.727\"N"" +lon_0=11d19\'55.9583\"E"" +towgs84[-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68]";
    char proDst[]="+proj=latlong +datum=WGS84 +towgs84[-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68]";
  
    projUV space;
    space.v = 150000;
    space.u = -150000;

    projPJ projSrc = pj_init_plus(proSrc);
    projPJ projDst = pj_init_plus(proDst);  

    space=pj_inv(space, projSrc); 

    double x=space.u;
    double y=space.v;  
    double z=0;

    double ris = pj_transform(projSrc, projDst, 1, 0, &x, &y, &z);

...now I have imported 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", CallingConvention = CallingConvention.Cdecl,         EntryPoint = "pj_init_plus")]
        static extern void* pj_init_plus(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", CallingConvention = CallingConvention.Cdecl)]
        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=cass +ellps=bessel +lat_0=43d19'05.727""N  +lon_0=11d19'55.9583""E";

            string proDst="+proj=latlong +datum=WGS84 +towgs84[-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68]";
  
            void* projSrc = pj_init_plus(proSrc);
            void* projDst = pj_init_plus(proDst);

            projUV space;
            space.v = 15000;
            space.u = -15000;

            space = pj_inv(space, projSrc);
          
            double x = space.u;
            double y = space.v;
            double z = 0.0;

            double ris = pj_transform(projSrc, projDst, 1, 0, &x, &y, &z);

        }
    }

but now last called function "ris = pj_transform(projSrc, projDst, 1, 0, &x, &y, &z);" return me an error:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I think that marshalling of string passed in pj_init_plus(proSrc) maybe the cause of problem but I don't understand how solve...

anyone can help me?

Thanks in advance,

Guton

this is the  procedure prototypes

projLP pj_inv(projXY, projPJ);
int pj_transform( projPJ src, projPJ dst, long point_count, int point_offset,
                  double *x, double *y, double *z );

projPJ pj_init_plus(const char *);


p.s. sorry for my english


 

推荐答案

为什么在C ++中使用projPJ但在C#中使用void *?为什么不在C#中也使用实际类型呢?空指针是弄碎东西的好方法.

Why do you use a projPJ in C++ but a void* in C#? Why not use the actual type in C# as well? Void pointers are a good way to get stuff broken.

您的projPJ结构/类实际上是什么样的?

What does your projPJ structure/class actually look like?


这篇关于VC/C#中使用相同代码的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆