2011-04-21 18 views
0

我有一個C++ DLL和C#應用程序。在C#應用程序中,我從dll調用函數。 通過簡單的功能,如:DllNotFound異常,當我在我的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(); 
} 
+0

請驗證答案。 – 2011-04-24 15:34:27

回答

1

設置你的應用程序中的DLL的路徑working directory,這應該解決您的問題。

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

勝算並不大,這將是一個有效的路徑。或者它可以幫助Windows找到任何依賴的DLL,但它不會。更糟糕的是,在部署應用程序後,可能性爲零。

將項目構建事件添加到項目中,該項目使用xcopy/d/y將所有必需的本機DLL複製到$(TargetDir)目錄中。 Windows總是首先看到那裏。它在你調試和部署之後都可以工作。而你的構建目錄包含你需要部署的所有東西。

相關問題