Can not get msconfig .exe to run in the system32 folder shellexecute Delphi
Pregunta
Im on Win 7 64b. Im trying to run msconfig from my delphi app. The msconfig.exe file is in the system32 folder . I copied the msconfig.exe to the c:\ and it works great. This looks like some kind of permission issue.
var
errorcode: integer;
begin
errorcode :=
ShellExecute(0, 'open', pchar('C:\Windows\System\msconfig.exe'), nil, nil, SW_NORMAL);
if errorcode <= 32 then
ShowMessage(SysErrorMessage(errorcode));
end;
Has anyone seen this and figured out how to run the msconfig.exe from the sys32 .
Solución
This behavior is caused by the File System Redirector
as workaround you can use the Wow64DisableWow64FsRedirection
and Wow64EnableWow64FsRedirection
functions.
{$APPTYPE CONSOLE}
uses
ShellAPi,
SysUtils;
Function Wow64DisableWow64FsRedirection(Var Wow64FsEnableRedirection: LongBool): LongBool; StdCall;
External 'Kernel32.dll' Name 'Wow64DisableWow64FsRedirection';
Function Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection: LongBool): LongBool; StdCall;
External 'Kernel32.dll' Name 'Wow64EnableWow64FsRedirection';
Var
Wow64FsEnableRedirection: LongBool;
begin
try
Wow64DisableWow64FsRedirection(Wow64FsEnableRedirection);
ShellExecute(0, nil, PChar('C:\Windows\System32\msconfig.exe'), nil, nil, 0);
Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Otros consejos
To access the 64-bit System folder from a 32-bit process, you should use the special ”SysNative" alias instead of the ”System32" folder directly:
PChar('C:\Windows\SysNative\msconfig.exe')
If you need to support 32-bit OS versions or 64-bit compiling, use IsWow64Process()
to detect if your app is running under WOW64:
{$IFDEF WIN64}
function IsWow64: Boolean;
begin
Result := False;
end;
{$ELSE}
function IsWow64Process(hProcess: THandle; out Wow64Process: BOOL): BOOL; stdcall; external 'kernel32.dll' delayed;
function IsWow64: Boolean;
var
Ret: BOOL;
begin
Result := False;
// XP = v5.1
if (Win32MajorVersion > 5) or
((Win32MajorVersion = 5) and (Win32MinorVersion >= 1)) then
begin
if IsWow64Process(GetCurrentProcess(), Ret) then
Result := Ret <> 0;
end;
end;
{$ENDIF}
var
errorcode: integer;
SysFolder: string;
begin
If IsWow64 then
SysFolder := 'SysNative'
else
SysFolder := 'System32';
errorcode := ShellExecute(0, 'open', PChar('C:\Windows\'+SysFolder'+\msconfig.exe'), nil, nil, SW_NORMAL);
if errorcode <= 32 then
ShowMessage(SysErrorMessage(errorcode));
end;
If you're building a 32bit Delphi app, then when it runs on 64bit Windows, the System32 folder is actually remapped. To a 32bit application, System32 is actually SysWOW64. Because you "see" it in System32 from the Explorer or cmd.exe, is because those are 64bit processes. In this case, a 32bit process cannot "see" the actual 64bit System32 folder.
One solution is to get the latest Delphi which supports 64bit targeting and build a 64bit version.