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(); } }
source share