DotNetBlog der DotNet-Blog Alles rund um Microsoft .NET – von  Sascha H. Baumann

MCTS Exam 70-536 – COM Interop

1 Star2 Stars3 Stars4 Stars5 Stars (Noch nicht bewertet. Seien Sie der/die Erste !)
Loading ... Loading ...

Hello everyone, big steps this week toward my goal. As usual, I share my notes about this topic with you. Hope it helps you while you are preparing :) Using COM from .NET

  • COM types are defines in a type library (stand-alone .tlb, or embeded in .exe, .ocx, .olb or .dll)
  • Easiest way: “Add reference” in Visual Studio to access the methods just like usual.
  • Use Tlbimp.exe (VS CommandPrompt) to import a Type Library
    • parameter /namespace to change namespace
    • parameter /out to specify filename for new assembly
    • parameter /keyfile for strong names
  • Instead of adding a reference you can use System.Runtime.InteropServies-namespace and the DllImport Attribute
  • With that, you need to declare managed prototypes for your methods that you use to access.
    • Example: [DllImport(“user32.dll”)]
    • followed by a methodheader ended with ; (Just like interfaces, but with an access modifier allowed)
  • .NET uses interop marshalling to pass data between itself and COM
  • You can use the following parameters with DllImport to influence marshalling etc.
    • BestFitMapping (true/false)  – While converting between Unicode (.NET) and ANSI (COM) unknown characters will be replaced with the best fit if enabled. If no mapping works and ThrowOnUnmappableChar is true, there will an exception be thrown.
    • CallingConvention – CallingConvention used when passing arguments. Default (WinAPI) typically works fine.
    • CharSet – Controls String-Parameter-Marshalling. Default is CharSet.Ansi and in most cases is fine.
    • EntryPoint – If your COM-Method has another name as your prototype, with this parameter you specify the underlying COM-Method to call.
    • ExactSpelling – Controls if an EntryPoint should be modified for a specific character set. Default is sufficient in most cases.
    • PreserveSig – if true, HRESULT and retval will automatically translated to Exceptions
    • SetLastError – Enables the caller to use Marshall.GetLastWin32Error
    • ThrowOnUnmappedChar – See BestFitMapping for that.
  • The Marshall-class provides useful static methods.Some Examples:
    • .GenerateGuidForType – returns GUID for type or generates new one
    • .GenerateProgIdForType – returns ProgID for type
    • .GetExceptionForHR / .GetHRForException – Converting between HRESULT and Exception.
    • .ReadBytes, ReadIntPtr,ReadInt64,ReadInt32, ReadInt16 – Reads from unmanaged memory
    • .WriteBytes, WriteIntPtr, WriteInt64, WriteInt32, WriteInt16 – Writes to unmanaged memory
    • .GetLastWin32Error returning an integer
    • .SizeOf returns the bytecount of the class/object after it has been marshaled.
    • .Copy – Copies data between managed array and unmanaged pointer
  • If you pass structures, you may want to layout the structure with the StrucLayout or the FieldOffset Attribute
    • You pass a member of the LayoutKind-Enum to the StructLayout-Attribute
      • LayoutKind.Auto – Give control to CLR incl. Sequence of Fields
      • LayoutKind.Sequential – Specify Sequence, leave Layout to CLR
      • LayoutKind.Explicit – Requires to specify the number of bytes for each field using the FieldOffset-AttributeExamples:

        [StructLayout(LayoutKind.Explicit)]

        public struct mystruct {

        [FieldOffset(0)] public int value1;

        [FieldOffset(4)] public int value2;

        }

        [StructLayout(LayoutKind.Sequential)]

        public struct mystruct {

        public int value1;

        public int value2;

        }

  • If COM-Functions require a callback, this is often indicated by arguments with lp-(long pointer) prefix and –Func suffix.
    • To handle callbacks do the following:
      • Create a method to handle the callback.
      • create delegate for this method (= new CallBack(MyFunction))
      • create a prototype for the function and specify the delegate for callback
      • call function
    • Callback-Function returning anything but 0 indicates the callback to be successful.
  • To Create a Wrapper for COM, do the following:
    • Declare DLL functions within a class
    • Define static methods for each DLL function you need to call

Keep on learning

Sascha

Share and Enjoy:
  • Digg
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • MisterWong.DE
  • Technorati
  • Webnews.de
  • YahooMyWeb

Hinterlasse eine Antwort

CAPTCHA-Bild CAPTCHA Audio
Bild neuladen

Bad Behavior has blocked 54 access attempts in the last 7 days.