Using Assembly.CreateInstance on assembly created with ConvertTypeLibToAssembly

  • Thread starter Thread starter Scott James
  • Start date Start date
S

Scott James

Help please...

I cannot seem to get Assembly.CreateInstance to work on an interop
assembly that was created using ConvertTypeLibToAssembly then loaded.

I'm able to successfully use relection to examine the classes and
methods. I just can't create an instance which makes life really
hard. What am I missing in this whole concept.

I want to allow the user to select a typelib from all available in the
registry, browse the available types and select a type with which to
create an instance.

In the example below, the file argument passed in is XL5EN32.OLB,
which is the Excel 5.0 type library. I keep getting an exception
telling me that "Constructor on type * not found", where * is the type
I'm trying to create an instance of.

<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


Public Function AddComComponent(ByVal name As String, ByVal
file As
String) As Boolean

Dim typeLib As [Object]
LoadTypeLibEx(file, RegKind.RegKind_None, typeLib)

If typeLib Is Nothing Then
Throw New Exception("LoadTypeLibEx failed.")
End If

Dim converter As New TypeLibConverter
Dim eventHandler As New ConversionEventHandler
Dim fi As New FileInfo(file)
Debug.WriteLine(fi.Name)
Dim assemblyName As String = fi.Name & ".interop.dll"
Dim AssemblyPath As String =
Windows.Forms.Application.StartupPath
& "\" & name & ".interop.dll"

Dim asm As AssemblyBuilder =
converter.ConvertTypeLibToAssembly
(typeLib, assemblyName, 0, eventHandler, Nothing,
Nothing,
Nothing, Nothing)

Dim myTypes() As System.Type
myTypes = asm.GetTypes()
Dim myType As System.Type
For Each myType In myTypes
Debug.WriteLine(myType.FullName)
Next

Dim myObj As Object

Try
myObj =
asm.CreateInstance("XL5EN32.OLB.interop.Application")
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Function
 
Scott,
myObj =
asm.CreateInstance("XL5EN32.OLB.interop.Application")

I believe Application is the class interface, try creating an instance
of the class ApplicationClass instead.



Mattias
 
Back
Top