.NET and VBA interoperability in MicroStation v8

Introduction
MicroStation v8 brings to developers among others two practical features: First in form of integration of Microsoft’s Visual Basic for Application and second complete possibility to compile MDL code into native DLL and use Visual Studio for debugging. On Bentley newsgroup someone says that the letter “L” in MDL now means Library not Language. Third time lucky, Microsoft has recently released final version of .NET framework and its SDK which defines a new platform for software solutions. This platform we my immediately use for MicroStation development as will be shown in the article. The key feature of each new technology is often backward compatibility or interoperability with the current code. We will focus in this contribution to these features – mutual interoperability among different platforms accessible in MicroStation.

Interoperability overview
Fig 1. shows that the central point of intercommunication is a Dynamic Link Library (DLL) which is capable to interact with different platforms. This could be one of the good reason why to compile MDL code into DLL – just to be in the “centre”. The upper right part of the picture was already discussed in article:”Java/Jmdl communication with MDL application” (MicroStation Manager 12/2001). We will give our attention to the VBA and .NET interoperability.
Fig. 1 In-process interoperability schema.

Designing DLL native code for interoperability
We will go through code which demonstrate interoperability to point out some interesting techniques. The native code handles up coming calls from VBA and .NET and invokes their procedures and methods. The interoperability is designed through direct function pointers, thus make it more simpler for understanding. The dll library is called nativeLib.dll and is referred inVBA and .NET code bellow. The following steps to interact with the DLL are common to VBA and .NET:

1. Calling exported function in any DLL is almost trivial – you only need to declare function as exported and set proper calling convention to stdcall (VBA require this convention)

Of course thee are different functions for VBA, .NET , MDL and Java in nativelib.dll to enable calling between them.

Designing VBA to call DLL code
VBA can invoke any standard DLL function in very simple and straightforward way. This possibility allow to call Window’s API or MDL built in functions (from version 8.00.02). For instance to call MDL function for opening dialog box you need to declare at global section of VBA code imported function from stdmdlbltin.dll, then it is possible to call it:

Designing .NET to call DLL code.NET allows to call native (unmanaged) functions that are implemented in a DLL via Platform Invocation Services (PInvoke) . The called function in DLL need not to be declared as standard :

Designing VBA to accept calls from DLL
DLL is able to call VBA procedures through its pointer (see above). We need to set the proper procedure pointer first with help of AddresOf operator and than call DLL function to store the pointer for later invocation of VBA method :

'Define a function to be called from DLL in VBA module:
Public Sub OnVBACall(ByVal limit As Long)
VBANonModal.Label1 = "VBA got " + Str$(limit)
End Sub
' Declare function from DLL which accept pointer to the VBA method:
Private Declare Function dll_setVBAMethodPointer_
Lib "nativeLib.dll" (ByVal pMethod As Any) As Long
' Call the DLL function to set pointer on method OnVBACall
Private Sub UserForm_Activate()
Call dll_setVBAMethodPointer(AddressOf OnVBACall)
End Sub

Designing .NET to accept calls from DLLThe .NET Framework defines a special type called Delegate that provides the functionality of a type-safe function pointer. We need to design method to be called from DLL, declare and call the native DLL function to set method pointer:

Wrapping .NET into COM
.NET Framework provides COM callable Wrapper (CCW) enabling an arbitrary COM client to seamlessly call a method on a .NET object. .NET object appears to COM clients just as if it were a native COM object. This feature fits perfectly to the VBA concept which provide easy way to call COM methods and manage COM events. CCW makes COM object from .NET just as simple as possible:

Final words
It has been shown how to interoperate among DLL, .NET and VBA code. Moreover you may download complete code for this article to test interoperability. In a code for this article there is actually done much more. There are dialog boxes created in MDL, Java , VBA and .NET which serve as a test for application interaction, see fig. 3. In VBA code there are used some tricks to allow VBA and .NET Forms to be displayed behind ordinary MDL dialog boxes and behave as child window of MicroStation.

Fig. 3 dialog boxes for interaction between MDL, Java, VBA and .NET

We can use all good features of particular language for our solution. Most promising is to use .NET together with MDL compiled into native code. Moving MDL code into native DLL would save one step in interaction between different platforms and MDL.. NET platform is no doubt ready for MicroStation software solution. We may use it even instead of VBA with possibilities to write the application in C#, VB.NET, VC.NET or other. These examples were compiled using Java 2 SE, VBA v.6, VC++.NET 7 , C#.NET 7, and MicroStation V8 (08.00.02.20) compilers runing on Windows XP Pro.

The programming code in this article is offered to the MicroStation community „as is“, without any responsibility for its use elsewhere. http://www.sumbera.com