首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在运行时指定[DllImport]路径?

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

Stack Overflow用户
提问于 2012-01-12 21:45:47
回答 7查看 161.8K关注 0票数 155

实际上,我得到了一个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”参数。

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8836093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档