mjbWorld program - ActiveX control inside a VC++ managed program

I have used an existing ActiveX control inside a VC++ managed extension program using Windows Forms. See book below for details of how to do this:

From the DOS command prompt I ran the aximp program on my ActiveX component mjbogl.dll as follows-
aximp mjbogl.dll

This produced two files:
mjboglLib.dll and AxmjboglLib.dll

I then used this component inside my program as follows:

Define it in the header-

#using <AxmjboglLib.dll>
using namespace AxmjboglLib;
using namespace System::ComponentModel;

Also in the header I have defined a AxHost* control:

 AxmjboglCtl* myControl;

I have then used the control in my class which is derived from Form.

void frame3d::InitializeComponent()
{
   Thread::CurrentThread->ApartmentState = ApartmentState::STA;
   myControl = new AxmjboglCtl(); 
   myControl->mouseDown += new _ImjboglCtlEvents_mouseDownEventHandler(this,&frame3d::mouseDownAction);
   myControl->mouseUp += new _ImjboglCtlEvents_mouseUpEventHandler(this,&frame3d::mouseUpAction);
   myControl->mouseDrag += new _ImjboglCtlEvents_mouseDragEventHandler(this,&frame3d::mouseDragAction);
   Controls->Add(myControl);
 }
 

From: Martin Baker
Subject: How do I include OpenGL in MC++ program
Newsgroups: microsoft.public.dotnet.languages.vc
View this article only
Date: 2002-03-26 04:31:54 PST

How do I include OpenGL in a C++ program which uses purely ManagedExtensions.? (the 3d frame is mixed in with other screen objects using windows forms).

I have tried doing this by taking the ATL ActiveX Cube sample that comes with VC++. I ran the aximp tool to generate a wrapper class that lets me use it as a control in the application.

My program compiles and links correctly, but when I run it (in debug mode) I get the following error:

An unhandled exception of type 'System.Threading.ThreadStateException' occurred

in system.windows.forms.dll

Additional information: Could not instantiate ActiveX control

'72d0889c-0ebd-11d0-bf0d-0000e8d0d146' because the current thread

is not in a single-threaded apartment.

Can anyone suggest how I can get round this problem? Or alternatively is
there any other way to include an OpenGL view in a MC++ form?

I would appreciate any ideas,

Martin
Message 2 in thread



From: Anson Tsao [MSFT] (ansontonline@microsoft.com)
Subject: Re: How do I include OpenGL in MC++ program
Newsgroups: microsoft.public.dotnet.languages.vc
View this article only
Date: 2002-03-27 12:09:22 PST

You need to set the current thread to STA.
There are 2 methods to do that:
1) using namespace System::Threading;

void SomeMethod()
{
Thread::CurrentThread->ApartmentState = ApartmentState::STA;
}
2) Use the STAThreadAttribute to annotate your main method.


--
Anson Tsao
Visual C++ libraries team

Of course, this posting wouldn't be complete without a nice, juicy
disclaimer from our lawyers: This posting is provided "AS IS" with no
warranties, and confers no rights. You assume all risk for your use. © 2001
Microsoft Corporation. All rights reserved.




Message 3 in thread
From: Martin Baker
Subject: Re: How do I include OpenGL in MC++ program
Newsgroups: microsoft.public.dotnet.languages.vc
View this article only
Date: 2002-03-28 01:35:08 PST

Anson Tsao,

I tried this, just to be sure I tried both methods, but I am still getting
the same error.

Thanks,

Martin



From: Anson Tsao [MSFT] (ansontonline@microsoft.com)
Subject: Re: How do I include OpenGL in MC++ program
Newsgroups: microsoft.public.dotnet.languages.vc
View this article only
Date: 2002-03-28 13:17:48 PST

Martin

For the [STAThreadAttribute] to wokr, you also need to set the linker option
/ENTRY: to 'main', otherwise the entry point is mainCRTStartup() (which is
unmanaged). Sorry for not making that clear last time. You can also set the
/SUBSYSTEM to Windows to get rid of the console.

To make sure that the current thread is STA, you can just test for
Threading::Thread::CurrentThread->ApartmentState in your MainWindow
constructor.

 


--
Anson Tsao
Visual C++ libraries team

Of course, this posting wouldn't be complete without a nice, juicy
disclaimer from our lawyers: This posting is provided "AS IS" with no
warranties, and confers no rights. You assume all risk for your use. © 2001
Microsoft Corporation. All rights reserved.

"Martin Baker"wrote in message
news:a7uo3k$1qc$1@knossos.btinternet.com...



From: Martin Baker
Subject: Re: How do I include OpenGL in MC++ program
Newsgroups: microsoft.public.dotnet.languages.vc
View this article only
Date: 2002-03-29 00:53:04 PST


Anson Tsao,

> For the [STAThreadAttribute] to wokr, you also need to set the linker option
> /ENTRY: to 'main'

Thanks! it works!


metadata block
see also:
Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover This explains how to program using the managed extensions to C++.

See chapter 27 "living with COM" for a step by step guide for how to use ActiveX controls in Windows Forms.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Visual C++ .NET for Microsoft Platforms only.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2017 Martin John Baker - All rights reserved - privacy policy.