- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 .NET 应用程序,它是使用 ShellExecute 通过 Delphi 程序启动的。不幸的是,当以这种方式启动时,应用程序似乎无法正确读取其 app.config 文件,就好像该文件不存在一样。
我尝试过在其他场景中测试该应用程序,例如从快捷方式调用,并将工作目录设置为不同的文件夹,并且运行正常。
[编辑]Environment.CurrentDirectory 属性返回 Delphi 程序的目录。
任何想法将不胜感激。
干杯,
詹姆斯
最佳答案
显然,您生成的进程无法处理工作目录不属于它自己的事实。
您可以使用 CreateProcess() 打开该文件。我有一个关于等待的小例子(但你可以将其剪掉):
procedure ExecuteAndWaitFor(CommandLine, CurrentDirectory: string; Environment: TStrings);
var
List: TList;
ActiveWin: HWnd;
i: Integer;
Ret: Longword;
SI: TStartupInfo;
PI: TProcessInformation;
MadeForeground: Boolean;
AssociatedCommandLine: string;
begin
// find the association ot use
AssociatedCommandLine := GetAssociatedCommandLine(CommandLine);
// first we create a list of windows which we need to block...
List := TList.Create;
try
ActiveWin := Windows.GetForegroundWindow;
// get the list of all visible and active top windows...
if not Windows.EnumThreadWindows(GetCurrentThreadId,@InternallyThreadWindowCallback,Integer(List)) then RaiseLastOSError;
// disable all those windows...
for i := 0 to List.Count - 1 do Windows.EnableWindow(HWnd(List[i]),False);
try
// create the process
System.FillChar(SI,sizeof(SI),0);
SI.cb := sizeof(SI.cb);
// todo: environment
if not Windows.CreateProcess(nil,PChar(AssociatedCommandLine),nil,nil,False,NORMAL_PRIORITY_CLASS,nil,PChar(CurrentDirectory),SI,PI) then RaiseLastOSError;
// wait until the process is finished...
MadeForeGround := False;
repeat
// process any pending messages in the thread's message queue
Application.ProcessMessages;
if not MadeForeground then begin
Windows.EnumThreadWindows(PI.dwThreadId,@InternallyTopWindowToForeMost,Integer(@MadeForeGround));
end;
// wait for a message or the process to be finished
Ret := Windows.MsgWaitForMultipleObjects(1, PI.hProcess, False, INFINITE, QS_ALLINPUT);
if Ret = $FFFFFFFF then RaiseLastOSError;
until Ret = 0;
// free the process handle
Windows.CloseHandle(PI.hProcess);
WIndows.CloseHandle(PI.hThread);
finally
// enable all those windows
for i := 0 to List.Count - 1 do Windows.EnableWindow(HWnd(List[i]), True);
end;
Windows.SetForegroundWindow(ActiveWin);
finally
List.Free;
end;
end;
添加了一些缺失的实用函数:
uses
SysUtils, Registry;
function GetAssociatedFile(const Extension: string; const RemoveParameters: Boolean = False): string;
var
FileClass: string;
Reg: TRegistry;
Position: Integer;
begin
// initialize
Result := '';
// create registry entry
Reg := TRegistry.Create(KEY_EXECUTE);
try
// find the given extension
Reg.RootKey := HKEY_CLASSES_ROOT;
FileClass := '';
if Reg.OpenKeyReadOnly(ExtractFileExt(Extension)) then begin
FileClass := Reg.ReadString('');
Reg.CloseKey;
end;
if FileClass <> '' then begin
if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then begin
Result := Reg.ReadString('');
Reg.CloseKey;
end;
end;
finally
Reg.Free;
end;
// remove the additional parameters
Position := Pos('"%1"', Result);
if RemoveParameters and (Position > 0) then
Result := Trim(Copy(Result, 1, Position - 1))
else
Result := Trim(Result);
end;
function GetAssociatedCommandLine(const CommandLine: string): string;
begin
// build the command line with the associated file in front of it
Result := Trim(GetAssociatedFile(CommandLine, True) + ' ') + '"' + CommandLine + '"';
end;
function InternallyThreadWindowCallback(Window: HWnd; Data: Longint): Bool; stdcall;
var
List: TList;
begin
Result := True;
if (not IsWindowVisible(Window)) or (not IsWindowEnabled(Window)) then Exit;
List := TList(Data);
List.Add(Pointer(Window));
end;
function InternallyTopWindowToForeMost(Window: HWnd; Data: LongInt): Bool; stdcall;
begin
Result := True;
if (not IsWindowVisible(Window)) or (not IsWindowEnabled(Window)) then Exit;
SetForegroundWindow(Window);
PBoolean(Data)^ := True;
end;
关于.net - 通过 ShellExecute 调用应用程序时,未读取应用程序配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2206410/
我想使用 ShellExec 在“配置”模式下运行屏幕保护程序。我使用这个 (Delphi) 调用: i:= ShellExecute(0, 'open', PChar('c:\temp\test.
我需要使用 ShellExecute 从我的 VB6 代码执行一个外部程序:我面临的问题是,当路径字符串有任何空间时,它不起作用: Dim Path As String Path = "E:\PROY
我是shell脚本的新手,最近我在脚本中看到一个命令“:>文件”,我不明白它的意思,谁能告诉我这个命令在做什么? 提前致谢 最佳答案 :是一个空操作。 >是重定向,因此文件被 no-op 命令的输出覆
有了这个 ShellExecute(Handle, 'print', PChar(ExtractFilePath(ParamStr(0))+'Test.txt'), nil, nil, SW_H
我尝试通过delphi在shell中执行命令,但它不起作用。我使用这个脚本: var shellexecommand:string; begin ShellExecute(0, nil, 'cmd.e
我正在尝试使用ShellExecute在 Excel 中打开文件。我在MSDN论坛上阅读了有关该函数的信息,发现了以下有关句柄的信息,即第一个参数: “hwnd [in] 用于显示用户界面 (UI)
我有一个程序在执行 ShellExecute() 指令时崩溃。如果我使用 system() 方法,一切正常,但我读过 system() 是邪恶的...... 我使用的是 Visual Studio 2
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
例如我需要运行: ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE); 作为新线程,但我不知道如何。我试过这个: HANDLE
我的任务是每周检查一次数据库并检查所有链接。我通常在 PHP 中工作,但在 PHP 中这样做会非常慢(实际上它会在大约 100 个 URL 后使页面超时),所以我决定制作一个快速的 C++ 应用程序。
我正在使用 ShellExecute(NULL, L"open", szSomeDirectory, 0, 0, SW_SHOWDEFAULT) 在资源管理器中打开一个目录。 我正在尝试打开一个目录,
我正在使用 ShellExecute WIN API 来运行 DOS 命令,因为在我运行应用程序时会看到 DOS 命令闪烁。下面是 ShellExecute API 调用。 ret = ShellEx
Createprocess API 有一个选项可以创建带有 CREATE_SUSPENDED 标志的进程。相似之处是,ShellExecute API 是否有可能在挂起状态下创建进程。 最佳答案 没有
程序 1 使用 SetEnvironment 并调用 ShellExecute 来启动程序 2。程序 2 调用 GetEnvironment 并从中检索值。令人惊讶的是,正如我在 MSDN 中读到的那
我正在学习如何使用 Python 打印文件。我找到了很多方法来做到这一点,我见过的最常见的方法之一是使用 win32api 模块。 import win32api win32api.ShellExec
成功时,ShellExecute 返回一个句柄。 我们是否需要关闭这个句柄,如果需要,如何关闭? 根据我微软公布的例子,我们不需要关闭这个句柄。但是 ShellExecute 本身的文档对这个主题保持
我有一个 VB6 应用程序,它使用以下方法打开文件及其关联的应用程序: ShellExecute(0, "open", filename, params, vbNullString, vbNormal
我计划使用 ShellExecute 作为用 delphi 2007 编写的应用程序的更新程序,我想知道 ShellExecute 是否可以在大多数使用 Win 的计算机上运行,或者是否会因为某些
我有一个 Delphi 应用程序,它使用 ShellExecute 在按下按钮时调用第二个 Delphi 应用程序。 应用程序存储在同一服务器上的同一网络共享上。它们的路径格式如下: const
我有一个希望很快的问题:是否可以稍微延迟 ShellExecute 的执行? 我有一个带有自动更新程序的应用程序。下载所有必需的文件等后,它将当前文件重命名为 *.OLD,并将新文件重命名为以前的文件
我是一名优秀的程序员,十分优秀!