How to put int * in C #?

I would like to call this method in an unmanaged library:

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

My decision:

  • Delegate Definition:

    [UnmanagedFunctionPointer (CallingConvention.StdCall)] private delegate void GetConstraintsDel (UIntPtr puiMaxWidth, UIntPtr puiMaxHeight, UIntPtr puiMaxBoxes);

  • Method call:

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

This works, but I have to enable the "unsafe" flag. Is there a solution without unsafe code? Or is this solution ok? I do not quite understand the consequences of installing a project with an unsafe flag.

Thanks for the help!

+6
marshalling intptr
source share
1 answer

Just from uint?

t

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

works great with:

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

Using an example;

 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 ); 
+4
source share

All Articles