加载的 DLL 能知道它所在的位置吗?
我正在构建一个 Office 使用的 DLL。当 Office 与它一起运行时,我想确定它所在的位置。这可能吗?
前任。在 Office 中运行时 DLL 中的代码:
// should return C:\tmp\officeaddin.dll,
// currently C:\Program Files\Microsoft Office\Office 12
MessageBox.Show(Application.StartupPath)
I am building a DLL that is used by Office. When Office runs with it, I would like to identify where it is located. Is that possible?
ex. of code within the DLL when it is run within Office:
// should return C:\tmp\officeaddin.dll,
// currently C:\Program Files\Microsoft Office\Office 12
MessageBox.Show(Application.StartupPath)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即使在 Office 内部运行,以下内容也应该有效:
获取当前程序集的路径
(它基本上可以归结为
Assembly.GetExecutingAssembly().Location
,但请参阅上面的链接以获取更多详细信息。)The following should work even if running inside of Office:
Getting the path of the current assembly
(It basically boils down to
Assembly.GetExecutingAssembly().Location
, but see the link above for more detailed information.)如果它是 .NET 库,则应使用
Assembly.GetExecutingAssembly().Location
。Application.StartupPath
显示主应用程序的路径。If it is a .NET library, you should use
Assembly.GetExecutingAssembly().Location
.Application.StartupPath
shows the path to the main app.这对我有用:
所以你可以为其设置一个字符串,例如:
This works for me:
So you could just set a string to it such as:
当 DLL 加载时,它使用实例句柄调用 DllMain。如果实现这个函数,就可以记录DLL的实例句柄。从此,您可以调用 GetModuleFileName 。
在 .NET 中,这已经为您处理好了。请参阅此问题< /a> 并选择答案以了解详细信息。
When the DLL loads, It calls DllMain with the instance handle. If you implement this function, you can then record the instance handle of the DLL. From this, you can then call GetModuleFileName.
In .NET this is taken care of for you. See this question and selected answer for details.