简体   繁体   English

C ++添加带有文件的文件夹

[英]C++ add folder with files

I have a very old and big project, which calls different EXE files during simulation. 我有一个非常大的项目,在仿真过程中会调用不同的EXE文件。 All of them are placed in one folder. 所有这些都放置在一个文件夹中。 For now, the folder directory was in the path env variable, but unfortunately I had to remove it from there. 现在,文件夹目录位于路径env变量中,但是不幸的是我不得不从那里删除它。

Is there a way to add a folder to the list where process is searching for files? 有没有一种方法可以将文件夹添加到进程正在搜索文件的列表中? I know it could be done for DLLs by SetDllDirectory but this doesn't work for EXEs. 我知道可以通过SetDllDirectory对DLL进行此操作,但这不适用于EXE。

If this can't be done, is it possible to change working directory for thread (not for process)? 如果不能这样做,是否可以更改线程的工作目录(而不是进程的目录)?

On starting of a new process (EXE), Windows creates a copy of the environment variables buffer of parent process for the started new process. 在启动新进程(EXE)时,Windows将为启动的新进程创建父进程的环境变量缓冲区的副本。 All changes on environment variables of started process have therefore no effect on the environment variables of the parent process. 因此,已启动进程的环境变量的所有更改都不会影响父进程的环境变量。 In other words Windows makes a malloc + memcpy to create a new environment variables table for the started process. 换句话说,Windows创建了一个malloc + memcpy来为启动的进程创建一个新的环境变量表。

So in your application you can use GetEnvironmentVariable to load the string value of local copy of PATH . 因此,在您的应用程序中,您可以使用GetEnvironmentVariable加载PATH的本地副本的字符串值。

Next check if this string contains already the path of directory containing the EXEs to call (from a previous call of your application). 接下来,检查该字符串是否已经包含包含要调用的EXE的目录路径(来自您的应用程序的上一次调用)。 Nothing to do if PATH contains already this path. 如果PATH已经包含此路径,则无需执行任何操作。

Otherwise check if PATH string ends already with a semi-colon. 否则,请检查PATH字符串是否已经以分号结尾。 If this is not the case append a semi-colon. 如果不是这种情况,请添加分号。 Then append the path of the directory containing the EXEs to call. 然后附加包含要调用的EXE的目录的路径。 Finally, update process copy of environment variable PATH by updated string using SetEnvironmentVariable . 最后,使用SetEnvironmentVariable通过更新的字符串更新环境变量PATH的过程副本。

Take a look on the Changing Environment Variables example code. 查看“ 更改环境变量”示例代码。

Now with updating the PATH environment variable for your application (process) only, calling or starting the other executables should work. 现在,仅更新应用程序(进程)的PATH环境变量,调用或启动其他可执行文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM