2

我想在 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# 中,我将其作为uintvar 进行管理。

这是我当前调用所有这些的 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; 
}
4

2 回答 2

1

您应该能够在没有 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。根据对齐/包装问题以及各种物品的使用方式,可能需要对此进行一些调整。

于 2009-09-15T15:42:10.743 回答
0

您始终可以创建一个 CPP/CLI 项目,该项目将使用托管 API 包装本机 API。通常以这种方式编写托管包装器要比使用 DllImprt 简单得多。

于 2009-09-11T09:50:29.580 回答