WiiLab Example Code - WiimoteCOM : Basic Class Structure
Annotations
The purpose of the WiimoteCOM class is to expose the WiimoteLib as a COM interop. Exposing the WiimoteLib as a COM interop allows any .NET, COM component, or COM aware application to access the WiimoteLib. Since MATLAB is a COM aware application it will be able to connect to the WiimoteCOM class and access the WiimoteLib functionality through it. The class can be broken down into three main pieces described below: the interface, the class, and the assembly information.
Class Structure
Interface
The interface required for a COM interop is the same as any other interface you would create for a class. The interface for the COM class must contain a method declaration for any method that you want to be able to access. The declaration must include the return type, as well as the number and type of parameters the method takes.
/// <summary>
/// Interface for the COM class/lib
/// </summary>
public interface WiimoteCOMSignature
{
void Method1();
int Method2(int param);
string Method3(int param1, string param2);
}
Class
The only aspect that differentiates the actual COM class from any other generic class is the
[ClassInterface(ClassInterfaceType.AutoDual)]
declaration right before the class.
/// <summary>
/// COM class
/// </summary>
[ClassInterface(ClassInterfaceType.AutoDual)]
public class WiimoteCOMClass : WiimoteCOMSignature
{
public WiimoteCOMClass()
{
//Constructor
}
public void Method1()
{
//Implement Method1 here
}
public int Method2(int param)
{
//Implement Method2 here
return param;
}
public string Method3(int param1, string param2)
{
//implement Method3 here
return param2 + param1;
}
}
Assembly Information
The assembly information (properties) is the only part of the COM class that is largely different from any other class. There are three properties that must be set in order for the COM class to be usable.
- The assembly must be assigned a strong name key.
- The COM visible property must be turned on.
- The assembly must be registered as a COM.
How to set and implement all of these are described in detail in the
Installing the Library section.
--
JordanBrindza - 30 Jun 2008