C ++ Garmin API Management in C #

I want to call Garmin API in VB.Net Compact Framework project. The API is in C ++, so I am doing a C # DLL project in an intermediate way between the API dll and VB.NET. I have some trouble executing my code because it throws NotSupportedException

(the type of bad arguments I think) in the call QueCreatePoint

. Below is the C ++ API code and working with C #.

C ++ protocol and C # P / Invoke Calls prototype:

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 { ... }

      

QuePointType:

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

      

QuePointHandle:

typedef uint32 QuePointHandle;

      

In C #, I manage it like uint

var.

And this is my current C # function to call all of this:

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

      

+2


source to share


2 answers


You should be able to use pInvoke directly from VB without a C # wrapper (or C ++ wrapper). Declarations should be something like this:

'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.'

      



I guess there is a reason why C declares QueClosePoint at the beginning and pInvoke declares QueRouteToPoint. This may require some tweaking, depending on the alignment / packing issues and how the various elements are used.

+1


source


you can always create a CPP / CLI project that will port the native API using the managed API. It is usually much easier to write a managed wrapper rather than using DllImprt.



0


source







All Articles