.net - 如何在 C# 中编码(marshal) int*?

标签 .net marshalling intptr

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

void __stdcall GetConstraints(

  unsigned int* puiMaxWidth,

  unsigned int* puiMaxHeight,

  unsigned int* puiMaxBoxes

);

我的解决方案:

  • 委托(delegate)定义:

    [UnmanagedFunctionPointer(CallingConvention.StdCall)] 私有(private)委托(delegate) 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);
     }
    

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

感谢您的帮助!

最佳答案

刚出uint?

即:

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

关于.net - 如何在 C# 中编码(marshal) int*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732975/

相关文章:

java - 如何确定 JAXB 已完成编码到文件

c# - 将指针数组转换为 IntPtr 数组

.net - 我可以依赖 LINQ ToArray() 总是返回一个新实例吗?

.net - 通过 SSLStream 发送数据时的数据包碎片

java - JAXB 编码 XMPP 节

java - 如何在编码时为属性添加前缀

c# - 将数据从 IntPtr 复制到 IntPtr

c# - System.Drawing.Graphics 中的 IntPtr.Zero 是什么意思

c# - 将 MailItem 转换为内存中的 MSG,而不是文件系统中的 MSG

c# - 为什么必须使用 "out"而不是 ref?