gpt4 book ai didi

delphi - Delphi IDE的 "Run without debugging"启动的进程继承了IDE的环境变量

转载 作者:行者123 更新时间:2023-12-03 15:04:03 25 4
gpt4 key购买 nike

我正在编写一个尝试调用 Apache ANT 脚本的 Windows GUI 应用程序。 ANT 脚本将构建 Delphi 项目组并将其打包到最终的 setup.exe 中。 ANT 脚本构建 Delphi 2007/2010 和 Delphi XE 应用程序。

我的 ANT GUI 应用程序是由 Delphi XE 编译和构建的。当我在 Delphi XE 中使用“运行而不调试”来运行应用程序时,应用程序无法与 ANT 脚本一起正常工作。 ANT 脚本不会构建 DCU、BPL 和 EXE 未编译到我期望的文件夹中的应用程序。

但是,如果我在 Windows 资源管理器中启动 GUI 应用程序,ANT 脚本会正确构建应用程序> 所有 DCU、BPL 和 EXE 均已构建并保存在我分配的文件夹中。

调试GUI应用程序后,我发现使用Delphi的“运行而不调试”运行的应用程序将从Delphi IDE继承环境变量。以下是我提取环境变量的方法:

function GetAllEnvVars(const Vars: TStrings): Integer;
var
PEnvVars: PChar; // pointer to start of environment block
PEnvEntry: PChar; // pointer to an env string in block
begin
// Clear the list
if Assigned(Vars) then
Vars.Clear;
// Get reference to environment block for this process
PEnvVars := GetEnvironmentStrings;
if PEnvVars <> nil then
begin
// We have a block: extract strings from it
// Env strings are #0 separated and list ends with #0#0
PEnvEntry := PEnvVars;
try
while PEnvEntry^ <> #0 do
begin
if Assigned(Vars) then
Vars.Add(PEnvEntry);
Inc(PEnvEntry, StrLen(PEnvEntry) + 1);
end;
// Calculate length of block
Result := (PEnvEntry - PEnvVars) + 1;
finally
// Dispose of the memory block
Windows.FreeEnvironmentStrings(PEnvVars);
end;
end
else
// No block => zero length
Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetAllEnvVars(Memo1.Lines);
end;

这是由 Delphi XE 的“运行而不调试”启动的进程的环境变量列表:

ActiveHostApplication=
ActiveProjectModule=
ALLUSERSPROFILE=C:\ProgramData
ANT_HOME=C:\Components\Components.d11\build.tool\apache-ant-1.7.1
ANT_OPTS=-Xmx512m
APPDATA=C:\Users\coder\AppData\Roaming
AQtime7_Product_Path=C:\Program Files (x86)\Automated QA\AQtime 7\Bin\
BDS=c:\program files (x86)\embarcadero\rad studio\8.0
BDSAppDataBaseDir=BDS
BDSBIN=c:\program files (x86)\embarcadero\rad studio\8.0\bin
BDSCOMMONDIR=C:\Users\Public\Documents\RAD Studio\8.0
BDSINCLUDE=c:\program files (x86)\embarcadero\rad studio\8.0\include
BDSLIB=c:\program files (x86)\embarcadero\rad studio\8.0\lib
BDSPROJECTSDIR=C:\Users\coder\Documents\RAD Studio\Projects
BDSUSERDIR=C:\Users\coder\Documents\RAD Studio\8.0
CG_BOOST_ROOT=C:\Program Files (x86)\Embarcadero\RAD Studio\8.0\include\boost_1_39
CommonProgramFiles=C:\Program Files (x86)\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=OBSERVER
ComSpec=C:\Windows\system32\cmd.exe
DELPHI=c:\program files (x86)\embarcadero\rad studio\8.0

这是 Windows 资源管理器启动的进程的环境变量列表:

ALLUSERSPROFILE=C:\ProgramData
ANT_HOME=C:\Components\Components.d11\build.tool\apache-ant-1.7.1
ANT_OPTS=-Xmx512m
APPDATA=C:\Users\coder\AppData\Roaming
BDSCOMMONDIR=C:\Users\Public\Documents\RAD Studio\5.0
CG_BOOST_ROOT=C:\Program Files (x86)\Embarcadero\RAD Studio\8.0\include\boost_1_39
CommonProgramFiles=C:\Program Files (x86)\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=OBSERVER
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO

比较 2 个环境变量列表,您可能会注意到 Windows Shell 启动的进程中不存在 BDSINCLUDE 和 BDSBIN 等少数变量。 Delphi XE IDE 中的那些环境变量影响了构建 Delphi 2007/2010 应用程序的 ANT 脚本。如果这些 Delphi IDE 环境变量没有在进程中显示,我的问题应该可以解决。

有谁知道是否可以从 Delphi IDE 启动不继承 Delphi IDE 环境变量的进程(无论是带调试运行还是不带调试运行)?

最佳答案

程序继承启动它的进程的环境。无论如何,Delphi 没有理由清理环境;程序应该为它们开始使用的任何任意环境做好准备。

如果您的程序在设置某些环境变量时失败,那么您需要让您的程序解决它。您可以使用 SetEnvironmentVariable 从程序环境中删除条目;传递一个空指针作为第二个参数。或者您可以找出为什么您的程序对看似不相关的变量如此敏感并更改它。

您还可以生成一个新环境并在程序启动 Ant 时使用它。将其作为第七个参数传递给CreateProcess。设置或删除您想要的任何值。

关于delphi - Delphi IDE的 "Run without debugging"启动的进程继承了IDE的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4383625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com