DllImportAttribute.PreserveSig vs PreserveSigAttribute

DllImportAttribute.PreserveSig -

  • Mainly used in PInvoke Signatures

  • By default, while using DllImportAttribute to p-invoke, the DllImportAttribute.PreserveSig is set to true.

  • If false: Converts returned HRESULTS from unmanaged signature to managed exceptions if they are not S_OK.

  • If true: Does not convert returned HRESULT to exceptions.


PreserveSigAttribute (pseudo custom attribute)–

  • This is mainly used in COM interface definitions in managed code.

  • By default, for COM interface methods, the CLR does HRESULT to exception transformations (opposite behavior to that of pinvoke).

  • If attribute applied: Does not convert returned HRESULT to exceptions.

  • If attribute not applied: Default behavior of HRESULT to exception transformations.

My short-term memory is bad and I tend to forget things as soon as I learn them. So I jotted this down while I was reading Adam Nathan's excellent manuscript on COM interop. Hope you find this tidbit useful as well. 



Comments (0)

Skip to main content