为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

RISEBY 2019-08-26 14:15:39
如何在运行时指定[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”参数。所以我的问题是::在这种情况下可以做些什么?
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 1439 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信