我有一个外部库需要一个IntPtr。是否有任何安全的方法来做到这一点...IntPtr和避免不安全的代码
int BytesWritten = 0;
Output.WriteBytes(buffer, new IntPtr(&BytesWritten));
......不必使用'不安全'的代码?我没那么熟悉IntPtrs,但我喜欢做这样的事情:
fixed int BytesWritten = 0;
Output.WriteBytes(buffer, IntPtr.GetSafeIntPtr(ref BytesWritten));
......在这样的,我不需要用/不安全编译的方式。
我无法更改WriteBytes函数,它是一个外部函数。
似乎应该在'ref int'和IntPtr之间进行某种转换,但是我没有找到它。
“ref int”在运行时产生一个IntPtr。只需更改P/Invoke声明。 – 2010-07-26 19:19:57