由本机C++加载的 DLL 中的 C# 窗体
C# form from DLL loaded by native C++
这是由此线程引起的问题:本机C++通过代理C++托管dll使用C# dll
简而言之,我正在通过 DLL 将(我的)C# 扩展加载到本机进程中。扩展需要显示窗体,以便用户可以控制它。我使用的是标准 .NET 表单,没有第三方库或任何东西,并且我的表单没有显示。更糟糕的是,它会挂起目标进程。它没有使用任何CPU,所以我感觉它在等待某些功能返回,但从来没有。
同样有趣的是,会弹出"初始化方法"消息框,但不会弹出"测试"消息框。我已经测试了我能想到的所有东西(STAthread,threads,DisableThreadLibraryCalls,以及不同的代码位置),直到星期天。我倾向于认为这是 Win32 互操作的一些模糊细节,但我找不到任何似乎会导致这些症状的东西。
你们中的一位专家可以看看我的代码并指出问题所在吗?
/// <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();
}
}
似乎目标本身有错。它没有直接加载扩展,而是加载本机"exensionManager.dll",幸运的是,他们正在使用DllMain来加载我的扩展。换句话说,我试图在加载器锁定下加载表单并遇到死锁。NET 尝试加载其他程序集。
答案很简单,我必须在新线程上显示表单。然而。NET 的线程也挂起了,因为它也需要一个死锁的库加载。
最后,我不得不直接求助于普通的P/Invoke来CreateThread(),但表单终于出现了。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- C++DLL中的C#窗体应用程序
- 将C++项目与 Visual Studio 中的窗体项目链接
- 显示主窗体 Qt C++ 中的其他窗体
- 如何在MFC SDI CFormView应用程序中调整窗体(对话框)的大小
- 具有可视C++的窗口窗体应用程序中的 Sleep()
- 由本机C++加载的 DLL 中的 C# 窗体
- 从 CLI C++应用程序加载库中的 C# WPF 窗体
- 如何在 Visual Studio 中的窗体之间继承变量,C++
- VS2010 WITHOUT.NET中的Windows窗体应用程序
- 如何在C++中访问MFC窗体中的全局var
- 在多字节字符集应用程序中的MFC窗体控件上设置Unicode文本
- Windows窗体中的DirectX9
- 窗体在_tWinMain中的WaitForSingleObject期间没有响应
- Windows窗体面板.Java swing中的句柄等价
- 是否有方法为窗口窗体中的特定按钮挂钩鼠标事件
- 无法从 Visual C++ 窗体应用程序中的窗体访问全局变量
- c++windows窗体中的字体渲染