Re: Simple COM EXE Server question [More queries]

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi it's virat here. and i was trying to create a MTA out-process server.

i took help from the article
http://support.microsoft.com/kb/244495

and created my Exe Com server as MTA and then tried to call my exe from ..NET using multiple threads at same time. i have following function in my COM Exe Server which i am calling from .NET.

STDMETHODIMP CMain::Check(BSTR bsInput, VARIANT *Result)
{
LogToFile(_T("Entered ... ")+(_bstr_t) (long) this +_T("\n"),_T("C:\\Chk.Log"));
_bstr_t bstMain = _T("");
try{
for (unsigned long a =0; a < 80000 ; a ++)
{
for (int x=0;x <100 ; x++)
int y= 0;
_bstr_t bstChk = _T("");
for (int i=0;i<10;i++)
{
bstChk += (_bstr_t)(long) a + _T("\n");
}
bstMain += (_bstr_t)(long) a + _T("\n");
//LogToFile(bstChk +_T("\t in Process ... ")+(_bstr_t) (long) this +_T("\n"),_T("C:\\Chk.Log"));
}
}
catch(...)
{
bstMain += (_bstr_t)(long)GetLastError();
}
Result->vt = VT_BSTR;
_bstr_t bstVal = (_bstr_t) bsInput;
Result->bstrVal = bstVal.copy();
LogToFile(_T("Ending ... ")+(_bstr_t) (long) this + _T(" with" ) + bstMain +_T("\n"),_T("C:\\Chk.Log"));
return S_OK;
}

and i used following code in .NET exe to call my com server in multiple threads

static int x = 0;
void chk()
{
Type Typ = Type.GetTypeFromProgID("Test.Main");
Object Obj = Activator.CreateInstance(Typ);
object[] ObjArr = new object[1];
ObjArr[0] = x.ToString() ;
x++;
object Result="";
try{
Result = Typ.InvokeMember("Check", BindingFlags.InvokeMethod, null, Obj, ObjArr);
}
catch(Exception e)
{
Exception ex = e.InnerException;
if (ex == null)
ex = e;
MessageBox.Show(ex.Message + " "+ ex.Source);
}
MessageBox.Show(Result.ToString());
}

private void button1_Click(object sender, EventArgs e)
{
Thread ChkThread = new Thread(new ThreadStart(chk));
ChkThread.Priority = ThreadPriority.Lowest;
ChkThread.IsBackground = true;
ChkThread.Start();
Thread ChkThread1 = new Thread(new ThreadStart(chk));
ChkThread1.Priority = ThreadPriority.Lowest;
ChkThread1.IsBackground = true;
ChkThread1.Start();
Thread ChkThrea2 = new Thread(new ThreadStart(chk));
ChkThrea2.Priority = ThreadPriority.Lowest;
ChkThrea2.IsBackground = true;
ChkThrea2.Start();
}

and when i run this test .net exe i am getting following error

Error HRESULT E_FAIL has been returned from a call to a COM component. mscorlib
The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

Please help inform me if i am missing any specific info to explain the problem.

Virat



"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message news:OHNjHDzbIHA.4196@xxxxxxxxxxxxxxxxxxxxxxx
Andy Larter <AndyLarter@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Please can you confirm that my understanding of COM out-of-process
servers is correct.

1) The threading model of all objects in a COM EXE is determined by
the _ATL_FREE_THREADED or _ATL_APARTMENT_THREADED macro.

Not directly. The threading model is determined by whether the thread
that registers the class factory for the object has entered STA or MTA.
I'm talking about the thread that calls CoRegisterClassObject (possibly
indirectly via CAtlExeModuleT::RegisterClassObjects). Typically, but not
necessarily, that's the application's main thread.

Now, if you use CAtlExeModule in your application (as you would in a
default wizard-generated project), it initializes COM in its constructor
based on those macros, and registers class factories in its Run()
method. So if you want to play funny games with apartments, you can't
use CAtlExeModule and will have to roll equivalent functionality
yourself.

_ATL_FREE_THREADED and _ATL_APARTMENT_THREADED also determine whether
CComObjectRoot uses thread-safe reference counting. For your own
objects, you can derive from CComObjectRootEx instead and specify
explicitly whether or not you want it to be thread-safe. But you can't
do this for class factories (unless you are willing to write a custom
one), so their thread-safety is controled by those macros. Naturally, if
you want the object to live in MTA, it has to be thread-safe. If the
object lives in STA, you can still make it thread-safe, it'll just add
slight performance overhead.

2) You can't mix the threading model of objects in a COM EXE, they are
either all apartment or all free threaded, depending on the macro.

In principle, you can. You just register class factories for different
CLSIDs from different apartments. But default ATL project setup is not
designed for this, so it will take some work to suppress default
behavior and put in custom one.

In addition, it is possible to write a fancy class factory that, while
itself registered on one apartment, actually creates objects in other
apartments. CComClassFactoryAutoThread does this.

3) The only way to use say a apartment threaded object in a free
threaded server is to host the object in a COM DLL.

Are you talking about objects the server itself implements? In addition
to techniques described in my response to question #2, you can create
your own object on an STA thread (which presumably you create
specifically for this purpose) with CComObject::CreateInstance,
completely bypassing COM registration and class factory mechanism. Don't
forget to marshal the object's interface pointer to your MTA thread, and
don't forget to run a message pump on your STA thread.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Relevant Pages

  • Re: Technical Delphi Question (but not about coding)
    ... > Windows 2003 Server allow users to rename a running EXE. ... > loading/reloading of resources from the original EXE cause problems? ...
    (borland.public.delphi.non-technical)
  • Re: Help please. Outlook express or comcast is creating misleading mail headers
    ... and go down to the "Outgoing Mail Server" section. ... > headers, but the smtp mail host that comcast uses now includes that in all ... MTA, and the aosake.net MX. ... that the filters are working for. ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • Re: New/Old Windows virus? Help required please.
    ... Synflood attack from this server - the server was flooding the entire ... Only when we killed this exe did it stop. ... Current A/V is Symantec 10d and it was fully up to date. ... Oh - the only other thing was that the server had not windows updated ...
    (microsoft.public.security.virus)
  • Re: Remote application lunch and terminate (IIS server)
    ... If the program you are trying to launch is not a real CGI application, ... What you want to do is possible, but really has NOTHING to do with IIS. ... then invoke a command on the server. ... depends on how the EXE is written. ...
    (microsoft.public.inetserver.iis)
  • RE: NDR 5.3.0 Generated. Exchange mistakenly sent to the wrong MTA
    ... > of your Exchange Server. ... Open Exchange System Manager and expand the "Connectors" node. ... Enable Diagnostic Logging to determine the root of the transport issue. ... The MTA Service is running ...
    (microsoft.public.windows.server.sbs)