This documentation is archived and is not being maintained.

AppDomain.CreateComInstanceFrom Method

Overload List

Creates a new instance of a specified COM type. Parameters specify the name of a file that contains an assembly containing the type and the name of the type.

[Visual Basic] Overloads Public Function CreateComInstanceFrom(String, String) As ObjectHandle
[C#] public ObjectHandle CreateComInstanceFrom(string, string);
[C++] public: ObjectHandle* CreateComInstanceFrom(String*, String*);
[JScript] public function CreateComInstanceFrom(String, String) : ObjectHandle;

Creates a new instance of a specified COM type. Parameters specify the name of a file that contains an assembly containing the type and the name of the type.

[Visual Basic] Overloads Public Function CreateComInstanceFrom(String, String, Byte(), AssemblyHashAlgorithm) As ObjectHandle
[C#] public ObjectHandle CreateComInstanceFrom(string, string, byte[], AssemblyHashAlgorithm);
[C++] public: ObjectHandle* CreateComInstanceFrom(String*, String*, unsigned char __gc[], AssemblyHashAlgorithm);
[JScript] public function CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm) : ObjectHandle;

Example

[Visual Basic, C#, C++] The following sample demonstrates

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of CreateComInstanceFrom. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

<ComVisible(True)> _
Class MyComVisibleType

   Public Sub New()
      Console.WriteLine("MyComVisibleType instantiated!")
   End Sub 'New

End Class 'MyComVisibleType

<ComVisible(False)> _
Class MyComNonVisibleType

   Public Sub New()
      Console.WriteLine("MyComNonVisibleType instantiated!")
   End Sub 'New

End Class 'MyComNonVisibleType

Module Test

   Sub Main()
      CreateComInstance("MyComNonVisibleType")   ' Fail!
      CreateComInstance("MyComVisibleType")      ' OK!
   End Sub 'Main

   Sub CreateComInstance(typeName As String)
      Try
         Dim currentDomain As AppDomain = AppDomain.CurrentDomain
         Dim assemblyName As String = currentDomain.FriendlyName
         currentDomain.CreateComInstanceFrom(assemblyName, typeName)
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'CreateComInstance

End Module 'Test

[C#] 
using System;
using System.Reflection;
using System.Runtime.InteropServices;

[ComVisible(true)]
class MyComVisibleType {
   public MyComVisibleType() {
      Console.WriteLine("MyComVisibleType instantiated!");
   }
}

[ComVisible(false)]
class MyComNonVisibleType {
   public MyComNonVisibleType() {
      Console.WriteLine("MyComNonVisibleType instantiated!");
   }
}

class Test {
   public static void Main() {
      CreateComInstance("MyComNonVisibleType");   // Fail!
      CreateComInstance("MyComVisibleType");      // OK!
   }
   
   static void CreateComInstance(string typeName) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         string assemblyName = currentDomain.FriendlyName;
         currentDomain.CreateComInstanceFrom(assemblyName, typeName);
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

[ComVisible(true)]
public __gc class MyComVisibleType {
public:
   MyComVisibleType() {
      Console::WriteLine(S"MyComVisibleType instantiated!");
   }
};

[ComVisible(false)]
public __gc class MyComNonVisibleType {
public:
   MyComNonVisibleType() {
      Console::WriteLine(S"MyComNonVisibleType instantiated!");
   }
};

void CreateComInstance(String* typeName) {
   try {
      AppDomain*  currentDomain = AppDomain::CurrentDomain;
      String* assemblyName = currentDomain->FriendlyName;
      currentDomain->CreateComInstanceFrom(assemblyName, typeName);
   } catch (Exception* e) {
      Console::WriteLine(e->Message);
   }
}

int main() {
   CreateComInstance(S"MyComNonVisibleType");   // Fail!
   CreateComInstance(S"MyComVisibleType");      // OK!
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

AppDomain Class | AppDomain Members | System Namespace

Show: