System.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.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.
*) ClassInterfaceType.AutoDispatch - The class only supports late binding. A dispinterface for the class is automatically exposed on request.
*) ClassInterfaceType.AutoDual - An interface is generated and exposed to COM. This is strongly discouraged because of versioning limitations.


InterfaceType attribute

System.Runtime.InteropServices.InterfaceTypeAttribute 

[InterfaceType(COMInterfaceType.InterfaceIsIDispatch)]

This can be applied to interfaces.
Used to indicate how to expose an interface to COM.
There are four choices:
*) COMInterfaceType.InterfaceIsDual -
*) COMInterfaceType.InterfaceIsIDispatch -
*) COMInterfaceType.InterfaceIsIInspectable
*) COMInterfaceType.InterfaceIsIUnknown -


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.


GUID attribute

System.Runtime.InteropServices.GuidAttribute 

[GuidAttribute("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.


DLLImport attribute

System.Runtime.InteropServices.DllImportAttribute 

[DllImport]

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


DLLExport attribute

System.Runtime.InteropServices.DllExportAttribute 

[DllExport]

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



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext