views:

204

answers:

2

Hello Everyone,

I write extensions to a C++ program. I write standard C/C++ dlls and I use IJW to call C# dlls. This has always worked perfectly until I wrote and called a C# dll that in turn called an OpenFileDialog and a SaveFileDialog. Whenever either was called with ShowDialog, the app would freeze.

So in making a "Minimum Working Example" I got an: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll

Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

So, I tried to add the STAThread attribute infront of the main in my "Minimum Working Example" and I got this error.

error C2337: 'STAThread' : attribute not found

So, two questions:

  1. How d0 I get the "Minimum Working Example" working and
  2. How do I get the real app working?

(is it even possible to add the STAThread attribute in a #pragma unmanaged block?)

#pragma unmanaged
BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{ 
...
}

Thanks for your help.

+2  A: 

When you're a DLL you can't directly control the apartment your thread is running in - a thread can only be in one apartment at a time, and so if the thread that calls into your DLL is already some other apartment, then you can't change it.

Do you control the code that is calling your DLL? If not, then I think the best you can do is to start up your own thread (in which you can control the apartment it runs in, via CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) and then call OpenFileDialog/SaveFileDialog from there.

Dean Harding
Much more better answer than mine.
Preet Sangha
A: 

Everyone,

Thanks for the help. For some reason starting a new thread never occurred to me.

Thanks everyone for that tidbit. Since I am already in IJW C++/cli land I thought it better to solve the problem using the .net framework.

Once I realized I needed a new thread, it was quite simple.

I moved my function that called the C# dll into a separate class:

ref class StaClass { public:

System::String^ strFile;
System::String^ strNote;

void CallWiki()
{
    WikiNotes::FrmWiki fw;

    fw.File = strFile;
    fw.Note = strNote;

    fw.ShowDialog();
}

};

and then from the main thread I used this code to spin up a STA Thread.

wiki->strFile = gcnew System::String(File);
wiki->strNote = gcnew System::String(Note);

ThreadStart^ threadDelegate = gcnew ThreadStart(wiki, &StaClass::CallWiki);
Thread^ newThread = gcnew Thread(threadDelegate, 0);
newThread->SetApartmentState(ApartmentState::STA);
newThread->Start();

Simple and easy to read and understand (At least for me - I'm a .Net programmer, I never delved much into COM, MFC and ATL)

saunderl