Long time not kodil on Delphi. Trying to run the file to view:

ShellExecute(Handle, 'open', 'notepad', PChar('%systemroot%\system32\drivers\etc\hosts'), Pchar('%WinDir%'), SW_SHOW); 

It seems to do everything right. But only the notebook opens (new file) and that's it. Hint what's wrong here.

  • What is a minus? If no one answered the question! - I_CaR

4 answers 4

Or maybe just do not have access to the file? On the other hand, who promised that% systemroot% will be deployed? In the MSD did not find this. But also to the eyeballs, and who will check the return code of the function? she can tell a lot.

  • The ex option can optionally make substitutions, and legacy of course does not. - karmadro4

The solution prompted the old my question on delphikingdom

 GetWindowsDirectory(WinPath, 255); WinDir := StrPas(WinPath); 

and further

 ShellExecute(Handle, 'open', 'notepad', PChar(WinDir+'\system32\drivers\etc\hosts'), Pchar(WinDir), SW_SHOW); 

and here is the result:

  • CTD. See answer @KoVadim. - Nofate

If you run the file from the Windows directory, then the optional parameters are optional:

 shellexecute(Handle,'Open',Pchar('notepad.exe'),0,0,SW_SHOWNORMAL); 

In other cases, you can do this (for example, with the Windows directory):

 ... var s:string; ... setlength(s,255); SHGetSpecialFolderPath(handle,Pchar(s),$24,false); s:=IncludeTrailingBackSlash(Pchar(s)); shellexecute(Handle,'Open',Pchar(s+'notepad.exe'),0,0,SW_SHOWNORMAL); 
  • How is it not necessary? Why should I just run bluetooth? See carefully the question! - I_CaR
  • SHGetSpecialFolderPath (handle, Pchar (s), $ 24, false) is cumbersome. But it worked, if you naturally register another parameter: shellexecute (Handle, 'Open', Pchar (s + 'notepad.exe'), Pchar (s + '\ system32 \ drivers \ etc \ hosts'), 0, SW_SHOWNORMAL); - I_CaR
 ShellExecute( Application.Handle, 'open', 'c:\windows\notepad.exe', 'c:\file_name.txt', nil, SW_SHOWNORMAL ); 

so as not to be hacked: D

 ShellExecute( Application.Handle, 'open', 'notepad.exe', 'c:\1.txt', nil, SW_SHOWNORMAL ); 

but the path to the hosts need to register completely

  • 2
    And if, say, the system is not installed on the C drive, but on the D drive? What then? That's how the programs break down ... - AseN
  • Such a program will not break, it makes no sense) - Malus
  • one
    He did not mean hacking the program, but the fact that it will not just work in your version with the absolute path to the host! - I_CaR