- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
MIXING THE MANAGED AND THE NATIVE TYPE SYSTEM in Visual C#.NET
CHAPTER 8 MIXING THE MANAGED AND THE NATIVE TYPE SYSTEM Data Matrix ECC200 Drawer In C#.NET Using Barcode creator for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications. www.OnBarcode.comData Matrix ECC200 Recognizer In C# Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comEND_DELEGATE_MAP() private: void OnChanged(Object^ sender, FileSystemEventArgs^ e) { DumpFile(e->FullPath); } void DumpFile(String^ name) { StreamReader sr(name); Console::WriteLine(sr.ReadToEnd()); } }; int main() { ChangedFileDumper cfd("c:\\tests"); Console::WriteLine("Press enter to stop the application"); Console::ReadLine(); } Paint EAN13 In Visual C# Using Barcode generation for .NET Control to generate, create EAN / UCC - 13 image in .NET framework applications. www.OnBarcode.comMake Matrix In C# Using Barcode maker for .NET Control to generate, create Matrix Barcode image in .NET framework applications. www.OnBarcode.comInternals of the Delegate Map
Data Matrix ECC200 Generation In Visual C# Using Barcode maker for VS .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications. www.OnBarcode.comPainting QR Code In Visual C#.NET Using Barcode drawer for VS .NET Control to generate, create QR Code JIS X 0510 image in .NET applications. www.OnBarcode.comThe preceding code shows the simple steps necessary to handle events in native types. If you extend existing code with .NET features, you will likely use the delegate map quite often. To understand how the delegate map is implemented, it is necessary to see what these macros expand to. This is shown in the following code: //////////////////////////////////////// // Created by "BEGIN_DELEGATE_MAP(ChangedFileDumper)" ref class delegate_proxy_type; delegate_proxy_factory< ChangedFileDumper > m_delegate_map_proxy; ref class delegate_proxy_type { ChangedFileDumper * m_p_native_target; public: delegate_proxy_type(ChangedFileDumper* pNativeTarget) : m_p_native_target(pNativeTarget) {} void detach() { m_p_native_target = 0; } //////////////////////////////////////// // created by "EVENT_DELEGATE_ENTRY(OnCreated, Object^, FileSystemEventArgs^)" Making PDF417 In Visual C#.NET Using Barcode maker for .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications. www.OnBarcode.comEncoding ANSI/AIM Code 93 In Visual C# Using Barcode encoder for .NET Control to generate, create USD-3 image in .NET framework applications. www.OnBarcode.comCHAPTER 8 MIXING THE MANAGED AND THE NATIVE TYPE SYSTEM
DataMatrix Generator In Java Using Barcode creation for Android Control to generate, create Data Matrix ECC200 image in Android applications. www.OnBarcode.comECC200 Generator In None Using Barcode encoder for Excel Control to generate, create Data Matrix image in Excel applications. www.OnBarcode.comvoid OnCreated(Object^ arg0,FileSystemEventArgs^ arg1) { if(m_p_native_target == 0) throw gcnew System::ArgumentNullException( "Delegate call failed: Native sink was not attached or " "has already detached from the managed proxy " "(m_p_native_target == NULL). Hint: see if native sink " "was destructed or not constructed properly"); m_p_native_target->OnCreated(arg0,arg1); } //////////////////////////////////////// // created by "END_DELEGATE_MAP" }; As you can see here, the macros for the delegate map define a complete nested ref class named delegate_proxy_type, including method implementations. To forward method calls to the event handlers in the native class, delegate_proxy_type needs a pointer to the native target object. For this reason, the nested proxy class has a data member to store such a pointer and a constructor for the appropriate initialization. The following code shows those parts of the proxy class that manage the pointer to the native target object: ref class delegate_proxy_type { ChangedFileDumper * m_p_native_target; public: delegate_proxy_type(ChangedFileDumper * pNativeTarget) : m_p_native_target(pNativeTarget) {} void detach() ... } delegate_proxy_type also has a detach function to reset the pointer to the native target. This function will be important for later explanations. The function delegate_proxy_type::OnChanged is used as the delegate target function. This function is added to the proxy class with the EVENT_DELEGATE_ENTRY macro. For every EVENT_DELEGATE_ENTRY in a delegate map, such a target method exists: void OnChanged(Object^ arg0,FileSystemEventArgs^ arg1) { if(m_p_native_target == 0) throw gcnew System::ArgumentNullException( "Delegate call failed: Native sink was not attached or " "has already detached from the managed proxy " "(m_p_native_target == NULL). Hint: see if native sink " { m_p_native_target = 0; } Barcode Generator In .NET Framework Using Barcode generation for Reporting Service Control to generate, create Barcode image in Reporting Service applications. www.OnBarcode.comCode 128 Code Set C Reader In Visual Basic .NET Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comCHAPTER 8 MIXING THE MANAGED AND THE NATIVE TYPE SYSTEM
Code39 Generation In Java Using Barcode encoder for Android Control to generate, create Code-39 image in Android applications. www.OnBarcode.comMatrix Printer In .NET Framework Using Barcode generation for .NET Control to generate, create Matrix image in Visual Studio .NET applications. www.OnBarcode.com"was destructed or not constructed properly"); m_p_native_target->OnChanged(arg0,arg1); } In its implementation, OnChanged checks if a native target pointer has been reset so far, and throws an ArgumentNullException if this is the case. In the case of normal execution, a native target pointer exists, and the call can be forwarded to m_p_native_target->OnChanged. To register the event handler, an instance of the nested ref class delegate_proxy_type must be created. This is done with the MAKE_DELEGATE macro. In the preceding code, this macro is expanded to the following: gcnew FileSystemEventHandler( m_delegate_map_proxy.get_proxy(this), &delegate_proxy_type::OnChanged); As you can see here, the target function passed is in fact the OnChanged method of the nested proxy class. Since this is a non-static method, the first argument of the delegate constructor is the object on which OnChanged should be invoked. To pass this argument, the expression m_delegate_map_proxy.get_proxy(this) is used. m_delegate_map_proxy is a data member of ChangedFileDumper. It is introduced to the native class with the macro BEGIN_DELEGATE_MAP: delegate_proxy_factory< ChangedFileDumper > m_delegate_map_proxy; Paint QR Code JIS X 0510 In Objective-C Using Barcode printer for iPad Control to generate, create QR image in iPad applications. www.OnBarcode.comPainting Code 3 Of 9 In None Using Barcode generator for Office Excel Control to generate, create Code-39 image in Microsoft Excel applications. www.OnBarcode.comm_delegate_map_proxy is of type delegate_proxy_factory < ChangedFileDumper >. delegate_proxy_factory is a template for a native class. Since it is used as a data member of ChangedFileDumper, its destructor will be called from the destructor of ChangedFileDumper. The delegate_proxy_factory destructor calls detach on the proxy class. This is done to ensure that the event is not forwarded after the native class has been destroyed. Code 3 Of 9 Reader In Visual Basic .NET Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comECC200 Recognizer In C# Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comSummary Create UCC-128 In Java Using Barcode creator for Java Control to generate, create USS-128 image in Java applications. www.OnBarcode.comDrawing UPCA In None Using Barcode generator for Font Control to generate, create UPC-A Supplement 5 image in Font applications. www.OnBarcode.comNative code can use native types only. Managed code can use managed types as well as native types. Therefore, only native types can be used to define functions that act as interoperability gateways between native code and managed code. To make native types available for managed code, the C++/CLI compiler automatically generates managed wrapper types for native classes, structs, unions, and enums. Managed types cannot be used in native code because native code is not able to deal with instances that can be relocated. However, to temporarily allow native code to access memory on the managed heap, pinned pointers can be used. You also have to be aware of restrictions when you define new types. Fields of managed classes can only be pointers to native classes. If you want a data member of a native type to refer to a managed object, you have to use the gcroot or auto_gcroot helper templates. While this chapter has discussed how to mix managed and native types, the next chapter will focus on mixing managed and native functions, and on method calls that cross the managed-unmanaged boundary.
|
|