System.Runtime.InteropServices


ClassInterface attribute

System.Runtime.InteropServices.ClassInterfaceAttribute 

[ClassInterface(ClassInterfaceType.None)]
public class MyClass

This can be applied to assemblies or classes.
Specifies the type of class to be generated for a class exposed to COM.
This attribute controls whether the Type Library Exporter (Tlbexp.exe) automatically generates a class interface for the attributed class.
If an interface is generated then it will exposes all public methods, properties, fields and events.
There are three choices:
*) ClassInterfaceType.AutoDispatch - (default) The class only supports late binding. A dispinterface for the class is automatically exposed to COM clients on request. The type library produced by Tlbexp.exe does not contain type information for the dispinterface in order to prevent clients from caching the DISPIDs of the interface. The dispinterface does not exhibit the versioning problems.
*) ClassInterfaceType.AutoDual - Indicates that a dual class interface is automatically generated for the class and exposed to COM. Type information is produced for the class interface and published in the type library. Using AutoDual is strongly discouraged because of the versioning limitations.
*) ClassInterfaceType.None - No class interface is generated for the class. This is the only way to expose functionality through interfaces implemented explicitly by the class. If no interfaces are implemented explicitly, the class can only provide late-bound access through the IDispatch interface. This is the recommended setting and is the only way to expose functionality through interfaces implemented explicitly by the class.


InterfaceType attribute

System.Runtime.InteropServices.InterfaceTypeAttribute 

[InterfaceType(COMInterfaceType.InterfaceIsIDispatch)]
public interface MyInterface

This can be applied to interfaces.
Used to indicate how to expose an interface to COM.
There are four choices:
*) COMInterfaceType.InterfaceIsDual - (default) Indicates the interface is exposed to COM as a dual interface, which enables both early and late binding.
*) COMInterfaceType.InterfaceIsIDispatch - Indicates an interface is exposed to COM as a dispinterface, which enables late binding only.
*) COMInterfaceType.InterfaceIsIInspectable -
*) COMInterfaceType.InterfaceIsIUnknown - Indicates an interface is exposed to COM as an IUnknown - derived interface, which enables only early binding.


ComDefaultInterface attribute

System.Runtime.InteropServices.ComDefaultInterfaceAttribute 

[ComDefaultInterfaceAttribute(typeof(IMyInterface))]
public class MyClass

Specifies a default interface to expose to COM. This class cannot be inherited.


ComVisible attribute

System.Runtime.InteropServices.ComVisibleAttribute 

[ComVisible(true)]
public class MyClass

This can be applied to assemblies, interfaces, classes, structures, delegates, enumerations, fields, methods, or properties.
Used to indicate a public attribute is COM visible.
Only public types can be made visible and this attribute cannot be used to make internal or protected types visible.
Including this attribute on a specific item will override any value in the AssemblyInfo.cs.
Setting the attribute to false on the assembly hides all public types within the assembly. You can selectively make types within the assembly visible by setting the individual types to true.
Setting the attribute to false prevents that type from being exported to a type library; classes are not registered; interfaces will not respond to unmanaged calls.


ComImport attribute

System.Runtime.InteropServices.ComImportAttribute 

[ComImport]
public interface MyInterface

This can be applied to classes or interfaces.
The Type Library Importer (Tlbimp.exe) can apply it for you when it imports a type library.
This is used to indicate that the class (or interface) is defined in an externally implemented COM class.
This can be used to hide interfaces specifically
This allows C# to refer (and use) a COM class.
You only need to use this attribute when generating an interop assembly manually in source code, and want to simulate the metadata produced by Tlbimp.exe.


ComRegisterFunction attribute

System.Runtime.InteropServices.ComRegisterFunctionAttribute 

[ComRegisterFunction]
public static void Method1

This can be applied to methods.
This indicates the method to call when the assembly is registered for COM.


ComUnRegisterFunction attribute

System.Runtime.InteropServices.ComUnRegisterFunctionAttribute 

[ComUnRegisterFunction]
public static void Method2

This can be applied to methods.
This indicates the method to call when the assembly is unregistered for COM.


DefaultParameterValue attribute

System.Runtime.InteropServices.DefaultParameterValueAttribute 

public static void Method2([DefaultParameterValue])

This attribute allows you to specify a default parameter value in a language that does not otherwise support default parameters.


GUID attribute

System.Runtime.InteropServices.GuidAttribute 

[Guid("9ED54F84-A89D-4fcd-A854-44251E925F09")]
public class MyClass

This can be applied to assemblies, interfaces, classes, enumerations, structures, or delegates.
The Type Library Importer (Tlbimp.exe) can apply it for you when it imports a type library.
Only use an explicit GUID when a type must have a specific GUID. If the attribute is omitted, a GUID is assigned automatically.
To avoid conflicts with the type Guid, use the long name GuidAttribute explicitly.


ProgId attribute

System.Runtime.InteropServices.ProgIdAttribute 

[ProgId("Library.Class"]

DLLImport attribute

System.Runtime.InteropServices.DllImportAttribute 

[DllImport("user32.dll")]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, Uint type);

This allows you to call an unmanaged dll (C or C++ code) from within C#.


DLLExport attribute

System.Runtime.InteropServices.DllExportAttribute 

[DllExport]

This allows you to call C# code from within C and C++.


In attribute

System.Runtime.InteropServices.InAttribute 


Optional attribute

System.Runtime.InteropServices.OptionalAttribute 

public static void Method2([Optional])

This attribute allows you to specify an optional parameter value in a language that does not otherwise support optional parameters.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext