本文整理汇总了VB.NET中System.Runtime.InteropServices.Marshal.GetActiveObject方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Marshal.GetActiveObject方法的具体用法?VB.NET Marshal.GetActiveObject怎么用?VB.NET Marshal.GetActiveObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.InteropServices.Marshal
的用法示例。
在下文中一共展示了Marshal.GetActiveObject方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Module1
' 导入命名空间
Imports System.Runtime.InteropServices
Module Module1
Sub Main()
Console.WriteLine(vbcrlf + "Sample: VB System.Runtime.InteropServices.Marshal.GetActiveObject.vb" + vbcrlf)
GetObj(1, "Word.Application")
GetObj(2, "Excel.Application")
End Sub
Sub GetObj(ByVal i As Integer, ByVal progID As [String])
Dim obj As [Object] = Nothing
Console.WriteLine((vbLf & i & ") Object obj = GetActiveObject(""") + progID & """)")
Try
obj = Marshal.GetActiveObject(progID)
Catch e As Exception
Write2Console((vbLf & " Failure: obj did not get initialized" & vbLf & " Exception = ") + e.ToString().Substring(0, 43), 0)
End Try
If obj IsNot Nothing Then
Write2Console(vbLf & " Success: obj = " & obj.ToString(), 1)
End If
End Sub
Sub Write2Console(ByVal s As [String], ByVal color As Integer)
Console.ForegroundColor = If(color = 1, ConsoleColor.Green, ConsoleColor.Red)
Console.WriteLine(s)
Console.ForegroundColor = ConsoleColor.Gray
End Sub
End Module
'Expected Output:
'
'Sample: VB System.Runtime.InteropServices.Marshal.GetActiveObject.vb
'
'1) Object obj = GetActiveObject("Word.Application")
'
' Success: obj = System.__ComObject
'
'2) Object obj = GetActiveObject("Excel.Application")
'
' Failure: obj did not get initialized
' Exception = System.Runtime.InteropServices.COMException
'