当我在我的Dll中使用过剩函数时,DllNotFound异常

时间:2011-04-21 12:12:10

标签: c# c++ dll glut dllnotfoundexception

我有一个c ++ dll和c#应用程序。在C#应用程序中,我从dll调用函数。 具有简单的功能,如:

extern "C"
{
    __declspec(dllexport) void HelloFromDll()
    {
        MessageBox(NULL, _T("Hello from DLL"), _T("Hello from DLL"), NULL);
    }
}

一切正常。 当我像这样使用过剩功能时:

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

3 个答案:

答案 0 :(得分:1)

将应用程序的working directory设置为DLL的路径,这可以解决您的问题。

答案 1 :(得分:1)

 const string pathToDll = "../../../Release/MyDLL.dll";

这可能是一条有效的道路。或者它有助于Windows找到任何依赖的DLL,但事实并非如此。更糟糕的是,部署应用程序后赔率为零。

向项目添加一个post build事件,该事件使用xcopy / d / y将所有必需的本机DLL复制到$(TargetDir)目录中。 Windows总是首先在那里看。它既可以在调试时也可以在部署后使用。您的构建目录包含您需要部署的所有内容。

答案 2 :(得分:0)

Look here
需要拆分DLL的名称和路径,如图所示......