Showing posts with label InterOp. Show all posts
Showing posts with label InterOp. Show all posts

Friday, August 7, 2009

Chapter 13-Lesson 3

  • Platform Invoke (or P/Invoke) is sometimes required when we need to call Windows API or other unmanaged code which does not support .NET directly.
  • System.Runtime.InteropServices namespaces is required. System.Runtime.CompilerServices may also be required.
  • StringBuilder is preferrable over string during a P/Invoke.
  • All that needs to be done is to create a static external method with same name as the name of the function that needs to be called and decorate it with DllImport attribute, specifying the unmanaged dll. (This would only be a function declaration, similar to like we'd be writing prototype for that function)
  • After that whenever we call the function from within our managed code methods, the platform is invoked.
  • If we wish to convert data type from unmanaged to managed code we may us MarshalAs Attribute over a field or a parameter and specify any of the UnmanagedType enum values.
  • If an unmanaged function requires a callback, we may create a delegate specifying the signature that the callback method should possess, and while creating prototype for unmanaged code (one that requires a callback), give it an instance of that delegate. (See code on page 818)
  • This means that we cannot send a method name directly in the callback parameter of an unmanaged function due to Type-Safety.

Thursday, August 6, 2009

Chapter 12-Lesson 2

  • COM Callable Wrapper (CCW) works the exact opposite of RCW. i.e. it makes .NET assemblies usable for COM.
  • We may se it simpy by putting a check on Register for COMInterOp option in Project Properties -> Build tab.
  • To turn on/off COM visibility of individual elements within an assembly use COMVisible attribute and set it to either true or false.
  • Any class or members of a class that need to be available for COM need to be public.
  • Also, the class thats required for COM visibility should use default constructor with no parameters, and should not be abstract as well.
  • After the assembly is built, we may use Type Library Exporter utility (TlbExp.exe) to export it.
  • Interface Definition Language (IDL) may be used to generate resource scripts for COM available .NET libraries.

Chapter 13-Lesson 1

  • Runtime Callable Wrapper (RCW) is the .NET Component that handles all complex issues related to referencing and communicating COM components.
  • Type Library Importer Tool (TlbImp.exe) may be used besides Visual Studio to add reference to a COM component.
  • Regsvr32 maybe used to register any DLLs for use as COM components.
  • null value cannot be passed during a Call By Reference.
  • TlbImp generates a .NET assembly from a COM assembly.
  • Intermediate Language Disassembler (Ildasm.exe) allows to view a visual representation of the Intermediate Language (IL).
  • Use Assembly Registration Tool (Regasm.exe) to Add/Remove .NET assemblies from system registration database.
  • In .NET versions prior to 2.0, System.Exception would handles only Common Language Specification (CLS)-compliant exception. Because COM errors won’t be CLS compliant, they won’t be caught.
  • However, in .NET 2.0 there exists RuntimeWrappedException class in System.Runtime.CompilerServices namespace which inherits from System.Exception class. Therefore, now we may catch all Exceptions (CLS-based and Non-CLS-based) through System.Exception class.
  • See Limitations of COM InterOp on page 794.