none
COM interop for IMessageFilter, Access Violations and interop attributes RRS feed

  • Question

  • I've been trying to implement IMessageFilter in C# and found something that puzzles me. My initial attempt to manually create the interop interface type and use it resulted in lots of access violations.

    I then found an article online with a declaration of COM's IMessageFilter that I guess maybe they generated using tlbimp and it has all these attributes

    [

     

    PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]

    and adding these attributes everywhere means I stop getting Access violations. This gives me a bunch of questions

    1) In IMessageFilter interface, there are HRESULTS everywhere but I don't see anything which is [Out Ref], so I don't undrstand why PreserveSig would make any difference here?

    2) Is the MethodImplOptions.InternalCall important? Why? What does it really mean?

    3) MethodCodeType = Runtime - apparently MethodCodeType is for use only by compiler writers. So why would it matter whether I include it here or not?

    And in summary, what has probably made the difference to whether I am getting access violations or not?

    Thanks,
    Tim

    Friday, September 3, 2010 10:48 PM

Answers

  • Hi,

    Seems we need to get a more clear understanding of the meaning of these attributes. So here are some quick reflections:

    1. For PreserverSig, please take a look at here.

    2. For MethodImplOptions.InternalCall, please take a look at here. Below are some quotation:

    "...The extern modifier in C# informs the C# compiler that a method is implemented externally to the class, though it doesn't tell it where it's implemented. You'll often see this keyword used in conjunction with the DllImport attribute, which informs the runtime that the implementation is actually part of an unmanaged DLL. In contrast, MethodImplOptions.InternalCall is used in conjunction with extern to tell the runtime that the method is implemented internally within the system itself. This is done for many of the core .NET Framework methods that are better served by being implemented in unmanaged code. "

    3. For MethodCodeType=Runtime, please take a look at here.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Thursday, September 16, 2010 7:11 AM
    Tuesday, September 7, 2010 9:20 AM

All replies

  • Hi,

    Seems we need to get a more clear understanding of the meaning of these attributes. So here are some quick reflections:

    1. For PreserverSig, please take a look at here.

    2. For MethodImplOptions.InternalCall, please take a look at here. Below are some quotation:

    "...The extern modifier in C# informs the C# compiler that a method is implemented externally to the class, though it doesn't tell it where it's implemented. You'll often see this keyword used in conjunction with the DllImport attribute, which informs the runtime that the implementation is actually part of an unmanaged DLL. In contrast, MethodImplOptions.InternalCall is used in conjunction with extern to tell the runtime that the method is implemented internally within the system itself. This is done for many of the core .NET Framework methods that are better served by being implemented in unmanaged code. "

    3. For MethodCodeType=Runtime, please take a look at here.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Thursday, September 16, 2010 7:11 AM
    Tuesday, September 7, 2010 9:20 AM
  • We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Please mark the right answer at the right time.
    Thanks,
    Sam
    Thursday, September 16, 2010 7:11 AM