加载托管 c++ 时dll 使用 Assembly.LoadFrom() 接收文件未找到异常
我正在使用 VSTO 为 Microsoft 项目开发一个加载项,并且我有一个托管 c++ dll,它包装了一个非托管 c++ dll。我需要部署两个版本的托管 c++ dll,一种用于 64 位,一种用于 32 位。我正在使用 Assembly.LoadFrom(path) 加载适当的 dll,具体取决于我运行的 Office 版本。这一切似乎在我的开发机器上运行良好,这是一台运行 64 位 Office 的 64 位机器。下面是有问题的代码:
try
{
//This will return true so I know the file exists
bool fileExists = File.Exists(path);
//This will throw a file not found exception
keyModAssembly = Assembly.LoadFrom(path);
}
catch (FileNotFoundException e)
{
}
我已经三次检查了路径和文件是否存在并且是正确的 32 位 dll。这一切在我的 64 位机器上运行良好,但当我尝试测试 32 位版本时在 xpmode 下失败。
任何建议将不胜感激。
提前致谢。
编辑
为了响应 Phillip 关于可能找不到非托管 dll 的建议,我使用 LoadLibraryW(path) 将非托管 dll 加载到范围中。
// this is not throwing an exception so I know the unmanaged dll is there.
if (!File.Exists(unmanagedPath))
throw new FileNotFoundException();
LoadLibraryW(unmangedlibPath);
I am developing an add-in using VSTO for Microsoft project and I have a managed c++ dll that wraps an un-managed c++ dll. I need to deploy two versions of the managed c++ dll one for 64 bit and one for 32 bit. I am using Assembly.LoadFrom(path) to load the appropriate dll depending on which version of Office I am running from. This all seems to work fine on my development machine which is a 64 bit machine running 64 bit office. Below is the code in question:
try
{
//This will return true so I know the file exists
bool fileExists = File.Exists(path);
//This will throw a file not found exception
keyModAssembly = Assembly.LoadFrom(path);
}
catch (FileNotFoundException e)
{
}
I have triple checked the path and the file exists and is the correct 32 bit dll. This all works fine on my 64 bit machine but fails in xpmode when I am trying to test it for 32 bit versions.
any suggestions would be greatly appreciated.
Thanks in advance.
Edit
In response to Phillip's suggestion about the unmanaged dll possibly not being found I am loading the unmanaged dll into scope using the LoadLibraryW(path).
// this is not throwing an exception so I know the unmanaged dll is there.
if (!File.Exists(unmanagedPath))
throw new FileNotFoundException();
LoadLibraryW(unmangedlibPath);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
包装器程序集(我假设是您的示例中所指向的路径)是否已正确加载,但未找到它引用的本机 DLL?您没有在代码中检查该内容。通常,错误消息会显示“未找到程序集或其依赖项之一”。
调查此问题的一个好方法是使用 SysInternals 工具中的 procmon 来监视文件系统访问(这将告诉您系统在哪里寻找 32 位 DLL)或使用 procmon Windows 中的 >Fashion 设施。
Could it be that the wrapper assembly (which I assume is what path points to in your example) is loaded correctly, but the native DLL which it references is not found? You are not checking for that one in your code. Often the error message says 'Assembly or one of its dependencies is not found'.
A good way to investigate this is to use either procmon from the SysInternals tools to monitor the file system access (which will tell you where the system is looking for your 32-bit DLL) or use the Fushion facilities in Windows.