是否可以捕获“未找到文件”的信息?从另一个进程然后将文件返回到该进程?

发布于 2024-09-04 08:59:43 字数 160 浏览 2 评论 0原文

我有一个旧应用程序,用于在目录中查找文件。它不能很好地处理丢失的文件。我想要做的是“捕获”文件未找到错误,并将另一个文件发送回调用应用程序。类似于如何处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

能怎样 2024-09-11 08:59:43

您可以通过拦截对 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

迷离° 2024-09-11 08:59:43

如果商业解决方案可以接受,那么像 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.

夜吻♂芭芘 2024-09-11 08:59:43

是的,你所描述的情况是可能的。

我建议对此类型使用文件系统筛选器驱动程序或迷你筛选器但在 .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.

浅笑轻吟梦一曲 2024-09-11 08:59:43

如果您知道要打开文件的进程中的代码在哪里,则可以在其周围编写一个包装进程,该进程充当调试器,拦截调用,检查它是否存在,如果不存在,则替换文件名与另一个不同的。

类似于:

CreateProcess(bla bla, DEBUG_ONLY_THIS_PROCESS, bla bla);

SetBreakPoint(address of code to set breakpoint)
{
  ReadProcessMemory to save off byte for breakpoint
  WriteProcessMemory 0xCC to set breakpoint
  FlushInstructionCache
}

while (TRUE == bContinue)
{
  bContinue = WaitForDebugEvent(&debugEvent);
  switch (dwDebugEventCode)
  {
    case EXCEPTION_BREAKPOINT:
      // Read the file name from memory, check if it exists, if not, replace it with
      // new file name using the same length in memory :)
      // Replace your code byte you read out when you set the breakpoint
  }
}

另一种方法是用您自己对 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:

CreateProcess(bla bla, DEBUG_ONLY_THIS_PROCESS, bla bla);

SetBreakPoint(address of code to set breakpoint)
{
  ReadProcessMemory to save off byte for breakpoint
  WriteProcessMemory 0xCC to set breakpoint
  FlushInstructionCache
}

while (TRUE == bContinue)
{
  bContinue = WaitForDebugEvent(&debugEvent);
  switch (dwDebugEventCode)
  {
    case EXCEPTION_BREAKPOINT:
      // Read the file name from memory, check if it exists, if not, replace it with
      // new file name using the same length in memory :)
      // Replace your code byte you read out when you set the breakpoint
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文