0

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();
}
EthanHunt
  • 473
  • 2
  • 9
  • 19

3 Answers3

1

Set your working directory of your application to the path of the DLL's, this should solve your problem.

Aykut Çevik
  • 2,060
  • 3
  • 20
  • 27
1
 const string pathToDll = "../../../Release/MyDLL.dll";

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Look here.
The name of the DLL and the path need to be split as shown there...

Community
  • 1
  • 1
weismat
  • 7,195
  • 3
  • 43
  • 58