gpt4 book ai didi

c# - 在 C# 中管理 C++ Garmin API

转载 作者:太空宇宙 更新时间:2023-11-03 14:35:04 25 4
gpt4 key购买 nike

我想打电话Garmin API在 VB.Net Compact Framework 项目中。 API 在 C++ 中,所以我正在制作一个 C# dll 项目作为 API dll 和 VB.NET 之间的中间方式。我在执行我的代码时遇到了一些问题,因为它在 QueCreatePoint 调用中抛出了一个 NotSupportedException(我认为是错误的参数类型)。下面是 C++ API 代码和我的 C# 工作。

C++ 函数原型(prototype)和 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 { ... }

QuePoint类型:

typedef struct
{
char id[25];
QueSymbolT16 smbl;
QuePositionDataType posn;
} QuePointType;

public struct QuePointType
{
public string id;
public QueSymbolT16 smbl;
public QuePositionDataType posn;
}

QueSymbolT16:

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;
}

队列点句柄:

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;
}

最佳答案

您应该能够在没有 C# 包装器(或 C++ 包装器)的情况下直接从 VB 对它们使用 pInvoke。声明应该是这样的:

'QueAPIExport QueErrT16 QueCreatePoint( const QuePointType* point, QuePointHandle* handle );'

'QueAPIExport QueErrT16 QueClosePoint( QuePointHandle point );'

<DllImport("QueAPI.dll")> _
Private Shared Function QueCreatePoint(ByRef point As QuePointType, ByRef handle As Integer) As QueErrT16
End Function

<DllImport("QueAPI.dll")> _
Private Shared Function QueRouteToPoint(ByVal point As Integer) As QueErrT16
End Function


'-- QueErrT16 ----------'

'typedef uint16 QueErrT16; enum { ... }'

Public Enum QueErrT16 As Short
blah
End Enum


'-- QuePointType ----------'

'typedef struct { char id[25]; QueSymbolT16 smbl; QuePositionDataType posn; } QuePointType;'

'Remeber to initialize the id array.'
Public Structure QuePointType
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=25)> Public id As Byte()
Public smbl As QueSymbolT16
Public posn As QuePositionDataType
End Structure


'-- QueSymbolT16 ----------'

'typedef uint16 QueSymbolT16; enum { ... }'

Public Enum QueSymbolT16 As Short
blahblah
End Enum


'-- QuePositionDataType ----------'

'typedef struct { sint32 lat; sint32 lon; float altMSL; } QuePositionDataType;'

Public Structure QuePositionDataType
Public lat As Integer
Public lon As Integer
Public altMSL As Single
End Structure


'-- QuePointHandle ----------'

'typedef uint32 QuePointHandle;'

'In VB use Integer.'

我假设有一个原因,一开始的C声明是QueClosePoint,而pInvoke声明是QueRouteToPoint。根据对齐/包装问题以及各种项目的使用方式,可能需要对此进行一些调整。

关于c# - 在 C# 中管理 C++ Garmin API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1410016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com