Creating Interop dll files on runtime VB.net

Samarinder Singh

Creating Interop dll files on runtime VB.net

Hi,

I would like to know if someone has done this before. The problem is we are running 2 versions of Catia on different machines. And it is hard to maintain the code for different versions. So one solution is to check these dll files if they exist or not in the root folder of the application if not then extract them on the fly from the tlb files.

Regards,
Samarinder

Little Cthulhu

RE: Creating Interop dll files on runtime VB.net
(in response to Samarinder Singh)

Hey. 

Direct answer is:

Use tlbimp.exe tool (sample code from MSDN):

tlbimp InfTypeLib.tlb /out: INFITF.dll
You'll have to distribute tlbimp though. 
Full answer goes below:
As so as you're talking about dlls I suppose that actually you have exactly the same source code for both CATIA version. So what you want to maintain are just two sets of dlls. 
This changes your original goal a bit, so my first suggestion is set up Visual Studio build environment in such manner so that you have two different build configurations (which may be painful as you'll have to manage project References). Or even just extract literally every .tlb and save interop .dlls in folders for further distribution. 
Totally different approach is based on VBs ability to use late-binded objects fluently. Simply declare all CATIA-related objects as "typeless" instances ("As Object" or "As Variant") and DO NOT add any CATIA COM references. You'll have to be EXTRA caution with such code but no interop dlls will be necessary to run your VB.NET application.

Samarinder Singh

RE: Creating Interop dll files on runtime VB.net
(in response to Little Cthulhu)

Hi LC,

Sorry I could not respond earlier because it took me a while to find the zip file.

Anyway I had already tried the tlbimp and it didn't work for me for some reason.

And I am afraid I can't use late-binding objects as I have roughly over 15K lines of code and I don't want to modify it now. Also I have been searching this for months and found some code here on MSDN. Below is the code I modified but it didn't work. I believe I missed something. But I would like to make it work.

Private Enum RegKind
        RegKind_Default = 0
        RegKind_Register = 1
        RegKind_None = 2
    End Enum 'RegKind

    <DllImport("oleaut32.dll", CharSet:=CharSet.Unicode, PreserveSig:=False)> _
    Private Shared Sub LoadTypeLibEx(ByVal strTypeLibName As [String], ByVal regKind As RegKind, <MarshalAs(UnmanagedType.Interface)> ByRef typeLib As [Object])
    End Sub       

 

Dim typeLib As New Object
        LoadTypeLibEx("C:\Program Files\Dassault Systemes\B22\win_b64\code\bin\PPRTypeLib.tlb", RegKind.RegKind_None, typeLib)
        'returnValue = instance.ConvertTypeLibToAssembly(typeLib, asmFileName, flags, notifySink, publicKey, _
        '       keyPair, asmNamespace, asmVersion) 'usage

        If typeLib Is Nothing Then
            Console.WriteLine("LoadTypeLibEx failed.")
            Return
        End If

        Dim converter As New TypeLibConverter()
        Dim eventHandler As New ConversionEventHandler()
        Dim asm As AssemblyBuilder = converter.ConvertTypeLibToAssembly(typeLib, "Interop.PPR.dll", 0, eventHandler, Nothing, Nothing, Nothing, Nothing)

 

Public Class ConversionEventHandler
    Implements ITypeLibImporterNotifySink

    Public Sub ReportEvent(ByVal eventKind As ImporterEventKind, ByVal eventCode As Integer, ByVal eventMsg As String) Implements ITypeLibImporterNotifySink.ReportEvent
        ' handle warning event here...
    End Sub 'ReportEvent

    Public Function ResolveRef(ByVal typeLib As Object) As [Assembly] Implements ITypeLibImporterNotifySink.ResolveRef
        ' resolve reference here and return a correct assembly...
        Return Nothing
    End Function 'ResolveRef
End Class 'ConversionEventHandler

 

Regards,
Samarinder

Little Cthulhu

RE: Creating Interop dll files on runtime VB.net
(in response to Little Cthulhu)

No problem, Samarinder. 

I'll look into your code as soon as I get time.

Little Cthulhu

RE: Creating Interop dll files on runtime VB.net
(in response to Little Cthulhu)

I was able to create dlls with the code you've provided.

I'll try to load assemblies from them to resolve references to CATIA types, specifically to INFITF.Application.

Little Cthulhu

RE: Creating Interop dll files on runtime VB.net
(in response to Little Cthulhu)

The only type libary I was able to convert to .NET assembly was InfTypeLibrary. Other libraries cause null-exception when calling ConvertTypeLibraryToAssembly method. And I believe there's something wrong with them as other (non-CATIA) tlbs get imported successfully. 

From this I conclude that your best way is to use tlbimp.exe. 

UPDATE Below goes a sample code to aid you:


Imports System.Runtime.InteropServices
Imports System.Reflection

Module Module1
 
Sub Main()

Dim asm As Assembly = LoadTypeLib("c:\CATIA\B22\intel_a\code\bin\InfTypeLib.tlb", "INFITF")
Dim tp As System.Type = asm.GetType("INFITF.Application")

CATTest()
 
End Sub
 
Private Function LoadTypeLib(ByVal inputDll As String, ByVal outputAssembly As String) As Assembly
Dim dllPath As String
dllPath = System.IO.Path.Combine( _
New IO.FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, _
"Interop." + outputAssembly + ".dll")
   
' start tlbimp.exe
Const TLBIMP As String = "c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\tlbimp.exe"
 
Dim pr As Process
pr = Process.Start(TLBIMP, """" + inputDll + """ /out:""" + dllPath + """ /silent /namespace:" + outputAssembly)
pr.WaitForExit()
 
' load libary
Return Assembly.LoadFile(dllPath)
 
End Function
 
Private Sub CATTest()
 
MsgBox("CATTest")
   
Dim CATIA As INFITF.Application
CATIA = GetObject([Class]:="CATIA.Application")
 
MsgBox(CATIA.Caption)
 
End Sub
 
End Module


Edited By:
Little Cthulhu[Sikorsky Aircraft] @ Dec 18, 2013 - 11:31 AM (Europe/Moscow)

Samarinder Singh

RE: Creating Interop dll files on runtime VB.net
(in response to Little Cthulhu)

Thanks LC.

Regards,
Samarinder Singh

Lur Aldaraborda

RE: Creating Interop dll files on runtime VB.net
(in response to Samarinder Singh)

Solved

Edited By:
Lur Aldaraborda[Subscriber Members] @ May 25, 2022 - 11:48 AM (Europe/Madrid)