-2

I need the functionality of C++ function "System()" in my Pascal program. Is there any possible way of using it or sth similar?

For example I want to imitate the C++ function:

System("COLOR fc");
Hooch
  • 28,817
  • 29
  • 102
  • 161
  • 1
    http://wiki.freepascal.org/Executing_External_Programs. Note that you might need to execute "cmd /c color fc". – Jim Mischel Dec 30 '12 at 00:44
  • @JimMischel I don't want to create external process. I want to execute command in my programs console. – Hooch Dec 30 '12 at 00:50

3 Answers3

3

The C++ library function system starts a new process. It looks to me like the FreePascal function SysUtils.ExecuteProcess will do what you want.

Your other alternative, if all you really want to do is change the background color on the console, is to call the Windows API function SetConsoleTextAttribute.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I was using SetConsoleTextAttribute. But this function can't set console background to pure white like "COLOR arg". – Hooch Dec 30 '12 at 12:42
  • @Hooch: "is not working for me" doesn't give us much to go on. I suggest that you either update your question here, or make a new question that shows the code you're using, and the error message you're getting. – Jim Mischel Dec 30 '12 at 15:45
  • executeprocess is a createprocess derivative, not a shellexecute derivative. IOW it will only execute binaries, and only with full path, see also my other answer. – Marco van de Voort Jan 14 '13 at 14:34
1

The problem is a bit complicated. There are several ways to approach this.

  1. On *nix, system is available as unix.fpsystem(). (though not strictly 100% the same as the libc system function)

  2. On Windows, FPC is native, and doesn't work through a POSIX or C library layer. Therefore there is no system() emulation on Windows.

  3. sysutils.Executeprocess abstracts the Windows "createprocess(ex)" and the nix Exec() functions. This is not for shell expansion though, and needs full path to the binary to execute (e.g. by using fsearch or the equivalent Delphi function)

  4. Process.TProcess is another, more involved such abstraction, accessing more features (like piping). New in the process unit in 2.6.2 are Runcommand functions which are simple tprocess wrappers for common cases.

  5. Further, the problem is that there are many different ways to do something like this on Windows, so what to pick?

    1. The old and cross-Windows version portable way: search command.com and execute with /C
    2. old/console style, the NT way, use COMSPEC to launch cmd.exe with /C
    3. The GUI way, use shellexecute(). Mainly used to start browsers and other applications associated with extensions.
    4. Create your own, search PATH, expand environmentvariables,parse cmdline yourself and then call the basic createprocess() routines.

In general I would use shellexecute unless it really is commandline stuff (like this specific case), then I would use method 5.2.

Added later, verified code for executeprocess (method 5.2)

uses 
 Sysutils;
begin
  ExecuteProcess(getenvironmentvariable('COMSPEC'), ['/C','COLOR fd']);
end.

Note that I pass the command (color fd) as one parameter, though afaik that is more important in the *nix case.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • Can you tell me littole bit more on how to use method 2? I would only like to use that "COLOR arg" command to change console bacground to PURE WHITE. Pascal color chaning functions are making backgrount grey, not white. – Hooch Dec 31 '12 at 00:05
  • Please add the code that doesn't make the background white to your question. I added code for method 5.2 – Marco van de Voort Dec 31 '12 at 14:36
  • Your code gives me error: Wrong number of parameters specified for call to "GetEnvironmentVariable". But replacing GetEnvVar with 'cmd.exe' executes. But it does nothing. ExecuteProcess('cmd.exe', ['/C','COLOR fd']); – Hooch Dec 31 '12 at 15:00
  • Very strange. This works for me on 2.6.0 and trunk, both win32 and win64. Moreover, I can't really imagine why it wouldn't work (since all used functions are at least 2.2.0+, which is already considered a stone-age version. – Marco van de Voort Dec 31 '12 at 19:13
  • I see that you are good in Pascal. Can you help me with that one problem? Can I contact you via mail somewhow? – Hooch Jan 01 '13 at 12:13
  • If you want mailsupport, best subscribe to the FPC or Lazarus maillists. But there are also they will want you to provide more info. – Marco van de Voort Jan 01 '13 at 20:32
  • I really need that fixed. Can you tell me what more info you need to help me? – Hooch Jan 02 '13 at 02:05
0

system() invokes the platform-specific command-line processor. On Windows, for example, system("command") calls CreateProcess() to invoke %COMSPEC% /C "command", where %COMSPEC% is either command.com or cmd.exe depending on Windows version.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770