Omnis Technical Note TNEX0001 April 2007

Using .Net objects in Omnis Studio

Omnis Studio 3 or after
By Klaus Schrödl

Prerequisites:
.Net Framework 2.0 installed
Visual Studio .Net 2005

Do you think Omnis and Microsoft .Net are two different and even opposing architectures?

This is correct but it doesn't mean that Omnis and .Net cannot talk to each other. In fact they can, and with little effort you will be able to integrate .Net objects - classes and even visual components like windows and dialogs within Omnis Studio.

This can be done really seamlessly, so that no one will notice that you are using .Net.

A big advantage of this possibility is that the complete .Net framework with its huge amount of functions and classes now is open for you to use. i. e. now it's easy to access the .Net functions for accessing Active Directory or LDAP.

So how does it work and what's the trick?

Although the .Net technology was supposed to be the successor of Microsoft COM technology and an escape from the well known "DLL hell", at Microsoft they couldn't ignore that COM is still present all over. All Microsoft applications like Word, Excel etc. are COM apps. Also the operating systems - including Vista - are based on Win32, COM and COM+ technology even if they have the phrase .Net in their name, like it was planned for .Net Server which now is known as Windows 2003 Server.

In consequence, to be successful, .Net had to provide interfaces both for Win32 and COM. And this can be utilized by Omnis developers since Omnis itself can make use of COM objects.

You just have to add some extra properties and methods to VB.Net or C# projects, to make them available for Omnis.

First of all, the projects have to be of the type "Class Library". Then, in the Compile Options, you have to check the "Register for COM Interop" option. To allow correct registering the libraries also have to have a certain piece of code inside the class that shall be visible in COM:

<ComClass(Api.ClassId, Api.InterfaceId, Api.EventsId)> _Public Class Api

#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
' .Net contains a tool for creating unique GUIDs
Public Const ClassId As String = "6A7FD308-6FF6-4FE3-A536-813C1400C439"
Public Const InterfaceId As String = "05073E2E-2EDC-49B3-BC50-D075FCC33012"
Public Const EventsId As String = "9CA06C4C-D86C-47F7-B8D6-8D280129DF1A"
#End Region

' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
 Public Sub New()
  MyBase.New()
 End Sub
End Class

So, that's it! Now all functions and properties inside this class which are declared as Public can be used from within Omnis Studio. And this class also can encapsulate other classes containing GUI elements like the example.

When using Visual Studio .Net, the class library is registered automatically in COM after compiling successfully. If you don't have Visual Studio .Net installed and want to test the .Net DLL together with Omnis you have to register the .Net DLL in COM. But you have to use a different tool instead of regsvr32 which is responsible for "real" COM objects created with C++ or VB6. The tool you have to use is regasm.exe which is responsible for registering .Net assemblies in COM. It resides in

%WINDIR%\Microsoft.NET\Framework\v2.0.50727

The tool is called with

regasm.exe /codebase <Drive>:\Path\To\TestLib.dll

This will

a) extract the interface definition and create a Type Library
file with the suffix .tlb and
b) register the DLL resp. the Type Lib

Also, the Framework directory can be added to your System path variable. After that the functions inside TestLib are available within Omnis.

If you don't have Visual Studio installed, you find the DLL and TLB ready for use in the /bin directory after unzipping the VS project file (ApiLibNet.zip).

Download files: ApiLibNet.zip (75kb)     dotnetinomnis.lbs (30kb)