当我在 Dll 中使用 glut 函数时出现 DllNotFound 异常
我有一个 C++ DLL 和 C# 应用程序。在 C# 应用程序中,我从 dll 调用函数。 具有简单的功能,例如:
extern "C"
{
__declspec(dllexport) void HelloFromDll()
{
MessageBox(NULL, _T("Hello from DLL"), _T("Hello from DLL"), NULL);
}
}
一切正常。 当我像这样使用 glut 函数时:
extern "C"
{
__declspec(dllexport) int InitGlut()
{
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("MyWindow");
glutDisplayFunc(renderScene);
glutMainLoop();
return 0;
}
}
我得到 DllNotFound 异常。为什么? C#代码:
const string pathToDll = "../../../Release/MyDLL.dll";
[DllImport(pathToDll)]
public static extern void HelloFromDll();
[DllImport(pathToDll)]
public static extern int InitGlut();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HelloFromDll();
InitGlut();
}
I have a c++ dll and c# application. In C# application I call function from dll.
With simple function like:
extern "C"
{
__declspec(dllexport) void HelloFromDll()
{
MessageBox(NULL, _T("Hello from DLL"), _T("Hello from DLL"), NULL);
}
}
all works fine.
When i use function with glut like this:
extern "C"
{
__declspec(dllexport) int InitGlut()
{
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("MyWindow");
glutDisplayFunc(renderScene);
glutMainLoop();
return 0;
}
}
i get DllNotFound Exception. Why?
C# code:
const string pathToDll = "../../../Release/MyDLL.dll";
[DllImport(pathToDll)]
public static extern void HelloFromDll();
[DllImport(pathToDll)]
public static extern int InitGlut();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HelloFromDll();
InitGlut();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将应用程序的工作目录设置为DLL 的路径,这应该可以解决您的问题。
Set your working directory of your application to the path of the DLL's, this should solve your problem.
这是一条有效路径的可能性并不大。或者说它可以帮助 Windows 找到任何依赖的 DLL,但事实并非如此。更糟糕的是,部署应用程序后,这种可能性为零。
将构建后事件添加到项目中,使用 xcopy /d /y 将所有必需的本机 DLL 复制到 $(TargetDir) 目录中。 Windows 总是首先查找那里。它在调试时和部署后都可以工作。您的构建目录包含部署所需的所有内容。
Odds are not great that this would be a valid path. Or that it helps Windows find any dependent DLLs, it doesn't. What's much worse is that the odds are zero after you deployed your app.
Add a post build event to your project that copies all the required native DLLs into the $(TargetDir) directory with xcopy /d /y. Windows always looks there first. And it will work both when you debug and after you deploy. And your build directory contains everything you need to deploy.
看这里。
DLL 的名称和路径需要拆分,如下所示...
Look here.
The name of the DLL and the path need to be split as shown there...