Delphi IDE“Run without debugging”启动的进程继承了IDE的环境变量

时间:2010-12-08 02:17:14

标签: delphi

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

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

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

调试GUI应用程序后,我发现使用Delphi的“Run without debugging”运行应用程序将从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继承环境变量的进程(有或没有调试运行),是否有人有想法?

2 个答案:

答案 0 :(得分:9)

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

如果您的程序在设置某些环境变量时失败,那么您需要让程序对其进行寻址。您可以使用SetEnvironmentVariable删除程序环境中的条目;传递空指针作为第二个参数。或者你可以弄清楚为什么你的程序对看似无关的变量如此敏感并改变它。

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

答案 1 :(得分:6)

感谢Rob Kennedy的提示。我试图逐个使用SetEnvironmentVariable来变量。 Delphi IDE强加的环境变量Platform = Win32之一导致了这个问题。

SetEnvironmentVariable('Platform', nil);

将平台设置为nil使ANT构建按预期工作。