需求:
    创建一个虚拟的文件,有虚拟的物理地址和数据。
    然后,在其他程序中,直接通过操作文件的API就能读取这个文件    最好是,在读取的时候,才真实加载文件的数据到内存中,因为要虚拟的文件很多、很大在网上查了半天,好像只有内存映射文件有类似的功能,具体的细节也不是很清楚,比如,A程序创建一个C:/Tmp.txt映射文件,B程序能用普通的文件操作打开并读取数据吗。另外,还有没有比较好的办法,实现这项功能呢

解决方案 »

  1.   

    在网上查了半天,好像只有内存映射文件有类似的功能,具体的细节也不是很清楚,比如,A程序创建一个C:/Tmp.txt映射文件,B程序能用普通的文件操作打开并读取数据吗。
    可以
      

  2.   

    Creating Named Shared Memory
      

  3.   


    刚才看到一些人说只有HOOK API的办法,才能实现呢,我想确认一下,上面说的B程序是一个三方软件,我是不能直接修改B程序的
      

  4.   

    内存映射文件好像不能 直接通过操作文件的API就能读取这个文件 吧
    需要 打开内存映射文件对象,MapViewOfFile 映射到自己的内存,然后使用也是直接使用的
    直接通过操作文件的API就能读取这个文件 应该是CreateFile WriteFile 之类的函数吧
    HOOK API 和内存映射文件一起用就好了....  或者驱动下写个近似真实的文件
      

  5.   

    Creating Named Shared Memory
    To share data, multiple processes can use memory-mapped files that are backed by the system paging file.First ProcessThe first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file views that are created. Then the process uses the file mapping object handle that CreateFileMapping returns in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view.When the process does not need access to the file mapping object, it should call the CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses.
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>#define BUF_SIZE 256
    TCHAR szName[]=TEXT("MyFileMappingObject");
    TCHAR szMsg[]=TEXT("Message from first process");void main()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;   hMapFile = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    // use paging file
                     NULL,                    // default security 
                     PAGE_READWRITE,          // read/write access
                     0,                       // max. object size 
                     BUF_SIZE,                // buffer size  
                     szName);                 // name of mapping object
     
       if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE) 
       { 
          printf("Could not create file mapping object (%d).\n", 
                 GetLastError());
          return;
       }
       pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                            FILE_MAP_ALL_ACCESS, // read/write permission
                            0,                   
                            0,                   
                            BUF_SIZE);           
     
       if (pBuf == NULL) 
       { 
          printf("Could not map view of file (%d).\n", 
                 GetLastError()); 
          return;
       }   
       CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
       getch();   UnmapViewOfFile(pBuf);   CloseHandle(hMapFile);
    }
    Second ProcessA second process can access the same data by calling the OpenFileMapping function with the same name as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view. 
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>#define BUF_SIZE 256
    TCHAR szName[]=TEXT("MyFileMappingObject");void main()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;   hMapFile = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object 
     
       if (hMapFile == NULL) 
       { 
          printf("Could not open file mapping object (%d).\n", 
                 GetLastError());
          return;
       } 
     
       pBuf = MapViewOfFile(hMapFile,    // handle to mapping object
                   FILE_MAP_ALL_ACCESS,  // read/write permission
                   0,                    
                   0,                    
                   BUF_SIZE);                   
     
       if (pBuf == NULL) 
       { 
          printf("Could not map view of file (%d).\n", 
                 GetLastError()); 
          return;
       }   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);   UnmapViewOfFile(pBuf);   CloseHandle(hMapFile);
    }
      

  6.   

    看来是没什么好办法了,需求的是B程序使用物理路径访问虚拟的文件,FileMapping做不到