0

我有一个 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();
}
4

3 回答 3

1

将应用程序的工作目录设置为 DLL 的路径,这应该可以解决您的问题。

于 2011-04-21T12:16:51.707 回答
1
 const string pathToDll = "../../../Release/MyDLL.dll";

这将是一条有效路径的可能性并不大。或者它可以帮助 Windows 找到任何依赖的 DLL,但它没有。更糟糕的是,部署应用程序后的可能性为零。

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.

于 2011-04-21T12:30:35.983 回答
0

看这里
DLL 的名称和路径需要拆分,如图所示...

于 2011-04-21T12:29:04.557 回答