在MFC下编程时,用了windows的一个函数GetCurrentDirectory来获取当前进程的路径。msdn里是这么写的:Retrieves the current directory for the current process.
程序在Win7下运行的好好的,但是在XP下运行就出错。调试了一下发现是GetCurrentDirectory函数的问题。在XP下,当程序中用CFileDialog打开一个文件后,用GetCurrentDirectory获取到的路径却变成了刚打开文件的路径了,而不是进程的路径。通过逆向发现,在出现“打开文件”浏览的窗口的时候,每改变一下浏览的路径,系统就会调用SetCurrentDirectory函数一次,并且设置的路径就是当前浏览文件的路径。在Win7下没有这样的情况。这可能是XP系统的浏览对话框实现的方式所致。
msdn后面还有一段话:
Multithreaded applications and shared library code should not use theGetCurrentDirectory function and should avoid using relative path names. The current directory state written by theSetCurrentDirectory function is stored as a global variable in each process, therefore multithreaded applications cannot reliably use this value without possible data corruption from other threads that may also be reading or setting this value. This limitation also applies to the SetCurrentDirectory and GetFullPathName functions. The exception being when the application is guaranteed to be running in a single thread, for example parsing file names from the command line argument string in the main thread prior to creating any additional threads. Using relative path names in multithreaded applications or shared library code can yield unpredictable results and is not supported.
意思是多线程下用GetCurrentDirectory函数可能会不准确。当前目录可能会被SetCurrentDirectory函数轻易改变。故可以使用GetModuleFileName等函数来获取当前进程路径,再适当处理一下。
另:CreateProcess时,cmdline可以用相对路径,只要将要开启的EXE放在当前进程目录下即可。
路径“./dir/”里的'.'代表当前目录,和GetcurrentDirectory一样。