我正在尝试从Xamarin中的Objective-C库绑定以下函数。
/*!
* This function writes data to the device.
*
* @param writeBuffer Pointer to a byte buffer containing the write data.
* @param offset Amount of data that was already written.
* @param size Amount of data left to write.
*
* @return Amount of data written.
*
* @note Throws PortException on failure.
*/
- (u_int32_t)writePort:(u_int8_t const *)writeBuffer :(u_int32_t)offSet :(u_int32_t)size;
我无法确定writeBuffer的数据类型。
[Export ("writePort:::")]
Int32 WritePort (Byte[] writeBuffer, int offSet, int size);
我尝试过Byte [](如上所示)但是我收到以下错误:
/Projects/StarIOBinding/StarIOBinding/obj/Debug/ios/StarIO/SMPort.g.cs(34,34): Error CS1502: The best overloaded method match for `MonoTouch.Foundation.NSArray.FromNSObjects(params MonoTouch.Foundation.NSObject[])' has some invalid arguments (CS1502) (StarIOBinding)
然后我尝试了:
[Export ("writePort:::")]
Int32 WritePort (NSObject[] writeBuffer, int offSet, int size);
然后编译 - 但是在运行命令时,我没有得到预期的结果。我发送了一个包含在NSObject中的Byte []。 NSObject是正确的参数吗?我在哪里错了?
编辑:使用IntPtr时,我仍然没有输出。我可以确认我做得对吗?我的约束力:
[Export ("writePort:::")]
Int32 WritePort (IntPtr writeBuffer, int offSet, int size);
当我打电话时:
IntPtr test = System.Runtime.InteropServices.Marshal.AllocHGlobal(myByteArray.Length);
System.Runtime.InteropServices.Marshal.Copy(myByteArray, 0, test, myByteArray.Length);
SMPort devPort = SMPort.GetPort ("TCP:10.0.1.4", "", 5);
int count = devPort.WritePort (test, 0, myByteArray.Length);
答案 0 :(得分:2)
您需要在绑定中使用IntPtr
并使用Marshal
类来回复制字节。