Search code examples
c#out-parameters

Parameter in C#


When I want get total value of memory in C# I found a kernel32 function in MSDN to invoke data from system. MSDN declare function this way:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);

but this don't work correctly. I change "ref" to "[In, Out]" then it work correctly. How can tell me what is [In, Out] parameters in C#?


Solution

  • In: http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.inattribute.aspx

    Out: http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.outattribute.aspx

    Short: They control the way data is marshalled. In this case, where you specify both of them, it means that data is marshalled to both sides (caller and callee).