MSDN Magazine - January 2008 - (Page 23) SysStringAllocByteLen/SysStringFree, since CoGetMalloc is subject to future change. Let’s look at an example. The GetAnsiStringFromNativeCode takes a char ** argument as [in, out] and returns a char * as [out, retval]. For the char ** argument, it can choose to call CoTaskMemFree to free the memory that is allocated by the CLR, then allocate new memory by using CoTaskMemAlloc and overwrite the pointer with a new memory pointer. Later, the CLR will free the memory and create a copy for the managed string. As for the return value, it only needs to allocate a new piece of memory by using CoTaskMemAlloc and return it to the caller. After return, the newly allocated memory is now owned by the CLR. The CLR will first create a new managed string from it and then call CoTaskMemFree to free it. Let’s consider the first option (see Figure 9). The corresponding C# function declaration is as follows: class Lib { [DllImport(@”MarshalLib.dll”, CharSet= CharSet.Ansi)] public static extern string GetAnsiStringFromNativeCode( ref string inOutString); } Figure 9 Using Pointers MARSHALLIB_API char stdcall GetAnsiStringFromNativeCode(char **arg) { char *szRet = (char *)::CoTaskMemAlloc(255); strcpy(szRet, “Returned String From Native Code”); printf(“Inside GetAnsiStringFromNativeCode: *arg = %s\n”, *arg); printf(“Inside GetAnsiStringFromNativeCode: CoTaskMemFree(*arg); *arg = CoTaskMemAlloc(100); strcpy(*arg, \”Changed\”)\n”); ::CoTaskMemFree(*arg); *arg = (char *)::CoTaskMemAlloc(100); strcpy(*arg, “Changed”); } return szRet; Figure 10 Using a Thunk When the following C# code makes a call to GetAnsiStringFromNativeCode string argStr = “Before”; Console.WriteLine(“Before GetAnsiStringFromNativeCode : argStr = \”” + argStr + “\””); string retStr = Lib.GetAnsiStringFromNativeCode(ref argStr); Console.WriteLine(“AnsiStringFromNativeCode() returns \”” + retStr + “\”” ); Console.WriteLine(“After GetAnsiStringFromNativeCode : argStr = \”” + argStr + “\””); In fact, what’s happening is that the CLR creates a thunk, which forwards the calls from native code to the actual delegate, then to the real function (see Figure 10). Usually, you won’t have to worry about the lifetime of delegates. Whenever you are passing a delegate to unmanaged code, the CLR will make sure the delegate is alive during the call. the output is: Before GetAnsiStringFromNativeCode : argStr = “Before” Inside GetAnsiStringFromNativeCode: *arg = Before Inside GetAnsiStringFromNativeCode: CoTaskMemFree(*arg); *arg = CoTaskMemAlloc(100); strcpy(*arg, “Changed”) AnsiStringFromNativeCode() returns “Returned String From Native Code” After GetAnsiStringFromNativeCode : argStr = “Changed” If the native function you are going to call doesn’t follow this convention, you’ll have to do the marshaling by yourself to avoid memory corruption. This could easily happen because the function for an unmanaged function could return whatever it wants; it can return the same piece of memory every time or return a new block of memory allocated by malloc/new, and so forth, again depending on the contract. Besides memory allocation, the size of the memory being passed in or out is also very important. As discussed in the StringBuilder case, it is very important to change the Capacity property so that the CLR can allocate a piece of memory big enough to hold the results. In addition, marshaling a string as [InAttribute, OutAttribute] (without out or ref and any other attribute) is a bad idea because you don’t know whether the string will be big enough. You can use SizeParamIndex and SizeConst fields in MarshalAsAttribute to specify the size of the buffer. However, these attributes cannot be used when passing by reference. Reverse P/Invoke and Delegate Lifetime The CLR allows you to pass a delegate to the unmanaged world so that it can call the delegate as an unmanaged function pointer. CLR Inside Out january2008 23 http://www.windowscontrols.com http://www.windowscontrols.com
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.