我想在非托管库中调用此方法:
void __stdcall GetConstraints(
unsigned int* puiMaxWidth,
unsigned int* puiMaxHeight,
unsigned int* puiMaxBoxes
);
我的解决方案:
委托定义:
[UnmanagedFunctionPointer(CallingConvention.StdCall)] 私有委托 void GetConstraintsDel(UIntPtr puiMaxWidth, UIntPtr puiMaxHeight, UIntPtr puiMaxBoxes);
方法的调用:
// PLUGIN NAME GetConstraintsDel getConstraints = (GetConstraintsDel)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetConstraintsDel)); uint maxWidth, maxHeight, maxBoxes; unsafe { UIntPtr a = new UIntPtr(&maxWidth); UIntPtr b = new UIntPtr(&maxHeight); UIntPtr c = new UIntPtr(&maxBoxes); getConstraints(a, b, c); }
它有效,但我必须允许“不安全”标志。有没有不安全代码的解决方案?或者这个解决方案可以吗?我不太了解使用 unsafe 标志设置项目的含义。
感谢帮助!