假如有一个工程需要一幅位图,那我可以将它作为资源文件加入我的工程,同时我也可以不加如工程,而将其作为一般文件在需要的时候读取.这都可以用LoadImage()函数实现,可是这两种对文件处理的方式有什么不同吗?
    请大家发表一下看法吧.

解决方案 »

  1.   

    谢谢大家了,再问一下:当程序运行时,资源文件是在需要的时候才加载到内存,还是在程序开始后就直接加载到内存去等待调用?
    -----------------------------------------------------------------------------------看书刚看到这部分:)
    这实际上是运用了memory-mapped file技术,我们知道windows有虚拟内存技术,对应用程序来讲可以使用的内存比实际的physical memory要大得多,系统将目前不用的数据先交换到paging file中,等此数据再次被访问的话,在load到physical内存中。由于效率和空间的考量,对于.exe和.dll文件,hard driver上的支持文件则不再是paging file,而是.exe 或.dll本身。也就是系统将.exe或者.dll的地址映射到virtal address space上。(下面是我的理解)因此系统运行.exe的代码就好像他们已经在内存中一样,如果发现要执行的代码不再physical memory中则从.exe或者.dll image中载入代码到physical memory中。载入的单位应该是page的大小,windows2000在32位 intel cpu上page的单位是4K bytes.
      

  2.   

    呵呵,下面的内容支持了我的理解:After all the .exe and DLL files have been mapped into the process's address space, the system can begin executing the .exe file's startup code. After the .exe file has been mapped, the system takes care of all the paging, buffering, and caching. For example, if code in the .exe causes it to jump to the address of an instruction that isn't loaded into memory, a fault will occur. The system detects the fault and automatically loads the page of code from the file's image into a page of RAM. Then the system maps the page of RAM to the proper location in the process's address space and allows the thread to continue executing as though the page of code were loaded all along. Of course, all this is invisible to the application. This process is repeated each time any thread in the process attempts to access code or data that is not loaded into RAM.
      

  3.   

    谢谢大家,我总结一下:
        1.资源文件每次编译都要,相当于静态连接,在运行时加载速度快;按findcaiyzh(秋心)的说法是:资源文件实际还是在硬盘上,但是已经把资源文件映射到虚拟内存中,如果到需要文件时就看它在不在物理内存中,不在就加载,说到底还是在硬盘中,只不过加载是速度比我直接读取硬盘文件快;就这样来说,对yxq123(无语)所说的"一运行就加载资源文件"的理解就应该是完成硬盘文件到虚拟内存的映射而已.
        2.如果直接读取文件就是在程序运行到需要时才从硬盘上搜索(没有现成的地址,我想这应该是比较慢的原因了吧).    也就是说如果一个工程需要外围文件的量很大,那就不应该把它们加入资源文件,而是在需要的那个文件时再加载它,资源文件比较适合于占空间小的文件.相应揭帖号召,如果没有弄错的话晚上就揭帖了.