本文整理汇总了C#中System.Runtime.InteropServices.Marshal.GetActiveObject方法的典型用法代码示例。如果您正苦于以下问题:C# Marshal.GetActiveObject方法的具体用法?C# Marshal.GetActiveObject怎么用?C# Marshal.GetActiveObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.InteropServices.Marshal
的用法示例。
在下文中一共展示了Marshal.GetActiveObject方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
class MainFunction
{
static void Main()
{
Console.WriteLine("\nSample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs\n");
GetObj(1, "Word.Application");
GetObj(2, "Excel.Application");
}
static void GetObj(int i, String progID)
{
Object obj = null;
Console.WriteLine("\n" +i+") Object obj = GetActiveObject(\"" + progID + "\")");
try
{ obj = Marshal.GetActiveObject(progID); }
catch (Exception e)
{
Write2Console("\n Failure: obj did not get initialized\n" +
" Exception = " +e.ToString().Substring(0,43), 0);
}
if (obj != null)
{ Write2Console("\n Success: obj = " + obj.ToString(), 1 ); }
}
static void Write2Console(String s, int color)
{
Console.ForegroundColor = color == 1? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine(s);
Console.ForegroundColor = ConsoleColor.Gray;
}
}
/*
Expected Output:
Sample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs
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
*/