MSDN Magazine - January 2008 - (Page 19) Figure 4 Marshaling Results C# Signature Basic Types int arg out int arg ref int arg Structs MyStruct arg out MyStruct arg ref MyStruct arg Strings string arg out string arg ref string arg Classes MyClass arg MyClass *arg MyClass [in] MyClass out MyClass arg MyClass **arg [out] MyClass & [out] MyClass & char *arg char **arg char **arg string [out] string & string & [in] string [out] string & [in, out] string & MyStruct arg MyStruct *arg MyStruct *arg MyStruct [out] MyStruct & MyStruct & [in] MyStruct [out] MyStruct & [in, out] MyStruct & int arg int *arg int *arg int [out] int & int & [in] int [out] int & [in, out] int & Unmanaged Signature MSIL Signature Actual MSIL Signature Seen by the CLR PassPointerToComplexStructure( [In]ref ComplexStructure pStructure); The signature above will override the default directional behavior of ref, making it [InAttribute]only. In this particular case, if you are doing a P/Invoke, a pointer to Complex Structure (which is a value type) is being passed from the CLR side to the native side, but the callee can’t make any changes visible to the Com plexStructure pointed to by pStruc ture pointer. Figure 6 shows other examples of directional attributes and keyword combinations. So far we have only been discuss ing arguments. What about values returned from functions? The CLR ref MyClass arg MyClass **arg MyClass & [in, out] Myclass & automatically treats a return value as ior. A typical example of this default behavior is the case of an int if it is a normal argument using the [OutAttribute]. Also, the CLR argument; specifying [InAttribute] int arg isn’t necessary. can make the transformation to the function signature, a process There are cases when [OutAttribute] will be ignored. For exam controlled by the PreserveSigAttribute. If [PreserveSigAttribute] ple, [OutAttribute]int doesn’t make any sense, so the [OutAttribute] is set to false when applied to a P/Invoke signature, the CLR will is simply ignored by the CLR. The same is true of [OutAttribute] map HRESULT return values to managed exceptions, and it will string because string is immutable. map [out, retval] parameters to the function’s return value. So the Interface definition (IDL) files also have [in] and [out] attri following managed function signature public static string extern GetString(int id); butes, which can be considered the same as [InAttribute] and [Out would become the unmanaged signature: Attribute] in the CLR. HRESULT GetString([in]int id, [out, retval] char **pszString); Return Values Figure 5 Default Attributes Previously, we’ve shown that C# out and ref keywords can be di rectly mapped to [InAttribute] and [OutAttribute]. As a matter of C# Signature MSIL Signature Default Direction Attributes fact, out and ref can also change what data type the CLR will mar type [InAttribute] shal into or from. Passing data as out or ref is the same as passing out [OutAttribute] type & [OutAttribute] by reference. If you examine the corresponding function signature ref type & [InAttribute, OutAttribute] in intermediate language (IL) using ILDASM, you can see there is an ampersand character (&) next to the type, which means that the argument should be passed by ref Figure 6 More Attributes and Keywords erence. When passing by reference, an extra level Unmanaged IDL Signature MSIL Signature of indirection will be added by the CLR. Figure 4 C# Signature Out shows some examples. Compiler error CS0036. An out N/A Let’s summarize what we discussed for out and [InAttribute] out int arg parameter cannot have the In attribute. ref in the table shown in Figure 5. [OutAttribute] out int arg [out] int *arg [out] int & Please note that when passing by reference, if An N/A no directional attributes are specified, the CLR [InAttribute, OutAttribute] Compiler error CS0036.the out out int arg parameter cannot have In attribute. will apply [InAttribute] and [OutAttribute] au Ref tomatically, which is why there is only “string &” [InAttribute] ref int arg [in] int *arg [in] int & in the Microsoft Intermediate Language (MSIL) [OutAttribute] ref int arg Compiler error CS0662 cannot specify N/A signature in Figure 4. If any of these attributes are only Out attribute on a ref parameter. Use both In and Out attributes, or neither. specified, the CLR will follow them instead of the [InAttribute, OutAttribute] [in, out] int *arg [in] [out] int & default behavior, as shown in this example: public static extern void Keywords Out and Ref and Passing by Reference ref int arg CLR Inside Out january2008 19
Table of Contents Feed for the Digital Edition of MSDN Magazine - January 2008 MSDN Magazine - January 2008 Contents Toolbox CLR Inside Out Data Points Advanced Basics Cutting Edge IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline World Ready - Around the World with ASP.NET AJAX Applications WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 Wicked Code Foundations Extreme ASP.NET {End Bracket} MSDN Magazine - January 2008 MSDN Magazine - January 2008 - Contents (Page Cover1) MSDN Magazine - January 2008 - Contents (Page Cover2) MSDN Magazine - January 2008 - Contents (Page 1) MSDN Magazine - January 2008 - Contents (Page 2) MSDN Magazine - January 2008 - Contents (Page 3) MSDN Magazine - January 2008 - Contents (Page 4) MSDN Magazine - January 2008 - Contents (Page 5) MSDN Magazine - January 2008 - Contents (Page 6) MSDN Magazine - January 2008 - Contents (Page 7) MSDN Magazine - January 2008 - Contents (Page 8) MSDN Magazine - January 2008 - Contents (Page 9) MSDN Magazine - January 2008 - Contents (Page 10) MSDN Magazine - January 2008 - Toolbox (Page 11) MSDN Magazine - January 2008 - Toolbox (Page 12) MSDN Magazine - January 2008 - Toolbox (Page 13) MSDN Magazine - January 2008 - Toolbox (Page 14) MSDN Magazine - January 2008 - Toolbox (Page 15) MSDN Magazine - January 2008 - Toolbox (Page 16) MSDN Magazine - January 2008 - CLR Inside Out (Page 17) MSDN Magazine - January 2008 - CLR Inside Out (Page 18) MSDN Magazine - January 2008 - CLR Inside Out (Page 19) MSDN Magazine - January 2008 - CLR Inside Out (Page 20) MSDN Magazine - January 2008 - CLR Inside Out (Page 21) MSDN Magazine - January 2008 - CLR Inside Out (Page 22) MSDN Magazine - January 2008 - CLR Inside Out (Page 23) MSDN Magazine - January 2008 - CLR Inside Out (Page 24) MSDN Magazine - January 2008 - CLR Inside Out (Page 25) MSDN Magazine - January 2008 - CLR Inside Out (Page 26) MSDN Magazine - January 2008 - Data Points (Page 27) MSDN Magazine - January 2008 - Data Points (Page 28) MSDN Magazine - January 2008 - Data Points (Page 29) MSDN Magazine - January 2008 - Data Points (Page 30) MSDN Magazine - January 2008 - Data Points (Page 31) MSDN Magazine - January 2008 - Data Points (Page 32) MSDN Magazine - January 2008 - Advanced Basics (Page 33) MSDN Magazine - January 2008 - Advanced Basics (Page 34) MSDN Magazine - January 2008 - Advanced Basics (Page 35) MSDN Magazine - January 2008 - Advanced Basics (Page 36) MSDN Magazine - January 2008 - Advanced Basics (Page 37) MSDN Magazine - January 2008 - Advanced Basics (Page 38) MSDN Magazine - January 2008 - Advanced Basics (Page 39) MSDN Magazine - January 2008 - Advanced Basics (Page 40) MSDN Magazine - January 2008 - Advanced Basics (Page 41) MSDN Magazine - January 2008 - Advanced Basics (Page 42) MSDN Magazine - January 2008 - Cutting Edge (Page 43) MSDN Magazine - January 2008 - Cutting Edge (Page 44) MSDN Magazine - January 2008 - Cutting Edge (Page 45) MSDN Magazine - January 2008 - Cutting Edge (Page 46) MSDN Magazine - January 2008 - Cutting Edge (Page 47) MSDN Magazine - January 2008 - Cutting Edge (Page 48) MSDN Magazine - January 2008 - Cutting Edge (Page 49) MSDN Magazine - January 2008 - Cutting Edge (Page 50) MSDN Magazine - January 2008 - Cutting Edge (Page 51) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 52) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 53) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 54) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 55) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 56) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 57) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 58) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 59) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 60) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 61) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 62) MSDN Magazine - January 2008 - IIS 7.0 - Enhance Your Apps with the Integrated ASP.NET Pipeline (Page 63) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 64) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 65) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 66) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 67) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 68) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 69) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 70) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 71) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 72) MSDN Magazine - January 2008 - World Ready - Around the World with ASP.NET AJAX Applications (Page 73) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 74) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 75) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 76) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 77) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 78) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 79) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 80) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 81) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 82) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 83) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 84) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 85) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 86) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 87) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 88) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 89) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 90) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 91) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 92) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 93) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 94) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 95) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 96) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 97) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 98) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 99) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 100) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 101) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 102) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 103) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 104) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 105) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 106) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 107) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 108) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 109) MSDN Magazine - January 2008 - WCF Syndication - HTTP Programming with WCF and the .NET Framework 3.5 (Page 110) MSDN Magazine - January 2008 - Wicked Code (Page 111) MSDN Magazine - January 2008 - Wicked Code (Page 112) MSDN Magazine - January 2008 - Wicked Code (Page 113) MSDN Magazine - January 2008 - Wicked Code (Page 114) MSDN Magazine - January 2008 - Wicked Code (Page 115) MSDN Magazine - January 2008 - Wicked Code (Page 116) MSDN Magazine - January 2008 - Foundations (Page 117) MSDN Magazine - January 2008 - Foundations (Page 118) MSDN Magazine - January 2008 - Foundations (Page 119) MSDN Magazine - January 2008 - Foundations (Page 120) MSDN Magazine - January 2008 - Foundations (Page 121) MSDN Magazine - January 2008 - Foundations (Page 122) MSDN Magazine - January 2008 - Foundations (Page 123) MSDN Magazine - January 2008 - Foundations (Page 124) MSDN Magazine - January 2008 - Foundations (Page 125) MSDN Magazine - January 2008 - Foundations (Page 126) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 127) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 128) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 129) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 130) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 131) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 132) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 133) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 134) MSDN Magazine - January 2008 - Extreme ASP.NET (Page 135) MSDN Magazine - January 2008 - {End Bracket} (Page 136) MSDN Magazine - January 2008 - {End Bracket} (Page Cover3) MSDN Magazine - January 2008 - {End Bracket} (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.