How to mark / build C# / WPF component STA

L

Leo

I am hosting WPF components in unmanaged MFC. This WPF components will
do some ricktext editing. Hosting / editing are working fine but
closing will crash application on exit (release version only and no
crash on debug). The following is the current structure:

class parentA_COM : definited / implemented in unmanaged C++, STA
class memberB_COM : definited in unmanaged C++, but implemented in C#
(ComVisible)
class winUserControl: implemented in C# to host WPF user control;
class wpfUserControl: implemented in C#

class MyApp // unmanaged C++
{
main ()
{
parentA_COM pPA(); // Initialize COM component
}
}

class parentA_COM // unmanaged C++
{
void get_hWnd()
{
m_ipMB.CreateInstance(xxx);
m_ipMB->get_hWnd();
}

private:
memberB_COM m_ipMB;
}

class memberB_COM : IDisposable, ... // C#
{
constuctor // initialize winUserControl
}

class winUserControl : UserControl
{
host WPF control wpfUserControl
}

How can I mark / make wpfUserControl / or winUserControl / or
memberB_COM STA? Thanks.
 
P

Peter Duniho

Leo said:
[...]
How can I mark / make wpfUserControl / or winUserControl / or
memberB_COM STA? Thanks.

Your question is not very easy to understand. There's not even any C#
code, and the C++ code won't compile.

As far as marking COM classes, you can indicate when registering the
class whether it's MTA or STA. If you don't indicate, it's assumed to
be STA.

Now, when _using_ an STA COM object, you also need to make sure you
create it in an STA thread. But that's not what you asked about.

Pete
 
L

Leo

Pete,

First apologize for posting same thing on multiple forums. I don't
know how to make it cross forum. If so, definitely I will only post to
one.

Second thanks for your reply. What I am trying to do is to mark and
build the WPF component as STA. I couldn't find instruction on how to
do so (VS 2008 C# project) although I do see option for C++. If you
know, please advise how.

Thanks again...

Leo
 
P

Peter Duniho

Leo said:
Pete,

First apologize for posting same thing on multiple forums. I don't
know how to make it cross forum. If so, definitely I will only post to
one.

I'm sure if you check with Google, it can provide references that detail
the difference between multi- and cross-posting, and how to do it
correctly. The short version is: write the post once, and specify all
newsgroups in the "Newsgroup:" field, separated by commas.
Second thanks for your reply. What I am trying to do is to mark and
build the WPF component as STA. I couldn't find instruction on how to
do so (VS 2008 C# project) although I do see option for C++. If you
know, please advise how.

Please be more specific. What "option" do you see in C++? AFAIK,
neither C# nor C++ has a project setting that sets STA vs MTA. As I
mentioned before, whether a COM object is STA or MTA has more to do with
the implementation, which is a holistic attribute, not a single setting.

When the COM object is registered, you can specify the apartment type in
the registry entry for the COM server. But that's not
language-specific. The same registry values are set whether the COM
object was written in C# or C++.

I don't recall, it's possible the C++ ATL project template provides the
setting in the template wizard, but once the project is created, there's
no setting to change, and you can easily inspect the C++ project to see
how the setting is made.

Until you provide much more detail in your question, it's doubtful
you're going to get a useful answer. For best results, post a
concise-but-complete code example showing exactly what you're trying to
do, and what you're having trouble with at the moment.

Pete
 

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