简体   繁体   English

使用 DLL 调用进入 function 时程序崩溃

[英]Program crashes when entering function with DLL call

Code:代码:

private void button1_Click(object sender, RoutedEventArgs e)
{
   MessageBox.Show("Enters a button click");
   // Create our OpenGL Hwnd 'control'...
   HwndHost host = new WPFOpenGLLib.OpenGLHwnd();

   // ... and attach it to the placeholder control:
   hwndPlaceholder.Child = host;
}

WPFOpenGLLib.OpenGLHwnd() is a class in a C++ DLL. WPFOpenGLLib.OpenGLHwnd()是 C++ DLL 中的 class。 I added a reference to that DLL in my C# WPF application.我在我的 C# WPF 应用程序中添加了对 DLL 的引用。 But when I click button1 the application crashes.但是当我单击button1时,应用程序崩溃了。 Even the messageBox with »Enters a button click« does not show.即使是带有 »Enters a button click« 的消息框也不会显示。 Even when I use即使我使用

bool a = false;
if (a)
{
   // Create our OpenGL Hwnd 'control'...
   HwndHost host = new WPFOpenGLLib.OpenGLHwnd();

   // ... and attach it to the placeholder control:
   hwndPlaceholder.Child = host;
}

its crashes.它的崩溃。

By the way, on one computer (where I write the application) (Win7 32bit) everything works fine, on another computer (Win7 64bit) it crashes, and on yet another computer (Win7 32bit) its crashes as well.顺便说一句,在一台计算机(我编写应用程序的地方)(Win7 32bit)上一切正常,在另一台计算机(Win7 64bit)上崩溃,在另一台计算机(Win7 32bit)上也崩溃。

Does anyone know what the problem is here?有谁知道这里有什么问题?

I have not done any OpenGL stuff for a while (used to do it in C++ and win32 years ago), but a Hwnd is a window handle and like most things in C++ is just a memory address to a window, and as 64bit changes the default pointer size I assume that either the library you are using acting on behalf of C++ in the managed environment is not setup to deal with 64bit systems. I have not done any OpenGL stuff for a while (used to do it in C++ and win32 years ago), but a Hwnd is a window handle and like most things in C++ is just a memory address to a window, and as 64bit changes the默认指针大小我假设您在托管环境中代表 C++ 使用的库未设置为处理 64 位系统。

This sounds most likely to be some architecture mixup that causes the issues.这听起来很可能是导致问题的一些架构混淆。 Do you use a x64 version of the library on the x64 system or are you trying to use the 32 bit one?您是在 x64 系统上使用 x64 版本的库还是尝试使用 32 位版本的库? In later case go to the project properties (of your C# project) and set the target CPU to x64 (by default it should be Any CPU ).在后一种情况下 go 到项目属性(您的 C# 项目)并将目标 CPU 设置为x64 (默认情况下它应该是Any CPU )。 With the default setting your project will be executed by the 64 bit virtual machine and therefore fail to load the 32 bit library.使用默认设置,您的项目将由 64 位虚拟机执行,因此无法加载 32 位库。

Once that's done, ensure all systems got the correct dependencies installed.完成后,确保所有系统都安装了正确的依赖项。 Eg I think Microsoft never changed the .net applications' behaviour of simply crashing if the correct runtime files are missing.例如,我认为 Microsoft 从未改变 .net 应用程序的行为,即在缺少正确的运行时文件时简单地崩溃。 If that doesn't solve the issue either, there's most likely some bug (or another reason for crashing) in the initialization code of the DLL file.如果这也不能解决问题,那么 DLL 文件的初始化代码中很可能存在一些错误(或其他崩溃原因)。

I realize this is an old, dead thread, but if anyone else looks at this --我意识到这是一个古老的死线,但如果其他人看到这个——

I have a C# application that relies on a DLL (native/clr code).我有一个依赖于 DLL(本机/clr 代码)的 C# 应用程序。 When I run the application WITHOUT the DLLs it opens fine and everything looks great until you click on a button that actually calls a DLL function.当我在没有 DLL 的情况下运行应用程序时,它可以正常打开并且一切看起来都很好,直到您单击一个实际调用 DLL function 的按钮。 The program then crashes as described in this question.然后程序崩溃,如this question中所述。 Seems to me that the OP might have the DLLs on his dev machine, but does not have them on the other, mystery computers.在我看来,OP 可能在他的开发机器上有 DLL,但在其他神秘计算机上没有。 So, program runs and seems to work, until a DLL call is made.因此,程序运行并且似乎可以工作,直到进行 DLL 调用。

I was searching around to see how to check for all required DLLs at start up and stumbled onto this question.我正在四处寻找如何在启动时检查所有必需的 DLL,并偶然发现了这个问题。 A native C++ application won't even start up if all required DLLs are not found.如果未找到所有必需的 DLL,则本机 C++ 应用程序甚至不会启动。 Is C# "delay loading" it's DLLs? C# “延迟加载”是 DLL 吗? Maybe.也许。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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