我在将以下几行翻译成 C# 时遇到问题:
extern leveldb_filterpolicy_t* leveldb_filterpolicy_create(
void* state,
void (*destructor)(void*),
char* (*create_filter)(
void*,
const char* const* key_array, const size_t* key_length_array,
int num_keys,
size_t* filter_length),
unsigned char (*key_may_match)(
void*,
const char* key, size_t length,
const char* filter, size_t filter_length),
const char* (*name)(void*));
这个函数是从本机 dll 导出的,我需要从 C# 调用它,我知道 , destructor
, create_filter
,key_may_match
是name
C# 中的代表,但是const char* const*
类型让我偶然发现它在 C# 中的外观,我的主要问题是是否可以编组这些通过 MarshalAs 属性使用标准方法的值,
顺便说一句,我对更简单的构造没有任何问题,例如:
public class Driver
{
static void Validate(string error)
{
if (!String.IsNullOrEmpty(error))
{
throw new ApplicationException(error);
}
}
public static extern leveldb_t leveldb_open(leveldb_options_t options, [param: MarshalAs(UnmanagedType.LPStr)] String name, out string error);
public static extern void leveldb_close(leveldb_t db);
public static extern void leveldb_put(leveldb_t db, leveldb_writeoptions_t options, byte[] key, size_t keylen, byte[] val, size_t vallen, out string error);
public static extern void leveldb_delete(leveldb_t db, leveldb_writeoptions_t options, byte[] key, size_t keylen, out string error);
public static extern void leveldb_write(leveldb_t db, leveldb_writeoptions_t options, leveldb_writebatch_t batch, out string error);
// Returns NULL if not found. A malloc()ed array otherwise. Stores the length of the array in *vallen.
[return: MarshalAs(UnmanagedType.LPArray, SizeParamIndex=4)]
public static extern byte[] leveldb_get(leveldb_t db, leveldb_readoptions_t options, byte[] key, size_t keylen, out size_t vallen, out string error);
public static extern leveldb_iterator_t leveldb_create_iterator(leveldb_t db, leveldb_readoptions_t options);
public static extern leveldb_snapshot_t leveldb_create_snapshot(leveldb_t db);
public static extern void leveldb_release_snapshot(leveldb_t db, leveldb_snapshot_t snapshot);
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern String leveldb_property_value(leveldb_t db, [param: MarshalAs(UnmanagedType.LPStr)] String propname); // Returns NULL if property name is unknown. Else returns a pointer to a malloc()-ed null-terminated value.
...
}