简体   繁体   中英

Visual Studio 2010 - my Windows Form doesn't show up unless I make a MessageBox.Show() call before or after

I'm currently making my first project in C# under Visual Studio 2010.

Here is the context:

I have a first DLL, which is written in not managed C++, and that DLL exports some functions with the following instruction: extern "C" __declspec(dllexport)

An external Windows .exe calls one of my functions as an entry point: this part is totally functional, OK.

Next part is the UI part, where I encounter some tricky behaviours and I'm not totally sure what's the right way, I took the good one.

Aside of my first DLL, I have an Assembly (currently a Windows Form application); its aim is to pop-up a Windows and displaying some stuff in classic controls like ListBox / ListView etc.

Since, I want to call my managed code (naming it “UI_part.exe”) from my unmanaged code (ie from my native C++ DLL), I have modified the post-build command line in my VS2010 project to register my assembly with RegAsm.exe in order to obtain a “UI_part.tlb”.

My native C++ code do a simple “#import” instruction on my .tlb to have access to the interface I have previously defined in my C# project.

In this interface, I defined in the main .cs file of my C# project, I have a Class implementing a function Initialize(): this function is the one which is supposed to make the calls to show the Windows Form, saying Application.Run(myForm);

My problem is that, in this state, whereas I know that my function Initialize is well called by native C++ code, thanks to COM interop mechanism, none of the following instructions make my Form to appear on screen:

  • Application.Run(myForm);
  • myForm.ShowDialog();
  • myForm.Show();

The fact is if I add a simple MessageBox.Show("myClass.Initialize()"); in my Initialize function before, the Form appears and all things seems to be normal.

But I don't want to make some pop up calls to obtain normal behaviour.

OK, finally I have found a solution:

I make the calls "myForm = new ();" and "Application.Run(myForm);" in a Thread;

But this alone is NOT sufficient:

I also add in the constructor of my Windows Form class the instruction "this.Show();" just after the "InitializeComponent();"

It makes my Form always showing up without need to pop up any MessageBox.

However, I have another issue with my project:

In my native C++ DLL, I use the #import UI_part.tlb to have access to the interface that my Assembly defines and implements.

But when I am under the real runtime execution environment, ie the external exe calling my native C++ DLL, if the path from which I did the .tlb import is not available anymore (I unmount the drive for my tests), the exe crash when attempting to access my Assembly functions...

However, my Assembly is present in the same directory of the native C++ DLL, but it seems that Windows search only to the original import path?

How can I do to make a safe deployment and not to be dependant of the compilation paths?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM