Sign in to follow this  

Unity [.net] COM, ActiveX, Windows Form, Web Browser [SOLVED]

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[edit: this has been solved, see here and to a lesser extent, here, for the solution] OK, let's get straight to business. I'm adding a Quick Help Browser to my application. It's mixed managed and unmanaged C++ (I think the fact it's got some unmanaged code is irrelevent here). I've added a Microsoft Web Browser object to my form in the standard way (through the IDE). Now, it all compiles happily, and it gets to this line in the form's InitializeComponent() function:
this->QuickHelpBrowser = new AxInterop::SHDocVw::AxWebBrowser();
And it gives me:
Quote:
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in system.windows.forms.dll Additional information: Could not instantiate ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' because the current thread is not in a single-threaded apartment.
Now, I'm not even really sure what a "single-threaded apartment" is. So basicly I have no idea where to start - as far as I can tell I need to turn my thread into a single-threaded appartment. The documentation I've found dosn't really help with managed C++ with forms. Any ideas? [Edited by - Andrew Russell on August 18, 2004 8:51:53 PM]

Share this post


Link to post
Share on other sites
Additionaly, this is my entry point:


int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}



So it's setting some kind of STA thing there... I'll try what AlanKemp just suggested in just a sec.

Share this post


Link to post
Share on other sites
I'm not sure what the exact error means (it seem to be missing a few words), but the apartment threading model (single-threaded apartment) for COM objects (the threading model is determined by the COM object) means that the COM Object ensures thread safety by only allowing the client (your program) to make method calls from the same thread the object was created in.

Perhaps the error means the control is being initialized from a thread other then the one that created it.

Share this post


Link to post
Share on other sites
A Single Threaded Apartment, basically means that when threads attempt to access a COM object, they have to access it sequentially. For example, say you have 3 threads that will at some point access one specific COM object. If they all try to access it at once, the first thread will gain access, finish what it's doing, and only at that point will the second thread be able to access the COM object. Then, once the second thread has finished, the third, and so on.

Multi Threaded Apartments on the other hand, allow simultaneous thread access to a COM object.

Hope that makes things a little clearer,

-hellz

Share this post


Link to post
Share on other sites
OK, I couldn't get AlanKemp's thing to work.

You're probably on the right track, Michalson. I'm not sure though - does windows forms do anything in particular with extra threads?

In any case, I'm just going to quickly check up on what kind of threading this is doing. Any more help would be appreciated.

BTW, I checked the error and that is the complete text.

Share this post


Link to post
Share on other sites
OK, on threads, This is the report from Process Explorer.

I've got the following thread start addresses:
mscoree.dll!CorExeMain
mscorwks.dll!CoInitializeCor+0x64a5
mscorwks.dll!CoInitializeCor+0x1780
mscorwks.dll!ReleaseFusionInterfaces+0x3fad9

That looks a lot like 4 threads to me. Not to mention two CoInitialize things, which sound like they could be causing a problem.

Of course, this is when it's paused in the debugger, so I'm not sure if that has any effect on things.

Additionally, .NET CLR Locks and Threads reports:
3 current logical threads
2 current physical threads
1 current recognized threads
1 total recogniszed threads

Anyway, perhaps there's some multi/single thread setting that I'm overlooking? Or is it something more sinister than that?

Cheers, AR

Share this post


Link to post
Share on other sites
OK folks, got somewhere at least. This is my new code:


__gc class AppFunctionClassThing
{
public:
static void MyAppMainFunction()
{
Application::Run(new Form1());
}
};

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
System::Threading::ThreadStart* ts = new System::Threading::ThreadStart(0, &AppFunctionClassThing::MyAppMainFunction);
System::Threading::Thread* myappthread = new System::Threading::Thread(ts);
myappthread->ApartmentState = System::Threading::ApartmentState::STA;
myappthread->Start();
myappthread->Join();
return 0;
}



This fixes the original problem but I still get an error:

Quote:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in system.windows.forms.dll

Additional information: File or assembly name Interop.SHDocVw, or one of its dependencies, was not found.


When it tries to do this:
this->ResumeLayout(false);


I've got AxInterop.Shdocvw.dll in my working directory, perhaps the Ax in the filename is the problem or something strange. I'm willing to bet it's something stupid, but I'll muck around with it in the morning *yawn* Late night debugging != fun.

Thanks for the help chaps, AR

Share this post


