偶然被人问到OpenFile和CreateFile路径的问题,查了MSDN,然后做了一下Debug,记录一下心得
首先OpenFile是一个16-bit windows 的函数,现在再写新的程序应该用CreateFile来代替。
有一些老的code可能还是用的OpenFile来打开文件,如果现在升级到CreateFile,就需要注意路径的问题了。
OpenFile 可以只给一个文件名称,然后会有相应的路径搜索顺序:
The directory where an application is loaded.
The current directory.
The Windows system directory.
The 16-bit Windows system directory.
There is not a function that retrieves the path of this directory, but it is searched.
The Windows directory.
The directories that are listed in the PATH environment variable.
在kernel32!openFile里面会调用到kernel32!SearchPath 函数来查找,这个行为是和loadlibrary一致的。
比如在c:\windows\system32下面有一个sicl32.dll的文件
如果用OpenFile("sicl32.dll"....)是可以打开成功的,因为里面会搜索到c:\windows\system32目录
但是用CreateFile("sicl32.dll"...)会失败,必须以“c:\\windows\\system32\sicl32.dll”为参数才能成功。