外部函数中参数的正确格式
Correct format of parameters in extern function
我正在创建一个 c# 应用程序,用于使用 RiVLIB 库 (http://riegl.com/index.php?id=224) 读取 rxp 数据。我不知道如何正确定义外部函数的属性。
我已经可以调用一些函数,例如 scanifc_get_library_version 和 scanifc_point3dstream_open,但是我无法弄清楚 scanifc_point3dstream_read[= 的参数的正确格式33=]函数。
调用 scanifc_point3dstream_read 后(使用从 scanifc_point3dstream_open 获取的 h3ds)我得到 System.AccessViolationException.
电话看起来像:
IntPtr h3ds = IntPtr.Zero;
int sync_to_pps = 0;
//this works ok -> h3ds gets assigned
int opened = scanifc_point3dstream_open_with_logging("190910_092831.rxp", ref sync_to_pps, "log.txt", ref h3ds);
uint PointCount = 0;
int EndOfFrame = 1;
const uint BLOCK_SIZE = 10;
scanifc_xyz32[] BufferXYZ = new scanifc_xyz32[BLOCK_SIZE];
scanifc_attributes[] BufferMISC = new scanifc_attributes[BLOCK_SIZE];
ulong[] BufferTIME = new ulong[BLOCK_SIZE];
//throws AccessViolationException
int read = scanifc_point3dstream_read(
h3ds, BLOCK_SIZE,
ref BufferXYZ, ref BufferMISC, ref BufferTIME,
ref PointCount, ref EndOfFrame);
[DllImport("scanifc-mt.dll")]
static extern int scanifc_point3dstream_read(
IntPtr h3ds,
uint want,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref scanifc_xyz32[] pxyz32,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref scanifc_attributes[] pattributes,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref ulong[] ptime,
ref uint got,
ref int end_of_frame);
使用的结构:
[StructLayout(LayoutKind.Sequential)]
struct scanifc_xyz32
{
float x;
float y;
float z;
}
[StructLayout(LayoutKind.Sequential)]
struct scanifc_attributes
{
float amplitude;
float reflectance;
ushort deviation;
ushort flags;
float background_radiation;
}
这是库函数头:
SCANIFC_API int
scanifc_point3dstream_read(point3dstream_handle, scanifc_uint32_t,
scanifc_xyz32 *, scanifc_attributes *,
scanifc_time_ns *, scanifc_uint32_t *,
scanifc_bool *);
和类型定义:
typedef IMPLEMENTATION_DEFINED scanifc_uint8_t; // unsigned 8 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int8_t; // signed 8 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uint16_t; // unsigned 16 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int16_t; // signed 16 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uint32_t; // unsigned 32 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int32_t; // signed 32 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uint64_t; // unsigned 64 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int64_t; // signed 64 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uintmax_t; // unsigned largest available bitsize integer
typedef IMPLEMENTATION_DEFINED scanifc_intmax_t; // signed largest available bitsize integer
typedef IMPLEMENTATION_DEFINED scanifc_float32_t; // 32 bit floating point
typedef IMPLEMENTATION_DEFINED scanifc_float64_t; // 64 bit floating point
typedef char* scanifc_sz;
typedef const char* scanifc_csz; // pointer to zero delimited string
typedef scanifc_int32_t scanifc_bool; // pointer to constant zero delimited string
typedef struct scanifc_xyz32_t scanifc_xyz32; // a point in cartesian coordinates
typedef struct scanifc_attributes_t scanifc_attributes; // per point attributes
typedef scanifc_uint64_t scanifc_time_ns;
struct scanifc_xyz32_t {
// public data members
scanifc_float32_t x; // x coordinates
scanifc_float32_t y; // y coordinates
scanifc_float32_t z; // z coordinates
};
struct scanifc_attributes_t {
// public data members
scanifc_float32_t amplitude; // relative amplitude in [dB]
scanifc_float32_t reflectance; // relative reflectance in [dB]
scanifc_uint16_t deviation; // a measure of pulse shape distortion.
scanifc_uint16_t flags; // some bit-mapped attribute values
scanifc_float32_t background_radiation; // background radiation.
};
所以几个小时后,我找到了解决方案...如果有人觉得它有用,我正在 post 寻找它。我在这个 post 中找到了我的答案:
int read = scanifc_point3dstream_read(
h3ds, BLOCK_SIZE,
BufferXYZ, BufferMISC, BufferTIME,
ref PointCount, ref EndOfFrame);
[DllImport("scanifc-mt.dll")]
static extern int scanifc_point3dstream_read(
IntPtr h3ds,
uint want,
[Out] scanifc_xyz32[] pxyz32,
[Out] scanifc_attributes[] pattributes,
[Out] ulong[] ptime,
ref uint got,
ref int end_of_frame);
我正在创建一个 c# 应用程序,用于使用 RiVLIB 库 (http://riegl.com/index.php?id=224) 读取 rxp 数据。我不知道如何正确定义外部函数的属性。
我已经可以调用一些函数,例如 scanifc_get_library_version 和 scanifc_point3dstream_open,但是我无法弄清楚 scanifc_point3dstream_read[= 的参数的正确格式33=]函数。
调用 scanifc_point3dstream_read 后(使用从 scanifc_point3dstream_open 获取的 h3ds)我得到 System.AccessViolationException.
电话看起来像:
IntPtr h3ds = IntPtr.Zero;
int sync_to_pps = 0;
//this works ok -> h3ds gets assigned
int opened = scanifc_point3dstream_open_with_logging("190910_092831.rxp", ref sync_to_pps, "log.txt", ref h3ds);
uint PointCount = 0;
int EndOfFrame = 1;
const uint BLOCK_SIZE = 10;
scanifc_xyz32[] BufferXYZ = new scanifc_xyz32[BLOCK_SIZE];
scanifc_attributes[] BufferMISC = new scanifc_attributes[BLOCK_SIZE];
ulong[] BufferTIME = new ulong[BLOCK_SIZE];
//throws AccessViolationException
int read = scanifc_point3dstream_read(
h3ds, BLOCK_SIZE,
ref BufferXYZ, ref BufferMISC, ref BufferTIME,
ref PointCount, ref EndOfFrame);
[DllImport("scanifc-mt.dll")]
static extern int scanifc_point3dstream_read(
IntPtr h3ds,
uint want,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref scanifc_xyz32[] pxyz32,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref scanifc_attributes[] pattributes,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref ulong[] ptime,
ref uint got,
ref int end_of_frame);
使用的结构:
[StructLayout(LayoutKind.Sequential)]
struct scanifc_xyz32
{
float x;
float y;
float z;
}
[StructLayout(LayoutKind.Sequential)]
struct scanifc_attributes
{
float amplitude;
float reflectance;
ushort deviation;
ushort flags;
float background_radiation;
}
这是库函数头:
SCANIFC_API int
scanifc_point3dstream_read(point3dstream_handle, scanifc_uint32_t,
scanifc_xyz32 *, scanifc_attributes *,
scanifc_time_ns *, scanifc_uint32_t *,
scanifc_bool *);
和类型定义:
typedef IMPLEMENTATION_DEFINED scanifc_uint8_t; // unsigned 8 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int8_t; // signed 8 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uint16_t; // unsigned 16 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int16_t; // signed 16 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uint32_t; // unsigned 32 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int32_t; // signed 32 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uint64_t; // unsigned 64 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_int64_t; // signed 64 bit integer
typedef IMPLEMENTATION_DEFINED scanifc_uintmax_t; // unsigned largest available bitsize integer
typedef IMPLEMENTATION_DEFINED scanifc_intmax_t; // signed largest available bitsize integer
typedef IMPLEMENTATION_DEFINED scanifc_float32_t; // 32 bit floating point
typedef IMPLEMENTATION_DEFINED scanifc_float64_t; // 64 bit floating point
typedef char* scanifc_sz;
typedef const char* scanifc_csz; // pointer to zero delimited string
typedef scanifc_int32_t scanifc_bool; // pointer to constant zero delimited string
typedef struct scanifc_xyz32_t scanifc_xyz32; // a point in cartesian coordinates
typedef struct scanifc_attributes_t scanifc_attributes; // per point attributes
typedef scanifc_uint64_t scanifc_time_ns;
struct scanifc_xyz32_t {
// public data members
scanifc_float32_t x; // x coordinates
scanifc_float32_t y; // y coordinates
scanifc_float32_t z; // z coordinates
};
struct scanifc_attributes_t {
// public data members
scanifc_float32_t amplitude; // relative amplitude in [dB]
scanifc_float32_t reflectance; // relative reflectance in [dB]
scanifc_uint16_t deviation; // a measure of pulse shape distortion.
scanifc_uint16_t flags; // some bit-mapped attribute values
scanifc_float32_t background_radiation; // background radiation.
};
所以几个小时后,我找到了解决方案...如果有人觉得它有用,我正在 post 寻找它。我在这个 post 中找到了我的答案:
int read = scanifc_point3dstream_read(
h3ds, BLOCK_SIZE,
BufferXYZ, BufferMISC, BufferTIME,
ref PointCount, ref EndOfFrame);
[DllImport("scanifc-mt.dll")]
static extern int scanifc_point3dstream_read(
IntPtr h3ds,
uint want,
[Out] scanifc_xyz32[] pxyz32,
[Out] scanifc_attributes[] pattributes,
[Out] ulong[] ptime,
ref uint got,
ref int end_of_frame);