我在C ++以下几种类型:
typedef void* keychain_handle;
typedef struct {
const char* keyHolderName;
unsigned int numKeys;
key* keys;
} key_holder;
typedef struct {
const char* keyName;
unsigned int keySize;
} key;
我有以下methos:
int createKeyChain(
int id,
key_holder* keyHolders,
keychain_handle* handle);
我需要我的C#创建密钥持有人键,将它发送到C ++代码,并收到一个手柄。
这是我的C#代码:
/* Structs */
[StructLayout(LayoutKind.Sequential)]
public struct Key
{
public string key;
public uint size;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyHolder
{
public string name;
public uint keys;
public IntPtr keys;
}
/* Sync API */
[DllImport("keys.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint createKeyChain(uint id, KeyHolder[] keyHolders, ref IntPtr handle);
Key[] myKeys = new Key[1];
myKeys[0] = new Key { key = "tst", size = 5 };
KeyHolder keyHolder = new DllWrapper.KeyHolder
{
name = "tst123",
items = 1,
keys = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (Key))*myKeys.Length)
};
IntPtr c = new IntPtr(keyHolder.keys.ToInt32());
for (int i = 0; i < myKeys.Length; i++)
{
Marshal.StructureToPtr(myKeys[i], c, false);
c = new IntPtr(c.ToInt32() + Marshal.SizeOf(typeof(Key)));
}
Marshal.StructureToPtr(c, keyHolder.keys, false);
IntPtr handle = IntPtr.Zero;
var ret = createKeyChain(111, new []{keyHolder}, ref handle);
除了重点对象,这已损坏里面的内部串一切运作良好。 我怀疑这是破坏它的StructureToPtr。 我怎样才能让在C ++侧串秀?
谢谢。