作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试公开需要从 GeoTrans c++ 库调用的方法,但遇到了问题。任何帮助都会很棒!
我有以下 c++ 文件,我正在运行 nmake 以将其编译成 dll。
#include <iostream>
#include "CoordinateConversionService.h"
#include "CoordinateSystemParameters.h"
#include "GeodeticParameters.h"
#include "CoordinateTuple.h"
#include "GeodeticCoordinates.h"
#include "CartesianCoordinates.h"
#include "Accuracy.h"
#include "MGRSorUSNGCoordinates.h"
#include "UTMParameters.h"
#include "UTMCoordinates.h"
#include "CoordinateType.h"
#include "HeightType.h"
#include "CoordinateConversionException.h"
using MSP::CCS::Precision;
int main(int argc, char **argv){}
extern "C"__declspec(dllexport) void __stdcall convertGeodeticToGeocentric(const double lat,const double lon, const double height, double& x, double& y, double& z)
{
MSP::CCS::CoordinateSystemParameters geocentricParameters(MSP::CCS::CoordinateType::geocentric);
MSP::CCS::CoordinateConversionService ccs( "WGE", &geodeticParameters, "WGE", &geocentricParameters );
MSP::CCS::Accuracy sourceAccuracy;
MSP::CCS::Accuracy targetAccuracy;
MSP::CCS::GeodeticCoordinates sourceCoordinates(MSP::CCS::CoordinateType::geodetic, lon, lat, height);
MSP::CCS::CartesianCoordinates targetCoordinates(MSP::CCS::CoordinateType::geocentric);
ccs.convertSourceToTarget( &sourceCoordinates, &sourceAccuracy, targetCoordinates, targetAccuracy );
x = targetCoordinates.x();
y = targetCoordinates.y();
z = targetCoordinates.z();
}
extern "C"__declspec(dllexport) void __stdcall convertGeocentricToGeodetic(const double x, const double y, const double z, double& lat,double& lon, double& height)
{
MSP::CCS::CoordinateSystemParameters geocentricParameters(MSP::CCS::CoordinateType::geocentric);
MSP::CCS::GeodeticParameters geodeticParameters(MSP::CCS::CoordinateType::geodetic, MSP::CCS::HeightType::ellipsoidHeight);
MSP::CCS::CoordinateConversionService ccs( "WGE", &geocentricParameters, "WGE", &geodeticParameters );
MSP::CCS::Accuracy sourceAccuracy;
MSP::CCS::Accuracy targetAccuracy;
MSP::CCS::CartesianCoordinates sourceCoordinates(MSP::CCS::CoordinateType::geocentric, x, y, z);
MSP::CCS::GeodeticCoordinates targetCoordinates;
ccs.convertSourceToTarget( &sourceCoordinates, &sourceAccuracy, targetCoordinates, targetAccuracy );
lat = targetCoordinates.latitude();
lon = targetCoordinates.longitude();
height = targetCoordinates.height();
}
extern "C"__declspec(dllexport) void __stdcall convertGeocentricToUTM(const double x, const double y, const double z, long& zone, char& hemisphere, double& easting, double& northing)
{
MSP::CCS::CoordinateSystemParameters geocentricParameters(MSP::CCS::CoordinateType::geocentric);
MSP::CCS::UTMParameters utmParameters(MSP::CCS::CoordinateType::universalTransverseMercator, 1, 0);
MSP::CCS::CoordinateConversionService ccs( "WGE", &geocentricParameters, "WGE", &utmParameters );
MSP::CCS::Accuracy sourceAccuracy;
MSP::CCS::Accuracy targetAccuracy;
MSP::CCS::CartesianCoordinates sourceCoordinates(MSP::CCS::CoordinateType::geocentric, x, y, z);
MSP::CCS::UTMCoordinates targetCoordinates;
ccs.convertSourceToTarget( &sourceCoordinates, &sourceAccuracy, targetCoordinates, targetAccuracy );
zone = targetCoordinates.zone();
hemisphere = targetCoordinates.hemisphere();
easting = targetCoordinates.easting();
northing = targetCoordinates.northing();
}
extern "C"__declspec(dllexport) void __stdcall convertGeocentricToMGRS(const double x, const double y, const double z, char*& mgrsString, Precision::Enum& precision)
{
MSP::CCS::CoordinateSystemParameters geocentricParameters(MSP::CCS::CoordinateType::geocentric);
MSP::CCS::CoordinateSystemParameters mgrsParameters(MSP::CCS::CoordinateType::militaryGridReferenceSystem);
MSP::CCS::CoordinateConversionService ccs( "WGE", &geocentricParameters, "WGE", &mgrsParameters );
MSP::CCS::Accuracy sourceAccuracy;
MSP::CCS::Accuracy targetAccuracy;
MSP::CCS::CartesianCoordinates sourceCoordinates(MSP::CCS::CoordinateType::geocentric, x, y, z);
MSP::CCS::MGRSorUSNGCoordinates targetCoordinates;
ccs.convertSourceToTarget( &sourceCoordinates, &sourceAccuracy, targetCoordinates, targetAccuracy );
mgrsString = targetCoordinates.MGRSString();
precision = targetCoordinates.precision();
}
然后我在我的 C# 类中有以下 p/invoke 调用..
[DllImport("CoordinateConversionWrapper.dll")]
private static extern void convertGeodeticToGeocentric(double lat, double lon, double height, ref double x, ref double y, ref double z);
[DllImport("CoordinateConversionWrapper.dll")]
private static extern void convertGeocentricToMGRS(double x, double y, double z, ref char[] mgrsString, Precision precision);
调用上述任何 p/invoke 方法都会导致 NullReferenceException。看起来问题出在 c++ 代码本身,但不是 c++ 专家我不确定问题是什么......
请帮忙!!
最佳答案
我建议调试您的代码。您可以在 C# 项目的设置(“调试”选项卡)中启用调试非托管代码。
请同时发布调用代码。
备注:p/invoke 的字符串输出参数通常由 StringBuilder 类完成。
[编辑] 字符串输出有问题 - 谁为字符串提供存储空间以及谁释放它(如果需要)?唯一有用的解决方案是将存储(作为 char*)及其长度传递给 C++ 函数。如前所述,在 C# 端使用 StringBuilder。
关于.net - GeoTrans p/调用包装器 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7052126/
我正在尝试公开需要从 GeoTrans c++ 库调用的方法,但遇到了问题。任何帮助都会很棒! 我有以下 c++ 文件,我正在运行 nmake 以将其编译成 dll。 #include #inclu
我是一名优秀的程序员,十分优秀!