Create COM component via late binding

J

jwang

I am trying to instantiate a COM component in C# via late binding. The
component appears to be launched, then it automatically gets unloaded. That
is, the instance is unloaded after the CreateInstance returns.

Any idea of what could be the problem? Thanks a lot.

Here is the code segment I am using:

Object target;

Type oType = Type.GetTypeFromProgID("my.application", true);
if (oType != null)

{

target = Activator.CreateInstance(oType);

if (target != null)

{

success = true;

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

jwang,

Either CreateInstance should throw, or return a value. What is the
exception that is being thrown? I doubt it is NullReferenceException.
 
J

jwang

Nicholas,
Thanks for responding to my question.

CreateInstance does not throw an exception, and it returns a
System._ComObject. The COM component I try to invoke is a out-of-process
component. From the Task Manager, I can see it was created and then went
away. (I used the same method on Excel.Application, it works fine.)

Also, I try with ComImport to explicitly import the type, and then use new
to instantiate the component, but get the same problem.

jwang


Nicholas Paldino said:
jwang,

Either CreateInstance should throw, or return a value. What is the
exception that is being thrown? I doubt it is NullReferenceException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

jwang said:
I am trying to instantiate a COM component in C# via late binding. The
component appears to be launched, then it automatically gets unloaded.
That is, the instance is unloaded after the CreateInstance returns.

Any idea of what could be the problem? Thanks a lot.

Here is the code segment I am using:

Object target;

Type oType = Type.GetTypeFromProgID("my.application", true);
if (oType != null)

{

target = Activator.CreateInstance(oType);

if (target != null)

{

success = true;

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

jwang,

This does not indicate an issue with .NET, but rather, the COM server
(which is out of process) is being shut down.

If you try and create the object through say, vb script, do you get the
same result?

Also, what do you have the ApartmentState set to on your thread?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

jwang said:
Nicholas,
Thanks for responding to my question.

CreateInstance does not throw an exception, and it returns a
System._ComObject. The COM component I try to invoke is a out-of-process
component. From the Task Manager, I can see it was created and then went
away. (I used the same method on Excel.Application, it works fine.)

Also, I try with ComImport to explicitly import the type, and then use new
to instantiate the component, but get the same problem.

jwang


Nicholas Paldino said:
jwang,

Either CreateInstance should throw, or return a value. What is the
exception that is being thrown? I doubt it is NullReferenceException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

jwang said:
I am trying to instantiate a COM component in C# via late binding. The
component appears to be launched, then it automatically gets unloaded.
That is, the instance is unloaded after the CreateInstance returns.

Any idea of what could be the problem? Thanks a lot.

Here is the code segment I am using:

Object target;

Type oType = Type.GetTypeFromProgID("my.application", true);
if (oType != null)

{

target = Activator.CreateInstance(oType);

if (target != null)

{

success = true;

}

}
 
P

Phil Wilson

Is it possible that the managed object is being garbage collected and
releasing references? Then the COM server just goes away.
--
Phil Wilson
[Microsoft MVP-Windows Installer]
Nicholas Paldino said:
jwang,

This does not indicate an issue with .NET, but rather, the COM server
(which is out of process) is being shut down.

If you try and create the object through say, vb script, do you get the
same result?

Also, what do you have the ApartmentState set to on your thread?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

jwang said:
Nicholas,
Thanks for responding to my question.

CreateInstance does not throw an exception, and it returns a
System._ComObject. The COM component I try to invoke is a out-of-process
component. From the Task Manager, I can see it was created and then went
away. (I used the same method on Excel.Application, it works fine.)

Also, I try with ComImport to explicitly import the type, and then use
new to instantiate the component, but get the same problem.

jwang


Nicholas Paldino said:
jwang,

Either CreateInstance should throw, or return a value. What is the
exception that is being thrown? I doubt it is NullReferenceException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to instantiate a COM component in C# via late binding. The
component appears to be launched, then it automatically gets unloaded.
That is, the instance is unloaded after the CreateInstance returns.

Any idea of what could be the problem? Thanks a lot.

Here is the code segment I am using:

Object target;

Type oType = Type.GetTypeFromProgID("my.application", true);
if (oType != null)

{

target = Activator.CreateInstance(oType);

if (target != null)

{

success = true;

}

}
 
J

jwang

I have been calling the application in VB6 suing CreateObject(). It always
works as expected.

I do not explicitly set the thread. Is that something I should do? I just
attempt to write a C# class that invokes that COM server.

It seems that the reference to the COM server is lost when CreateInstnace
returns. Just do not know what has caused that.

Thanks.
jwang





Phil Wilson said:
Is it possible that the managed object is being garbage collected and
releasing references? Then the COM server just goes away.
--
Phil Wilson
[Microsoft MVP-Windows Installer]
Nicholas Paldino said:
jwang,

This does not indicate an issue with .NET, but rather, the COM server
(which is out of process) is being shut down.

If you try and create the object through say, vb script, do you get
the same result?

Also, what do you have the ApartmentState set to on your thread?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

jwang said:
Nicholas,
Thanks for responding to my question.

CreateInstance does not throw an exception, and it returns a
System._ComObject. The COM component I try to invoke is a
out-of-process component. From the Task Manager, I can see it was
created and then went away. (I used the same method on
Excel.Application, it works fine.)

Also, I try with ComImport to explicitly import the type, and then use
new to instantiate the component, but get the same problem.

jwang


in message jwang,

Either CreateInstance should throw, or return a value. What is the
exception that is being thrown? I doubt it is NullReferenceException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to instantiate a COM component in C# via late binding. The
component appears to be launched, then it automatically gets unloaded.
That is, the instance is unloaded after the CreateInstance returns.

Any idea of what could be the problem? Thanks a lot.

Here is the code segment I am using:

Object target;

Type oType = Type.GetTypeFromProgID("my.application", true);
if (oType != null)

{

target = Activator.CreateInstance(oType);

if (target != null)

{

success = true;

}

}
 
W

Willy Denoyette [MVP]

What kind of client are you talking about? Please post a simple client that
illustrates the issue.
If the COM out-proc server ends it's because it stops running for a reason
known by the server not because of the client.
What happens if you create an instance of say Excel or Word from the same
client?

Willy.




|I have been calling the application in VB6 suing CreateObject(). It always
| works as expected.
|
| I do not explicitly set the thread. Is that something I should do? I
just
| attempt to write a C# class that invokes that COM server.
|
| It seems that the reference to the COM server is lost when CreateInstnace
| returns. Just do not know what has caused that.
|
| Thanks.
| jwang
|
|
|
|
|
| | > Is it possible that the managed object is being garbage collected and
| > releasing references? Then the COM server just goes away.
| > --
| > Phil Wilson
| > [Microsoft MVP-Windows Installer]
| > in message | >> jwang,
| >>
| >> This does not indicate an issue with .NET, but rather, the COM
server
| >> (which is out of process) is being shut down.
| >>
| >> If you try and create the object through say, vb script, do you get
| >> the same result?
| >>
| >> Also, what do you have the ApartmentState set to on your thread?
| >>
| >>
| >> --
| >> - Nicholas Paldino [.NET/C# MVP]
| >> - (e-mail address removed)
| >>
| >> | >>> Nicholas,
| >>> Thanks for responding to my question.
| >>>
| >>> CreateInstance does not throw an exception, and it returns a
| >>> System._ComObject. The COM component I try to invoke is a
| >>> out-of-process component. From the Task Manager, I can see it was
| >>> created and then went away. (I used the same method on
| >>> Excel.Application, it works fine.)
| >>>
| >>> Also, I try with ComImport to explicitly import the type, and then use
| >>> new to instantiate the component, but get the same problem.
| >>>
| >>> jwang
| >>>
| >>>
| >>> "Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
| >>> in message | >>>> jwang,
| >>>>
| >>>> Either CreateInstance should throw, or return a value. What is
the
| >>>> exception that is being thrown? I doubt it is
NullReferenceException.
| >>>>
| >>>>
| >>>> --
| >>>> - Nicholas Paldino [.NET/C# MVP]
| >>>> - (e-mail address removed)
| >>>>
| >>>> | >>>>>I am trying to instantiate a COM component in C# via late binding.
The
| >>>>>component appears to be launched, then it automatically gets
unloaded.
| >>>>>That is, the instance is unloaded after the CreateInstance returns.
| >>>>>
| >>>>> Any idea of what could be the problem? Thanks a lot.
| >>>>>
| >>>>> Here is the code segment I am using:
| >>>>>
| >>>>> Object target;
| >>>>>
| >>>>> Type oType = Type.GetTypeFromProgID("my.application", true);
| >>>>> if (oType != null)
| >>>>>
| >>>>> {
| >>>>>
| >>>>> target = Activator.CreateInstance(oType);
| >>>>>
| >>>>> if (target != null)
| >>>>>
| >>>>> {
| >>>>>
| >>>>> success = true;
| >>>>>
| >>>>> }
| >>>>>
| >>>>> }
| >>>>>
| >>>>>
| >>>>
| >>>>
| >>>
| >>>
| >>
| >>
| >
| >
|
|
 
J

jwang

The client is a simple C# class that invokes the COM server. The code
segment I included in my initial post is pretty much what I have now. I
need to get a valid reference to the COM server instance before I could do
anything more.

Yes, I use the same method to invoke Excel.Application or Word.Application.
Both work as expected.

Is there any tool that can be used to trace what is happening in the COM
interop layer?

Thanks.
jwang


Willy Denoyette said:
What kind of client are you talking about? Please post a simple client
that
illustrates the issue.
If the COM out-proc server ends it's because it stops running for a reason
known by the server not because of the client.
What happens if you create an instance of say Excel or Word from the same
client?

Willy.




|I have been calling the application in VB6 suing CreateObject(). It
always
| works as expected.
|
| I do not explicitly set the thread. Is that something I should do? I
just
| attempt to write a C# class that invokes that COM server.
|
| It seems that the reference to the COM server is lost when
CreateInstnace
| returns. Just do not know what has caused that.
|
| Thanks.
| jwang
|
|
|
|
|
| | > Is it possible that the managed object is being garbage collected and
| > releasing references? Then the COM server just goes away.
| > --
| > Phil Wilson
| > [Microsoft MVP-Windows Installer]
| > "Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
| > in message | >> jwang,
| >>
| >> This does not indicate an issue with .NET, but rather, the COM
server
| >> (which is out of process) is being shut down.
| >>
| >> If you try and create the object through say, vb script, do you
get
| >> the same result?
| >>
| >> Also, what do you have the ApartmentState set to on your thread?
| >>
| >>
| >> --
| >> - Nicholas Paldino [.NET/C# MVP]
| >> - (e-mail address removed)
| >>
| >> | >>> Nicholas,
| >>> Thanks for responding to my question.
| >>>
| >>> CreateInstance does not throw an exception, and it returns a
| >>> System._ComObject. The COM component I try to invoke is a
| >>> out-of-process component. From the Task Manager, I can see it was
| >>> created and then went away. (I used the same method on
| >>> Excel.Application, it works fine.)
| >>>
| >>> Also, I try with ComImport to explicitly import the type, and then
use
| >>> new to instantiate the component, but get the same problem.
| >>>
| >>> jwang
| >>>
| >>>
| >>> "Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
| >>> in message | >>>> jwang,
| >>>>
| >>>> Either CreateInstance should throw, or return a value. What is
the
| >>>> exception that is being thrown? I doubt it is
NullReferenceException.
| >>>>
| >>>>
| >>>> --
| >>>> - Nicholas Paldino [.NET/C# MVP]
| >>>> - (e-mail address removed)
| >>>>
| >>>> | >>>>>I am trying to instantiate a COM component in C# via late binding.
The
| >>>>>component appears to be launched, then it automatically gets
unloaded.
| >>>>>That is, the instance is unloaded after the CreateInstance returns.
| >>>>>
| >>>>> Any idea of what could be the problem? Thanks a lot.
| >>>>>
| >>>>> Here is the code segment I am using:
| >>>>>
| >>>>> Object target;
| >>>>>
| >>>>> Type oType = Type.GetTypeFromProgID("my.application", true);
| >>>>> if (oType != null)
| >>>>>
| >>>>> {
| >>>>>
| >>>>> target = Activator.CreateInstance(oType);
| >>>>>
| >>>>> if (target != null)
| >>>>>
| >>>>> {
| >>>>>
| >>>>> success = true;
| >>>>>
| >>>>> }
| >>>>>
| >>>>> }
| >>>>>
| >>>>>
| >>>>
| >>>>
| >>>
| >>>
| >>
| >>
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top