我想在 VB.Net Compact Framework 项目中调用Garmin API 。API 在 C++ 中,所以我正在制作一个 C# dll 项目作为 API dll 和 VB.NET 之间的中间方式。我在执行代码时遇到了一些问题,因为它NotSupportedException
在调用中抛出了一个(我认为是错误的参数类型)QueCreatePoint
。下面是 C++ API 代码和我的 C# 工作。
C++ 函数原型和 C# P/Invoke 调用:
QueAPIExport QueErrT16 QueCreatePoint( const QuePointType* point, QuePointHandle* handle );
QueAPIExport QueErrT16 QueClosePoint( QuePointHandle point );
[DllImport("QueAPI.dll")]
private static extern QueErrT16 QueCreatePoint(ref QuePointType point, ref uint handle);
[DllImport("QueAPI.dll")]
private static extern QueErrT16 QueRouteToPoint(uint point);
QueErrT16:
typedef uint16 QueErrT16; enum { ... }
public enum QueErrT16 : ushort { ... }
查询点类型:
typedef struct
{
char id[25];
QueSymbolT16 smbl;
QuePositionDataType posn;
} QuePointType;
public struct QuePointType
{
public string id;
public QueSymbolT16 smbl;
public QuePositionDataType posn;
}
阙SymbolT16:
typedef uint16 QueSymbolT16; enum { ... }
public enum QueSymbolT16 : ushort { ... }
QuePositionDataType:
typedef struct
{
sint32 lat;
sint32 lon;
float altMSL;
} QuePositionDataType;
public struct QuePositionDataType
{
public int lat;
public int lon;
public float altMSL;
}
QuePointHandle:
typedef uint32 QuePointHandle;
在 C# 中,我将其作为uint
var 进行管理。
这是我当前调用所有这些的 C# 函数:
public static QueErrT16 GarminNavigateToCoordinates(double latitude , double longitude)
{
QueErrT16 err = new QueErrT16();
// Open API
err = QueAPIOpen();
if(err != QueErrT16.queErrNone)
{
return err;
}
// Create position
QuePositionDataType position = new QuePositionDataType();
position.lat = GradosDecimalesASemicirculos(latitude);
position.lon = GradosDecimalesASemicirculos(longitude);
// Create point
QuePointType point = new QuePointType();
point.posn = position;
// Crete point handle
uint hPoint = new uint();
err = QueCreatePoint(ref point, ref hPoint); // HERE i got a NotSupportedException
if (err == QueErrT16.queErrNone)
{
err = QueRouteToPoint(hPoint);
}
// Close API
QueAPIClose();
return err;
}