VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.net调用Com组件方法

第一种方法


  •  
    Dim obj As IAnSoft = CType(Activator.CreateInstance(oType), IAnSoft)
  •  
    Dim bk As IBasePlugInterFace = obj.get_BankObject()
  •  
    Console.WriteLine("<{0}><{1}>", obj.Ver(), bk.Ver())
 System.Runtime.InteropServices
  •  
     
  •  
    '显示的指定GUID
  •  
    '这个方法有个缺陷,就是接口方法或者类成员方法都要按com的接口顺序排列,否则会产生异常
  •  
    <Guid("22ec302d-081e-4241-901f-fa86bcb7f536"),
  •  
    InterfaceType(ComInterfaceType.InterfaceIsDual)>
  •  
    Public Interface IAnSoft
  •  
     
  •  
    end Interface
  •  
     
  •  
     
  •  
     
  •  
    <ComImport, Guid("D27CF6C9-FEA8-34F6-B444-8E6F41BF0CA8")>
  •  
    public class AnSoft
  •  
     
  •  
    end class

第二种方法


  •  
          Dim objActiveX As Object = Activator.CreateInstance(oType)
  •  
     
  •  
     
  •  
    ''' <summary>
  •  
    ''' 动态执行Com组件内的方法(无参)
  •  
    ''' </summary>
  •  
    ''' <param name="name">方法名</param>
  •  
    ''' <returns></returns>
  •  
    ''' <remarks></remarks>
  •  
    Public Overridable Function InvokeMember(ByVal name As String) As Object Implements IBaseActiveXLogic.InvokeMember
  •  
    Dim retvalue As Object = Nothing
  •  
    If String.IsNullOrEmpty(name) Then
  •  
    Return retvalue
  •  
    End If
  •  
    If objActiveX IsNot Nothing Then
  •  
    retvalue = ComType.InvokeMember(name, BindingFlags.Default Or BindingFlags.InvokeMethod Or BindingFlags.IgnoreCase, Nothing, objActiveX, Nothing)
  •  
    End If
  •  
    Return retvalue
  •  
    End Function
  •  
     
  •  
    ''' <summary>
  •  
    ''' 动态执行Com组件内的方法(有参)
  •  
    ''' </summary>
  •  
    ''' <param name="memberName">方法名</param>
  •  
    ''' <param name="args">参数列表</param>
  •  
    ''' <returns></returns>
  •  
    ''' <remarks></remarks>
  •  
    Public Overridable Function InvokeMember(ByVal memberName As String, ParamArray args() As Object) As Object Implements IBaseActiveXLogic.InvokeMember
  •  
    Dim retvalue As Object = Nothing
  •  
    If ComType IsNot Nothing AndAlso objActiveX IsNot Nothing Then
  •  
    If args.Length = 0 Then 'OrElse Len(args(0)) = 0
  •  
    AppMessage.MessagePrompt(String.Format(AppMessage.MSG0025, memberName), MethodBase.GetCurrentMethod())
  •  
    Return retvalue
  •  
    End If
  •  
    If String.IsNullOrEmpty(memberName) Then
  •  
    Return retvalue
  •  
    End If
  •  
    retvalue = ComType.InvokeMember(memberName, BindingFlags.Default Or BindingFlags.InvokeMethod Or BindingFlags.IgnoreCase, Nothing, objActiveX, args)
  •  
    End If
  •  
    Return retvalue
  •  
    End Function
  •  
     
  •  
    ''' <summary>
  •  
    ''' 动态执行方法
  •  
    ''' </summary>
  •  
    ''' <param name="memberName">方法名</param>
  •  
    ''' <param name="args">参数列表</param>
  •  
    ''' <param name="nItem">需要返回的参数下标</param>
  •  
    ''' <returns></returns>
  •  
    ''' <remarks></remarks>
  •  
    Public Overridable Function InvokeMember(memberName As String, <Out> ByRef args() As Object, nItem() As Integer) As Object Implements IBaseActiveXLogic.InvokeMember
  •  
    Dim retvalue As Object = Nothing
  •  
    If String.IsNullOrEmpty(memberName) Then
  •  
    Return retvalue
  •  
    End If
  •  
    If ComType IsNot Nothing AndAlso objActiveX IsNot Nothing Then
  •  
    Dim modifers(0) As ParameterModifier
  •  
    modifers(0) = New ParameterModifier(args.Length)
  •  
     
  •  
    For i As Integer = 0 To UBound(nItem)
  •  
    If i < args.Length Then
  •  
    modifers(0)(nItem(i)) = True
  •  
    End If
  •  
    Next
  •  
    If isBindTest Then
  •  
    GenerateCall(memberName, args, nItem)
  •  
    End If
  •  
    ''指定第二,三个参数需要返回值 True 需要返回值,False 不需要
  •  
    retvalue = ComType.InvokeMember(memberName, BindingFlags.InvokeMethod Or BindingFlags.Instance, Nothing, objActiveX, args, modifers, Nothing, Nothing)
  •  
    End If
  •  
     
  •  
    Return retvalue
  •  
    End Function
 

第三种方法就是引用的,这里就不多做介绍了,百度多的是

这个就是做下记录,省得忘了还要到处去找

很遗憾的就是没有找到动态调用com连接点的方式,如果有知道的大神请在下方回复下,谢谢


相关教程