如何在运行时指定[DllImport]路径?

如何在运行时指定[DllImport]路径?

事实上,我得到了一个C ++(工作)DLL,我想导入我的C#项目来调用它的函数。

当我指定DLL的完整路径时,它确实有效,如下所示:

string str = "C:\\Users\\userName\\AppData\\Local\\myLibFolder\\myDLL.dll";[DllImport(str, CallingConvention = CallingConvention.Cdecl)]public static extern int DLLFunction(int Number1, int Number2);

问题是它将是一个可安装的项目,因此用户的文件夹将不同(例如:皮埃尔,保罗,杰克,妈妈,爸爸......),这取决于计算机/会话的运行情况。

所以我希望我的代码更通用,如下所示:

/* 
goes right to the temp folder of the user 
    "C:\\Users\\userName\\AppData\\Local\\temp"
then go to parent folder
    "C:\\Users\\userName\\AppData\\Local"
and finally go to the DLL's folder
    "C:\\Users\\userName\\AppData\\Local\\temp\\myLibFolder"
*/string str = Path.GetTempPath() + "..\\myLibFolder\\myDLL.dll"; [DllImport(str, CallingConvention = CallingConvention.Cdecl)]public static extern int DLLFunction(int Number1, int Number2);

最重要的是“DllImport”需要DLL目录的“const string”参数。

所以我的问题是::在这种情况下可以做些什么?


RISEBY
浏览 1442回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP