Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a new instance of a specified COM type.
CreateComInstanceFrom(String, String) |
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. |
CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm) |
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. |
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.
public:
System::Runtime::Remoting::ObjectHandle ^ CreateComInstanceFrom(System::String ^ assemblyName, System::String ^ typeName);
public System.Runtime.Remoting.ObjectHandle CreateComInstanceFrom(string assemblyName, string typeName);
member this.CreateComInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
Public Function CreateComInstanceFrom (assemblyName As String, typeName As String) As ObjectHandle
The name of a file containing an assembly that defines the requested type.
The name of the requested type.
An object that is a wrapper for the new instance specified by typeName
. The return value needs to be unwrapped to access the real object.
assemblyName
or typeName
is null
.
The type cannot be loaded.
The operation is attempted on an unloaded application domain.
No public parameterless constructor was found.
assemblyName
is not found.
typeName
is an abstract class.
-or-
This member was invoked with a late-binding mechanism.
The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.
assemblyName
is an empty string ("").
assemblyName
is not a valid assembly.
An assembly or module was loaded twice with two different evidences.
The COM object that is being referred to is null
.
The following sample demonstrates
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
[ComVisible(true)]
public ref class MyComVisibleType
{
public:
MyComVisibleType()
{
Console::WriteLine( "MyComVisibleType instantiated!" );
}
};
[ComVisible(false)]
public ref class MyComNonVisibleType
{
public:
MyComNonVisibleType()
{
Console::WriteLine( "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( "MyComNonVisibleType" ); // Fail!
CreateComInstance( "MyComVisibleType" ); // OK!
}
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 CreateComInstanceFromSnippet {
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);
}
}
}
open System
open System.Runtime.InteropServices
[<ComVisible true>]
type MyComVisibleType() =
do
printfn "MyComVisibleType instantiated!"
[<ComVisible false>]
type MyComNonVisibleType() =
do
printfn "MyComNonVisibleType instantiated!"
let createComInstance typeName =
try
let currentDomain = AppDomain.CurrentDomain
let assemblyName = currentDomain.FriendlyName
currentDomain.CreateComInstanceFrom(assemblyName, typeName)
|> ignore
with e ->
printfn $"{e.Message}"
createComInstance "MyComNonVisibleType" // Fail!
createComInstance "MyComVisibleType" // OK!
Imports System.Reflection
Imports System.Runtime.InteropServices
<ComVisible(True)> _
Class MyComVisibleType
Public Sub New()
Console.WriteLine("MyComVisibleType instantiated!")
End Sub
End Class
<ComVisible(False)> _
Class MyComNonVisibleType
Public Sub New()
Console.WriteLine("MyComNonVisibleType instantiated!")
End Sub
End Class
Module Test
Sub Main()
CreateComInstance("MyComNonVisibleType") ' Fail!
CreateComInstance("MyComVisibleType") ' OK!
End Sub
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
End Module 'Test
Use this method to create objects remotely without having to load the type locally.
The return value must to be unwrapped to access the real object.
A System.Runtime.InteropServices.ComVisibleAttribute attribute with a value of true
must be applied either explicitly or by default to the COM type for this method to create an instance of that type; otherwise, TypeLoadException is thrown.
Product | Versions |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
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.
public:
System::Runtime::Remoting::ObjectHandle ^ CreateComInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName, cli::array <System::Byte> ^ hashValue, System::Configuration::Assemblies::AssemblyHashAlgorithm hashAlgorithm);
public System.Runtime.Remoting.ObjectHandle CreateComInstanceFrom(string assemblyFile, string typeName, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
member this.CreateComInstanceFrom : string * string * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Runtime.Remoting.ObjectHandle
Public Function CreateComInstanceFrom (assemblyFile As String, typeName As String, hashValue As Byte(), hashAlgorithm As AssemblyHashAlgorithm) As ObjectHandle
The name of a file containing an assembly that defines the requested type.
The name of the requested type.
Represents the value of the computed hash code.
Represents the hash algorithm used by the assembly manifest.
An object that is a wrapper for the new instance specified by typeName
. The return value needs to be unwrapped to access the real object.
assemblyFile
or typeName
is null
.
The type cannot be loaded.
The operation is attempted on an unloaded application domain.
No public parameterless constructor was found.
assemblyFile
is not found.
typeName
is an abstract class.
-or-
This member was invoked with a late-binding mechanism.
The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.
assemblyFile
is the empty string ("").
assemblyFile
is not a valid assembly.
An assembly or module was loaded twice with two different evidences.
The COM object that is being referred to is null
.
Use this method to create objects remotely without having to load the type locally.
The return value must to be unwrapped to access the real object.
A System.Runtime.InteropServices.ComVisibleAttribute attribute with a value of true
must be applied either explicitly or by default to the COM type for this method to create an instance of that type; otherwise, TypeLoadException is thrown.
Product | Versions |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in