C # form from a DLL loaded in native C ++

This question arises from this topic: Native C ++ uses C # dll through a proxy server managed with C ++

In short, I load the (my) C # extension into my own process through a DLL. The extension should show the form so that the user can control it. I use standard .NET forms, there are no third-party libraries or anything else, and my form does not appear. Worse, it hangs in the target process. It does not use any processor, so I feel that it is waiting for the return of a function, but it never does.

It is also interesting that the “Initialize Method” message box appears, but not the “Test” message box. I tested everything I can think of (STAthread, threads, DisableThreadLibraryCalls, as well as various code locations) every Sunday. I tend to think that these are some obscure details of the Win32 interaction, but I cannot find anything that could cause these symptoms.

Can one of you experts take a look at my code and indicate what the problem is?

/// <summary> /// Provides entry points for native code /// </summary> internal static class UnmanagedExports { [UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)] public delegate int SendRecv([MarshalAs(UnmanagedType.SafeArray)]byte[] ByteArray, UInt64 Len); [STAThread] [DllExport("Initialize", CallingConvention.StdCall)] public static int Initialize(IntPtr hInstance, SendRecv Send, SendRecv Recv) { return DLLinterface.Initialize(hInstance, Send, Recv); } [DllExport("Terminate", CallingConvention.StdCall)] public static void Terminate() { DLLinterface.Terminate(); } } internal class DLLinterface { static System.Threading.Thread uiThread; [STAThread] internal static int Initialize(IntPtr hInstance, UnmanagedExports.SendRecv Send, UnmanagedExports.SendRecv Recv) { MessageBox.Show("Initialize method"); try { uiThread = new System.Threading.Thread(Run); uiThread.Start(); } catch (Exception ex) { MessageBox.Show("Failed to load: " + ex.Message, "Infralissa error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return 1; } [STAThread] private static void Run() { MessageBox.Show("Test"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } internal static void Terminate() { MessageBox.Show("Terminating."); if (uiThread.IsAlive) uiThread.Abort(); } } 
+4
source share
1 answer

It seems that the goal itself was to blame. He did not download extensions directly, but instead loaded his own "exensionManager.dll", where, as luck would have it, they used DllMain to load MY extensions. In other words, I tried to load the form under loaderlock and ran into a dead end. NET tried to load other assemblies.

The answer is simple, I had to show the form in a new thread. However, .NET threads also hung up, as it also required loading a library, which came to a standstill.

In the end, I had to resort to vanilla P / Invoke to CreateThread () directly, but the form finally appears.

+1
source

All Articles