Behind the scenes of ServiceController????

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to know if ServiceController is a FULL MANAGE implementation or if is
really a proxy over native api's or COM.


Does anybody knows?


Thanks in advance
 
The service APIs were never COM-based. In any case, most of .NET is a
managed wrapper around some unmanaged API or COM interface.

Why is this important?
 
I wish I can explain my problem!, I'm trying to resolve a conflict problem
with an application that I create to integrate an old C application with .NET:

Old C App --> C++.NET IJW Bridge --> C#

It's a "message" type communication integration, where I pass some
parameters from the Old C App to the C# using a C++.NET as a bridge for
interoperability. The C# process the information and sends back a response.

On my Old C App, I'm receiving the following error message:

OleInitialize failed, rtnval=RPC_E_CHANGED_MODE. A previous call to
CoInitializeEx specified the concurrency model for this apartment as
multithreaded apartment (MTA).


This is happening under very specific circunstances, and I was able to track
down the problem to an specific line of code in my C# class, which is:

ServiceController[] services = ServiceController.GetServices();


The main problem I have is that I'm a C# developer with a little background
of C/C++ and threading, looking at the explanation of the error I'm getting,
looks to me, that some how, the ServiceController is changing the Apartment
Model of the thread that is running, what is interesting to me, is that the
error is being reported by the Old C App which makes me believe that all the
communication between the Old C App -> C++.NET -> C# is running under the
same thread, and that, some how, the ServiceController is changing the
Apartment Model to something different (MTA).

I know that I'm not explaining myself in a way to expect any help, but if
you or somebody have an idea I'll really appreciate it.


Thanks in advance
 
Luis -

Are you setting the apartment model of the active thread in your C# code?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


Luis Fajardo said:
I wish I can explain my problem!, I'm trying to resolve a conflict problem
with an application that I create to integrate an old C application with ..NET:

Old C App --> C++.NET IJW Bridge --> C#

It's a "message" type communication integration, where I pass some
parameters from the Old C App to the C# using a C++.NET as a bridge for
interoperability. The C# process the information and sends back a response.

On my Old C App, I'm receiving the following error message:

OleInitialize failed, rtnval=RPC_E_CHANGED_MODE. A previous call to
CoInitializeEx specified the concurrency model for this apartment as
multithreaded apartment (MTA).


This is happening under very specific circunstances, and I was able to track
down the problem to an specific line of code in my C# class, which is:

ServiceController[] services = ServiceController.GetServices();


The main problem I have is that I'm a C# developer with a little background
of C/C++ and threading, looking at the explanation of the error I'm getting,
looks to me, that some how, the ServiceController is changing the Apartment
Model of the thread that is running, what is interesting to me, is that the
error is being reported by the Old C App which makes me believe that all the
communication between the Old C App -> C++.NET -> C# is running under the
same thread, and that, some how, the ServiceController is changing the
Apartment Model to something different (MTA).

I know that I'm not explaining myself in a way to expect any help, but if
you or somebody have an idea I'll really appreciate it.


Thanks in advance



Klaus H. Probst said:
The service APIs were never COM-based. In any case, most of .NET is a
managed wrapper around some unmanaged API or COM interface.

Why is this important?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


if
is
 
Klaus, thank you very much for helping me on this,

I'm not doing anything with the active thread, however, yesterday, following
the theory that the ServiceController was causing something to my active
thread, I decide to execute the method that is accessing the
ServiceController in a new thread, so in that way, my active thread doesn't
get affected in any way by whatever ServiceController is doing behind de
scenes, and I confirm the theory, the error I was having on my Old C App is
not happening any more. The only thing is that I still don't know why :) ?

Base on the error message I was getting, does that means that my active
thread was being changed to MTA? The thread is initiated on the Old C App
and going through layers up to a point is changed I imagine from STA to MTA.
Does that interpretation of the error have sense?

Any ideas, it will be nice to truly understand the problem, maybe I can stop
doing that workaround to avoid the problem.

Thanks in advance


Klaus H. Probst said:
Luis -

Are you setting the apartment model of the active thread in your C# code?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


Luis Fajardo said:
I wish I can explain my problem!, I'm trying to resolve a conflict problem
with an application that I create to integrate an old C application with ..NET:

Old C App --> C++.NET IJW Bridge --> C#

It's a "message" type communication integration, where I pass some
parameters from the Old C App to the C# using a C++.NET as a bridge for
interoperability. The C# process the information and sends back a response.

On my Old C App, I'm receiving the following error message:

OleInitialize failed, rtnval=RPC_E_CHANGED_MODE. A previous call to
CoInitializeEx specified the concurrency model for this apartment as
multithreaded apartment (MTA).


This is happening under very specific circunstances, and I was able to track
down the problem to an specific line of code in my C# class, which is:

ServiceController[] services = ServiceController.GetServices();


The main problem I have is that I'm a C# developer with a little background
of C/C++ and threading, looking at the explanation of the error I'm getting,
looks to me, that some how, the ServiceController is changing the Apartment
Model of the thread that is running, what is interesting to me, is that the
error is being reported by the Old C App which makes me believe that all the
communication between the Old C App -> C++.NET -> C# is running under the
same thread, and that, some how, the ServiceController is changing the
Apartment Model to something different (MTA).

I know that I'm not explaining myself in a way to expect any help, but if
you or somebody have an idea I'll really appreciate it.


Thanks in advance



Klaus H. Probst said:
The service APIs were never COM-based. In any case, most of .NET is a
managed wrapper around some unmanaged API or COM interface.

Why is this important?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


I want to know if ServiceController is a FULL MANAGE implementation or if
is
really a proxy over native api's or COM.


Does anybody knows?


Thanks in advance
 
Luis,

Well, I'm surprised. It seems then (given your situation here) that either
your C app or your C++ bridge are calling CoInitializeEx() and specifying
MTA as the apartment model, and the CLR's SCM wrapper is STA instead, so
that's why you're getting that exception. Interesting. Technically you do
not need COM at all to interface with the SCM, but maybe .NET is using WBEM
(?) or something like that and that's where the problem is.

I'm assuming this is an EXE->DLL->DLL call chain, right?

Now, threads cannot "change" apartments once they call CoInitializeEx, but
remember that there is only *one* MTA per process, with as many threads as
you need. On the flip side, you can have as many STAs as you want but only
one thread can live in them. Confusing =)

So your apartment is not being *changed*, it's being initialized at some
point to MTA. If the service controller requires STA then your only recourse
is going to be calling it from a separate thread.

--

Klaus H. Probst, MVP
http://www.vbbox.com/


Luis Fajardo said:
Klaus, thank you very much for helping me on this,

I'm not doing anything with the active thread, however, yesterday, following
the theory that the ServiceController was causing something to my active
thread, I decide to execute the method that is accessing the
ServiceController in a new thread, so in that way, my active thread doesn't
get affected in any way by whatever ServiceController is doing behind de
scenes, and I confirm the theory, the error I was having on my Old C App is
not happening any more. The only thing is that I still don't know why :) ?

Base on the error message I was getting, does that means that my active
thread was being changed to MTA? The thread is initiated on the Old C App
and going through layers up to a point is changed I imagine from STA to MTA.
Does that interpretation of the error have sense?

Any ideas, it will be nice to truly understand the problem, maybe I can stop
doing that workaround to avoid the problem.

Thanks in advance


Klaus H. Probst said:
Luis -

Are you setting the apartment model of the active thread in your C# code?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


Luis Fajardo said:
I wish I can explain my problem!, I'm trying to resolve a conflict problem
with an application that I create to integrate an old C application
with
..NET:
Old C App --> C++.NET IJW Bridge --> C#

It's a "message" type communication integration, where I pass some
parameters from the Old C App to the C# using a C++.NET as a bridge for
interoperability. The C# process the information and sends back a response.

On my Old C App, I'm receiving the following error message:

OleInitialize failed, rtnval=RPC_E_CHANGED_MODE. A previous call to
CoInitializeEx specified the concurrency model for this apartment as
multithreaded apartment (MTA).


This is happening under very specific circunstances, and I was able to track
down the problem to an specific line of code in my C# class, which is:

ServiceController[] services = ServiceController.GetServices();


