如何在 C# 中封送 int*?

发布于 2024-08-31 07:56:08 字数 895 浏览 11 评论 0原文

我想在非托管库中调用此方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-09-07 07:56:08

刚出单位?

即:

HRESULT GetTypeDefProps (
   [in]  mdTypeDef   td,
   [out] LPWSTR      szTypeDef,
   [in]  ULONG       cchTypeDef,
   [out] ULONG       *pchTypeDef,
   [out] DWORD       *pdwTypeDefFlags,
   [out] mdToken     *ptkExtends
);

适用于:

uint GetTypeDefProps
(
  uint td, 
  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)]char[] szTypeDef, 
  uint cchTypeDef, 
  out uint pchTypeDef, 
  out uint pdwTypeDefFlags, 
  out uint ptknds
 );

示例使用;

char[] SzTypeDef;
uint CchTypeDef;
uint PchMember;
IntPtr PpvSigBlob;
uint PbSigBlob;

  SzTypeDef= new char[500];
  CchTypeDef= (uint)SzTypeDef.Length;

ResPT= 
  MetaDataImport.GetTypeDefProps
  (
    td, 
    SzTypeDef, 
    CchTypeDef, 
    out pchTypeDef, 
    out pdwTypeDefFlags,
    out ptkExtends
  );

Just out uint?

ie:

HRESULT GetTypeDefProps (
   [in]  mdTypeDef   td,
   [out] LPWSTR      szTypeDef,
   [in]  ULONG       cchTypeDef,
   [out] ULONG       *pchTypeDef,
   [out] DWORD       *pdwTypeDefFlags,
   [out] mdToken     *ptkExtends
);

works fine with:

uint GetTypeDefProps
(
  uint td, 
  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)]char[] szTypeDef, 
  uint cchTypeDef, 
  out uint pchTypeDef, 
  out uint pdwTypeDefFlags, 
  out uint ptknds
 );

Sample use;

char[] SzTypeDef;
uint CchTypeDef;
uint PchMember;
IntPtr PpvSigBlob;
uint PbSigBlob;

  SzTypeDef= new char[500];
  CchTypeDef= (uint)SzTypeDef.Length;

ResPT= 
  MetaDataImport.GetTypeDefProps
  (
    td, 
    SzTypeDef, 
    CchTypeDef, 
    out pchTypeDef, 
    out pdwTypeDefFlags,
    out ptkExtends
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文