实际上,我得到了一个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);
问题是这将是一个可安装的项目,因此用户的文件夹将不会相同(例如: pierre、paul、jack、妈妈、爸爸……)取决于运行它的计算机/会话。
所以我希望我的代码更通用一些,就像这样:
/*
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”参数。
所以我的问题是:在这种情况下可以做些什么?
https://stackoverflow.com/questions/8836093
复制相似问题