简体   繁体   English

如何将这个不安全的 DLL 调用重构为在 C# 中使用 IntPtr 的调用

[英]How can I refactor this unsafe DLL call into one that uses IntPtr in C#

I am trying to build an application in .NET for the HackRF One using the library which is written in C... https://github.com/mossmann/hackrf/blob/master/host/libhackrf/src/hackrf.c我正在尝试使用用 C 编写的库在 .NET 中为 HackRF One 构建一个应用程序... https://github.com/mossmann/hackrf/blob/master/host/libhackrf/src/hackrf.c

This is the DLL call...这是DLL调用...

[DllImport(dllname)]
public static extern unsafe hackrf_device_list_t* hackrf_device_list();

Which returns a pointer to the following object...它返回一个指向以下对象的指针......

public struct unsafe hackrf_device_list_t
{
    public byte** serial_numbers;
    public hackrf_usb_board_id* usb_board_ids;
    public int* usb_device_index;
    public int devicecount;
    public void** usb_devices;
    public int usb_devicecount;
};

public enum hackrf_usb_board_id
{
    USB_BOARD_ID_JAWBREAKER = 0x604B,
    USB_BOARD_ID_HACKRF_ONE = 0x6089,
    USB_BOARD_ID_RAD1O = 0xCC15,
    USB_BOARD_ID_INVALID = 0xFFFF,
};

I would like to get it into a normal C# managed object.我想把它变成一个普通的 C# 托管对象。 This is how its being used...这是它的使用方式...

unsafe public static hackrf_device_info[] HackrfDeviceList() // Enumerates connected hackrf devices
{
    libhackrf.hackrf_device_list_t* ptr = libhackrf.hackrf_device_list();
    //if (ptr == null) throw new Exception("Null pointer returned");
    if (ptr == null) return new hackrf_device_info[0];
    libhackrf.hackrf_device_list_t devs = *ptr;
    hackrf_device_info[] ret = new hackrf_device_info[devs.devicecount];
    for (int i = 0; i < devs.devicecount; i++)
    {
        hackrf_device_info dev = new hackrf_device_info {
            serial_number = PtrToStr(devs.serial_numbers[i]),
            usb_board_id = (hackrf_board)devs.usb_board_ids[i],
            usb_device_index = devs.usb_device_index[i]
        };
        ret[i] = dev;
    }
    return ret;
}

This is all from a git repo I found... https://github.com/makar853/nethackrf这一切都来自我发现的 git repo... https://github.com/makar853/nethackrf

Try following to see if you get a good size.试试下面的方法,看看你的尺码是否合适。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication169
{
    class Program
    {
        public const string dllname = @"hackrf.dll";
        [StructLayout(LayoutKind.Sequential)]
        public struct hackrf_device_list_t
        {
            public byte[][] serial_numbers;
            public hackrf_usb_board_id usb_board_ids;
            public int usb_device_index;
            public int devicecount;
            public IntPtr usb_devices;
            public int usb_devicecount;
        };
        public enum hackrf_usb_board_id
        {
            USB_BOARD_ID_JAWBREAKER = 0x604B,
            USB_BOARD_ID_HACKRF_ONE = 0x6089,
            USB_BOARD_ID_RAD1O = 0xCC15,
            USB_BOARD_ID_INVALID = 0xFFFF,
        };
        [DllImport(dllname)]
        public static extern IntPtr hackrf_device_list();

        static void Main(string[] args)
        {
            IntPtr ptr = hackrf_device_list();
            int size = Marshal.SizeOf(ptr);

            hackrf_device_list_t hackrf = new hackrf_device_list_t();
            int structuresize = Marshal.SizeOf(hackrf);
        }
    }
  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM