[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RubyDotNet and Drag and Drop

Wayne Vucenic

9/28/2003 5:09:00 AM

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"


3 Answers

Thomas Sondergaard

9/28/2003 10:26:00 AM

0

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

"Wayne Vucenic" <wvucenic@netgate.net> wrote in message
news:182202853037.20030927220828@netgate.net...
> 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"
>
>


Thomas Sondergaard

9/28/2003 11:00:00 AM

0

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

"Thomas Sondergaard" <thomas@FirstNameGoesHereSondergaard.com> wrote in
message news:3f76b756$0$48887$edfadb0f@dtext02.news.tele.dk...
> 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
>
> "Wayne Vucenic" <wvucenic@netgate.net> wrote in message
> news:182202853037.20030927220828@netgate.net...
> > 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"
> >
> >
>
>


Wayne Vucenic

9/28/2003 9:01:00 PM

0

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