找到带有解决方案的帖子:如何处理DllImport 失败?
我正在编写一个应用程序,用于检查操作系统版本以根据主机使用的是 Vista 系列还是 NT 系列版本的 Windows 来执行不同的操作。如果是 Vista 系列,它会加载一些 DLL(使用 DllImport),但在其他情况下不使用这些。问题是,如果在没有这些 DLL 的旧版本 Windows 上使用,使用 DllImport 加载它们将在运行时导致 DllNotFoundException。
如何捕获/防止/忽略 DllNotFoundExceptions?尝试在未处理的异常事件中将异常设置为“已处理”不允许应用程序继续。
Found post with a solution: How do I handle a failed DllImport?
I'm writing an app that checks the OS version to do different things depending on whether the host is using a Vista-series or NT-series version of Windows. If Vista-series, it loads some DLLs (using DllImport), but doesn't use these otherwise. The problem is, using DllImport to load them will cause a DllNotFoundException at runtime if used on older versions of Windows that don't have those DLLs.
How can I catch / prevent / ignore the DllNotFoundExceptions? Trying to set the exception to "Handled" in my unhandled exception event does not allow the app to continue.
发布评论
评论(2)
我认为您应该能够使用 win32 LoadLibrary/GetProcAddress/FreeLibrary 和委托采用“传统”方式(就像使用回调函数一样)。
http://msdn.microsoft.com/en-us/library/d186xcf0。 aspx 可能是一个起点...
这应该可以帮助您开始:
然后您声明一个具有要调用的导出的正确签名的委托,并使用 Marshal.GetDelegateForFunctionPointer() 从您从 GetProcAddress。
I think you should be able to go the "traditional" way with the win32 LoadLibrary/GetProcAddress/FreeLibrary and a delegate (just as you do it with callback functions).
http://msdn.microsoft.com/en-us/library/d186xcf0.aspx might be a starting point...
This should get you started:
Then you declare a delegate with the correct signature of the export to be called, and use Marshal.GetDelegateForFunctionPointer() to create it from a function pointer you got back from GetProcAddress.
作为一种选择,您可以在 C++/CLI 中编写某种装饰器组件,该组件将使用 LoadLibrary 或静态链接将所有调用转发到 Windows dll,并手动处理这些 dll 的缺失。
As an option you could write some sort of decorator component in C++/CLI which would forward all calls to windows dlls using LoadLibrary or static linking and handle manually any absences of those dlls.