John Lam 1997 — 2026

Ruby, COM and the CLR

If you want to write code that interacts with windows (note the lowercase w) and COM, then your thread must live in its own single threaded apartment (STA). This poses a problem if you want to write some Windows Forms code in Ruby. Here’s the problem in a nutshell:

If you’re using my bridge, the CLR is delay-loaded; it loads when my bridge is loaded by the Ruby interpreter. Now Ruby doesn’t know anything about COM, so Ruby doesn’t turn on COM via a CoInitializeEx call at startup. So I should have a chance to turn on COM on behalf of Ruby (more on why this is bad later) when my bridge loads. One of the really nice things about using MC++ is that the compiler/libraries does a bunch of auto-magical startup code to hoist the CLR into memory when my bridge loads. This is where our problems begin.

In V1.0 and V1.1 of the CLR, the runtime theoretically didn’t turn on COM either (it actually did in a number of corner cases). This lets you write code like:

Thread::CurrentThread::ApartmentState = ApartmentState::STA;

However, under V2.0 of the CLR all threads automatically start in the MTA – which means that you can no longer write code like the line above. You can instruct the CLR at intialization time to have threads default to their own STA (via an app.config file or via an STAThread attribute on your application’s Main method), or you can intialize COM yourself before you initialize the CLR.

For a variety of reasons, if the CLR is delay-loaded, there really is no way to declaratively tell the CLR about your intentions with respect to COM. This leaves you with one other option: turn on COM yourself before the CLR loads.

Now how can you do this without hacking Ruby itself? Anson Tsao, and old friend of mine at Microsoft provided me with the answer as I was feverishly coding up my bridge in anticipation of its first internal review meeting.

I had an Avalon demo application that I was coding up for Matthew’s second year birthday and it was refusing to run because the calling thread was not an STA thread! Now, as anyone with a 2 year old knows, they can get rather upset when things don’t go their way :) But thankfully at the very last minute, Anson provided me with this hack:

void InitializedManagedRubyBridge() {
  ...
}

#pragma unmanaged
// Main entry point
__declspec(dllexport) void Init_RubyShim() {
  CoInitializeEx(0, COINIT_APARTMENTTHREADED);
  InitializeManagedRubyBridge();
}

Notice how I can mix unmanaged and managed code inside the same DLL! If I define my DLL entry point in unmanaged code, I can turn on COM and then jump into my managed code. The call to InitializeManagedRubyBridge() gets intercepted by MC++ libraries which in turn hoist the CLR into memory before calling my method.

This is a nice way to solve this problem. In a future post, I’ll point out how this doesn’t really solve this problem in all cases but likely solves this problem in most cases that users of my bridge will run across.

4 archived reader comments

Jamie Cansdale (2005-11-14)

Hi John,

You said, “You can instruct the CLR at intialization time to have threads default to their own STA (via an app.config file or via an STAThread attribute on your application???s Main method)”

I haven’t been able to find any documentation about setting the default threading model using app.config settings. Could you point me in the right direction?

Thanks, Jamie.

John Lam (2005-11-16)

My mistake Jamie. It was a C++ linker switch /CLRTHREADATTRIBUTE:STA and not an app.config setting.

atul (2006-11-21)

how to set /CLRTHREADATTRIBUTE:STA in dot net 2003

Jamie Cansdale (2005/11/14)

Hi John, You said, "You can instruct the CLR at intialization time to have threads default to their own STA (via an app.config file or via an STAThread attribute on your application???s Main method)" I haven’t been able to find any documentation about setting the default threading model using app.config settings. Could you point me in the right direction? Thanks, Jamie.