The main problem I have is that I'm a C# developer with a little background
of C/C++ and threading, looking at the explanation of the error I'm getting,
looks to me, that some how, the ServiceController is changing the Apartment
Model of the thread that is running, what is interesting to me, is
that
the
error is being reported by the Old C App which makes me believe that
all
the
communication between the Old C App -> C++.NET -> C# is running under the
same thread, and that, some how, the ServiceController is changing the
Apartment Model to something different (MTA).

I know that I'm not explaining myself in a way to expect any help, but if
you or somebody have an idea I'll really appreciate it.


Thanks in advance



:

The service APIs were never COM-based. In any case, most of .NET is a
managed wrapper around some unmanaged API or COM interface.

Why is this important?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


I want to know if ServiceController is a FULL MANAGE
implementation or
if
is
really a proxy over native api's or COM.


Does anybody knows?


Thanks in advance
 
Thanks for all that feedback, is being a very good learning experience.

Klaus H. Probst said:
Luis,

Well, I'm surprised. It seems then (given your situation here) that either
your C app or your C++ bridge are calling CoInitializeEx() and specifying
MTA as the apartment model, and the CLR's SCM wrapper is STA instead, so
that's why you're getting that exception. Interesting. Technically you do
not need COM at all to interface with the SCM, but maybe .NET is using WBEM
(?) or something like that and that's where the problem is.

I'm assuming this is an EXE->DLL->DLL call chain, right?

Now, threads cannot "change" apartments once they call CoInitializeEx, but
remember that there is only *one* MTA per process, with as many threads as
you need. On the flip side, you can have as many STAs as you want but only
one thread can live in them. Confusing =)

So your apartment is not being *changed*, it's being initialized at some
point to MTA. If the service controller requires STA then your only recourse
is going to be calling it from a separate thread.

--

Klaus H. Probst, MVP
http://www.vbbox.com/


Luis Fajardo said:
Klaus, thank you very much for helping me on this,

I'm not doing anything with the active thread, however, yesterday, following
the theory that the ServiceController was causing something to my active
thread, I decide to execute the method that is accessing the
ServiceController in a new thread, so in that way, my active thread doesn't
get affected in any way by whatever ServiceController is doing behind de
scenes, and I confirm the theory, the error I was having on my Old C App is
not happening any more. The only thing is that I still don't know why :) ?

Base on the error message I was getting, does that means that my active
thread was being changed to MTA? The thread is initiated on the Old C App
and going through layers up to a point is changed I imagine from STA to MTA.
Does that interpretation of the error have sense?

Any ideas, it will be nice to truly understand the problem, maybe I can stop
doing that workaround to avoid the problem.

Thanks in advance


Klaus H. Probst said:
Luis -

Are you setting the apartment model of the active thread in your C# code?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


I wish I can explain my problem!, I'm trying to resolve a conflict problem
with an application that I create to integrate an old C application with
..NET:

Old C App --> C++.NET IJW Bridge --> C#

It's a "message" type communication integration, where I pass some
parameters from the Old C App to the C# using a C++.NET as a bridge for
interoperability. The C# process the information and sends back a
response.

On my Old C App, I'm receiving the following error message:

OleInitialize failed, rtnval=RPC_E_CHANGED_MODE. A previous call to
CoInitializeEx specified the concurrency model for this apartment as
multithreaded apartment (MTA).


This is happening under very specific circunstances, and I was able to
track
down the problem to an specific line of code in my C# class, which is:

ServiceController[] services = ServiceController.GetServices();


The main problem I have is that I'm a C# developer with a little
background
of C/C++ and threading, looking at the explanation of the error I'm
getting,
looks to me, that some how, the ServiceController is changing the
Apartment
Model of the thread that is running, what is interesting to me, is that
the
error is being reported by the Old C App which makes me believe that all
the
communication between the Old C App -> C++.NET -> C# is running under the
same thread, and that, some how, the ServiceController is changing the
Apartment Model to something different (MTA).

I know that I'm not explaining myself in a way to expect any help, but if
you or somebody have an idea I'll really appreciate it.


Thanks in advance



:

The service APIs were never COM-based. In any case, most of .NET is a
managed wrapper around some unmanaged API or COM interface.

Why is this important?


--

Klaus H. Probst, MVP
http://www.vbbox.com/


I want to know if ServiceController is a FULL MANAGE implementation or
if
is
really a proxy over native api's or COM.


Does anybody knows?


Thanks in advance
 
Back
Top