Method Porting

  • Thread starter Thread starter jenya7
  • Start date Start date
J

jenya7

Guest
I'm trying to port API's from C++ to C#


In C++


HRESULT WINAPI BluetoothGATTGetServices(
_In_ HANDLE hDevice,
_In_ USHORT ServicesBufferCount,
_Out_opt_ PBTH_LE_GATT_SERVICE ServicesBuffer,
_Out_ USHORT *ServicesBufferActual,
_In_ ULONG Flags
);

typedef struct _BTH_LE_GATT_SERVICE {
BTH_LE_UUID ServiceUuid;
USHORT AttributeHandle;
} BTH_LE_GATT_SERVICE, *PBTH_LE_GATT_SERVICE;

typedef struct _BTH_LE_UUID {
BOOLEAN IsShortUuid;
USHORT ShortUuid;
GUID LongUuid;
} BTH_LE_UUID, *PBTH_LE_UUID;


In C# I did it this way

[StructLayout(LayoutKind.Sequential)]
struct BTH_LE_UUID
{
public Boolean IsShortUuid;
public ushort ShortUuid;
public Guid LongUuid;
};

[StructLayout(LayoutKind.Sequential)]
struct BTH_LE_GATT_SERVICE
{
public BTH_LE_UUID ServiceUuid;
public ushort AttributeHandle;
};


[DllImport("BluetoothApis.dll", PreserveSig = false)]
static extern long BluetoothGATTGetServices(
IntPtr hDevice,
ushort ServicesBufferCount,
out BTH_LE_GATT_SERVICE ServicesBuffer,
ref ushort ServicesBufferActual,
ulong Flags
);


When I call it

ushortserviceBufferCount=0;

BTH_LE_GATT_SERVICE le_gatt_service = new BTH_LE_GATT_SERVICE();

long result = BluetoothGATTGetServices(hLEDevice, 0, out le_gatt_service, ref serviceBufferCount, BLUETOOTH_GATT_FLAG_NONE);


I get a message

A call to PInvoke function 'BLE_Sniffer!BLE_Sniffer.BLE::BluetoothGATTGetServices' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.


What's wrong with my code?

Continue reading...
 
Back
Top