代码之家  ›  专栏  ›  技术社区  ›  MartyIX

如何在C中整理int*

  •  6
  • MartyIX  · 技术社区  · 16 年前

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

    void __stdcall GetConstraints(
    
      unsigned int* puiMaxWidth,
    
      unsigned int* puiMaxHeight,
    
      unsigned int* puiMaxBoxes
    
    );
    

    我的解决方案:

    • 委托定义:

      [非托管函数指针(callingConvention.stdcall)] 私有委托void getconstraintsdel(uintptr puimaxwidth、uintptr puimaxheight、uintptr puimaxbox);

    • 方法的调用:

      // 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);
       }
      

    它可以工作,但我必须允许“不安全”标志。有没有不安全代码的解决方案?或者这个解决方案可以吗?我不太理解用不安全标志设置项目的含义。

    谢谢你的帮助!

    1 回复  |  直到 16 年前
        1
  •  4
  •   Daniel Dolz    16 年前

    刚刚出去吗?

    IE:

    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
      );
    
    推荐文章