RubyDotNet and Drag and Drop

W

Wayne Vucenic

I just heard about RubyDotNet, and it looks really great!

I did encounter a problem while trying to use drag and drop. (RubyDotNet
r4, Ruby 1.8, .NET 1.1, Windows XP). Here's a stripped down piece of
code that demonstrates the problem:

require 'dotnet'

loadLibrary 'System.Windows.Forms'

form = Form.new
form.topMost = true
form.text = 'Drag and Drop Test'
form.AllowDrop = true;

Application.Run form

When I run this I get a "Microsoft .NET Framework" dialog saying "An
unhandled exception has occurred in your application...DragDrop
registration failed" with the following details:

************** Exception Text **************
System.InvalidOperationException: DragDrop registration failed. --->
System.Threading.ThreadStateException: The current thread must set to
Single Thread Apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.
at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
--- End of inner exception stack trace ---
at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
at System.Windows.Forms.Control.OnHandleCreated(EventArgs e)
at System.Windows.Forms.Form.OnHandleCreated(EventArgs e)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmCreate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

Thanks!!!

Wayne
No Bugs Software
"Ruby and C++ Contract Programming in Silicon Valley"
 
T

Thomas Sondergaard

Apparently there is a need to set the COM ApartmentState to STA

Adding this in the first line of the ruby Init_dotnet function makes it go
away:

System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;

Cheers,

Thomas
 
T

Thomas Sondergaard

I was a little fast there. It seems to be sufficient to do it from ruby:

System::Threading::CurrentThread.ApartmentState =
System::Threading::ApartmentState.STA

or with the module you are using, I would guess:

CurrentThread.ApartmentState = ApartmentState.STA

Try it out.

Cheers,

Tom
 
W

Wayne Vucenic

Hi Tom and John,

Thank you both for your helpful replies. I really appreciate it!

I tried doing it from Ruby, but I still got exactly the same error.
The only reason I could think of that this wouldn't work would be if
this thread had already entered the MTA. I checked, and this was the
case. For example, the simple program

require 'dotnet'
puts System.Threading.Thread.CurrentThread.ApartmentState.ToString

outputs "MTA"

So, I'll use your earlier suggestion.

Thanks again,

Wayne

Sunday, September 28, 2003, 4:11:48 AM, you wrote:

TS> I was a little fast there. It seems to be sufficient to do it from ruby:

TS> System::Threading::CurrentThread.ApartmentState =
TS> System::Threading::ApartmentState.STA

TS> or with the module you are using, I would guess:

TS> CurrentThread.ApartmentState = ApartmentState.STA
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top