如何在 C# 中封送 int*?
我想在非托管库中调用此方法:
void __stdcall GetConstraints(
unsigned int* puiMaxWidth,
unsigned int* puiMaxHeight,
unsigned int* puiMaxBoxes
);
我的解决方案:
委托定义:
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void GetConstraintsDel(UIntPtr puiMaxWidth, UIntPtr puiMaxHeight, UIntPtr puiMaxBoxes);
方法的调用:
// 插件名称 GetConstraintsDel getConstraints = (GetConstraintsDel)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetConstraintsDel)); uint maxWidth, maxHeight, maxBoxes; 不安全 { UIntPtr a = new UIntPtr(&maxWidth); UIntPtr b = new UIntPtr(&maxHeight); UIntPtr c = new UIntPtr(&maxBoxes); getConstraints(a, b, c); }
它有效,但我必须允许“不安全”标志。有没有不安全代码的解决方案?或者这个解决方案可以吗?我不太明白使用不安全标志设置项目的含义。
感谢您的帮助!
I would like to call this method in unmanaged library:
void __stdcall GetConstraints(
unsigned int* puiMaxWidth,
unsigned int* puiMaxHeight,
unsigned int* puiMaxBoxes
);
My solution:
Delegate definition:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void GetConstraintsDel(UIntPtr puiMaxWidth, UIntPtr puiMaxHeight, UIntPtr puiMaxBoxes);The call of the method:
// 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); }
It works but I have to allow "unsafe" flag. Is there a solution without unsafe code? Or is this solution ok? I don't quite understand the implications of setting the project with unsafe flag.
Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚出单位?
即:
适用于:
示例使用;
Just out uint?
ie:
works fine with:
Sample use;