我有一些C代码,想要从C#中使用。大部分的转换已经完成。但是还有几个问题。
其中一部分'C'代码看起来像这样。
typedef struct r_GetCPL {
UInt8 Storage;
UInt8 Key[16]; //(1)
UInt8 *Buff; //(2)
Array16 *CryptoKeyIDs; //(3)
} REPLY_GETCPL;
'C'中定义的别名
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef unsigned long UInt32;
typedef unsigned long long UInt64;
typedef UInt8 Array8[8];
typedef UInt8 Array16[16];
typedef UInt8 Array32[32];
typedef UInt8 Array64[64];
typedef UInt8 Array128[128];
我假设我可以用直接的结构定义来替换typedef。这样可以吗?我定义的等价C#结构体是:
public struct REPLY_GETCPL
{
Byte Storage;
Byte[16] Key; //(1) Is this right?
UInt8 *Buff; //(2) What is the equivalent?
Array16 *CryptoKeyIDs; //(3) What is the equivalent?
}
此外,我在几个导入方法上遇到了困难。
void hex_print(char* data, UInt32 length);
DRMKLVItem *NewDRMKLVItem(UInt32 lenBuff, UInt32 cmdId);
RESULT DRMKLVLengthDecode(const UInt8 *s, UInt32 *pLen);
C#
//I think this one is defined right
[DllImport("DoremiSource.dll")]
public static extern void hex_print([MarshalAs(UnmanagedType.LPStr)]string data, UInt32 length);
//(How to convert the function return type?)
[DllImport("DoremiSource.dll")]
public static extern DRMKLVItem *NewDRMKLVItem(UInt32 lenBuff, UInt32 cmdId); //(4)
//(How do i convert the function parameters here)
[DllImport("DoremiSource.dll", CharSet = CharSet.Ansi)]
public static extern int DRMKLVLengthDecode(const UInt8 *s, ref UInt32 pLen); //(5)
StructLayout(LayoutKind.Explicit)
和成员偏移来注释你的结构体布局。然而,根据这个问题,对于成员指针,你可能会运气不佳。 - DCoder