是否可以捕获“未找到文件”的信息?从另一个进程然后将文件返回到该进程?
我有一个旧应用程序,用于在目录中查找文件。它不能很好地处理丢失的文件。我想要做的是“捕获”文件未找到错误,并将另一个文件发送回调用应用程序。类似于如何处理 Web 服务器上的 404 错误并根据请求的 URL 返回某些内容,但本地文件系统除外。
这可能吗?更优选的是,在.Net 中可能吗?
I have a legacy application that looks for files in a directory. It does not handle missing files very well. What I want to do is "capture" the file not found errors, and send another file back to the calling app instead. Similar to how you could handle a 404 error on a webserver and return something based on what the requested URL was, except on the local file system.
Is this possible? And more preferably, is it possible in .Net?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过拦截对 Win API 函数 CreateFile 的调用来完成此操作。这就需要dll注入。在 .NET 中,您可以使用此库:easyhook.codeplex.com
You can do this by intercepting the call to Win API function CreateFile. This requires dll injection. In .NET you can use this library: easyhook.codeplex.com
如果商业解决方案可以接受,那么像 Eldos CallbackFilter 这样的解决方案可能符合要求:
http://www .eldos.com/cbflt/spec.php
我没有将它用于您的目的,但您当然可以拦截文件系统调用,您可以在其中检查文件是否存在,如果不存在则创建一个虚拟文件。
对于整理行为不良的遗留应用程序来说,这可能会更加简单。
If a commercial solution is acceptable, then something like Eldos CallbackFilter might fit the bill:
http://www.eldos.com/cbflt/spec.php
I haven't used it for exactly your purposes, but you can certainly intercept file system calls, where you could check if the file exists and create a dummy one if none exists.
This might prove to be a lot more straightforward for sorting out a badly behaving legacy application.
是的,你所描述的情况是可能的。
我建议对此类型使用文件系统筛选器驱动程序或迷你筛选器但在 .Net 中无法完成的事情。
我建议的方法可能是在文件系统级别捕获所有内容的最正确方法。
Yes what you described is possible.
I would suggest using a filesystem filter driver or mini filter for this type of thing though which can't be done in .Net.
The way I am suggesting is probably the most proper way that catches everything at the filesystem level.
如果您知道要打开文件的进程中的代码在哪里,则可以在其周围编写一个包装进程,该进程充当调试器,拦截调用,检查它是否存在,如果不存在,则替换文件名与另一个不同的。
类似于:
另一种方法是用您自己对 CreateFile 的调用(或他们在相关应用程序中使用的任何内容)覆盖函数调用表。查找 API 挂钩,甚至 Dll 注入可能会帮助您。
Microsoft 有 Detours 包可以帮助您,CodePlex 有EasyHook 看起来很有趣。
If you know where the code in the process that is going to open the file, you could write a wrapper process around it that acts like a debugger, intercept the call, check to see if it exists yourself, and if not, replace the filename with a different one.
Something like:
Another method is to overwrite the function call table with your own call to CreateFile (or whatever they are using in the app in question). Look up API hooking, or even Dll injection may help you out here.
Microsoft has the Detours package that can help you out, and CodePlex has the EasyHook that looks quite interesting.