Link to post
Share on other sites
Also, my quick effort for a hackish fix produced this error instead:

Quote:
An unhandled exception of type 'System.IO.FileLoadException' occurred in system.windows.forms.dll

Additional information: The located assembly's manifest definition with name 'Interop.SHDocVw' does not match the assembly reference.


When I just copied the file and renamed it without the Ax [smile]. I didn't think it would work.

Share this post


Link to post
Share on other sites
Hi dude,

I spent a while looking around for any other information, but there isn't very much around at all. Some guy posted on UseNet saying he had the same problem, but I couldn't find a reply to his post. That was virtually all I could find.

Let us know if you solve it.

-hellz

Share this post


Link to post
Share on other sites
It works!!! Woo!

OK, the problem was, I have my outputs redirected to ($SolutionDir), and I had changed the AxSHDocVw thing to end up there, instead of ($OutDir) which is the default. (This is under References under the project in the solution explorer)

What I hadn't done was change the other two references to be dumped there (SHDocVw and stdole), mostly because I didn't realise that they were also added.

(see, I told you it'd be something stupid [wink])

This, combined with the threading trick makes it work very nicely.

Thanks for all the help chaps, AR

[Edited by - Andrew Russell on August 18, 2004 9:48:22 PM]

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628701
    • Total Posts
      2984289
  • Similar Content

    • By Mert Oguz
      well, i have started developing games last year, alone , I made a singleplayer 3d openworld rpg on unity you can look at it on googleplaystore ( kooru stone rpg ) whatever, this year, i wanted to make mmo, which gone really fine until I first try real hosting, I was working on "wamp" until then. The reason i am desperate now is that the way my game works.
      On my pc, using wamp mysql , with localhost as host for my game, i was testing my mmorpg with using andorid emulators, ofcourse no lag no issues no restrictions, beautiful dream... But then, I wanted to get real host from web, so, I rent a basic, cheaphest ever web host ( 10$ year ), and transferred my php files along with sql database. 
      So, I launched the game, still no issues, tried to handle 2-3 players by using my pc, phone, friend's phone...  
      After a while, ( after really short time (3-4mins)) host started not to respond, beacause those web hosting were not fit to handle mmos, i predicted that.
      now what i am explaining is that my game works like this and asking what way should i use to handle it :
      - Creates web request ( like : webhost.com/game/getplayerdata.php?ID=2 )
      -Reads request ( request result be like = "ID2-GoodGuyXx-23-123-4-123-43 )
      -Builds player using result string
      -does similar requests REEAALY FREQUENTLY  ( total requests of 8 - 12 times per seconds )
      With my current ultimate cheap web hosting, i can handle 2 players with low lag ( lol ) but, i want to handle around 20-100 players,
      just need a clear path, i have been struggling with google cloud sql and other vps server dedicated server options, i dont wanna pay much and get ripped off.
    • By Apollo Cabrera
      That planet is awesome!!! It's just slowly dissipating into space. Death by emmissions
      Love the green and yellow-orange juxtaposition (did I just say that?).
    • By Marc Vandenbosch
      D.O.T is an abstract arena shooter in which you fight for highscore with good or bad weapons.
      Follow me if you want more info:  https://twitter.com/bombjackm
    • By Apollo Cabrera
      Yasss!!! My first Unity3d game is on the App Store and Google Play.
      Download please! About 30 minutes to get through 5 missions.
      Let me know what you guys think.
      Thanks a bunch
      Bleeping Bots MacOsx Demo Cut.mp4
    • By TheZeph
      I've been doing some digging and come to the conclusion that I'm not really qualified to identify a game engine for my project alone so I came here asking for some advice.
      My game idea involves movement inside a sphere where you always have your feet on the ground (the "walls" of the sphere), basically an inverted planet. It's supposed to be multiplayer so it's not just you moving in the world. When you jump, gravity (or force etc) pulls your character towards where it jumped from and the character model should have its feet pointing that way.
      I'm really new to this and my own conclusion is that it messes with core functionality of Unreal Engine 4 which makes it really hard for me as a beginner to wrap my head around. It comes with practice but It's hard for me to come to a fair conclusion on what engine to use based on my knowledge. Therefore I'm asking for some guidance. I have most experience with the Unreal Engine.
      What engine would you recommend?
      Is the game idea with being inside a sphere difficult to do in general? Do you have any tips?
      I'm kinda lost at the moment so thank you for the help!

  • Popular Now