Recent

Author Topic: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);  (Read 3069 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 278
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« on: September 18, 2023, 03:01:18 pm »
I'm migrating a Delphi 7 app I wrote years ago to Lazarus Free Pascal.  A lot easier than I thought starting this little project. But, can't find function WinExec() in Lazarus or a similar function. See the subject.  Can anyone advise? Also, is there anywhere to go for this discussion, Delphi 7 --> Lazarus Free Pascal?

Tks,
Donald

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11984
  • FPC developer.
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #1 on: September 18, 2023, 03:17:53 pm »
Winexec is a Windows function from the windows 3.11 era. It has been replaced in win9x with CreateProcess and/or shellexecute depending on what you exactly want to do.

Delphi has some legacy stuff for Windows 3.11 that never were in FPC because the windows port was directly for win9x The current windows 3.11 came later and is a different codebase.

In general for things like this there are few general rules. Googling on the subject and a bit of studying is best.


rvk

  • Hero Member
  • *****
  • Posts: 6640
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #2 on: September 18, 2023, 03:26:29 pm »
Although it is deprecated.... It is still in there (even in trunk) and still works (even on Windows 64 bit).

Just add Windows to your uses.

Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. begin
  5.   WinExec('CALC.EXE', 1);
  6. end;


Code: Pascal  [Select][+][-]
  1. // Winexec is a win16 legacy function, deprecated for 17 years (deprecated since Windows 95).
  2. // Annotated the MS deprecated status with a FPC deprecated directive so it won't be used for new development.
  3. function WinExec(lpCmdLine:LPCSTR; uCmdShow:UINT):UINT; external 'kernel32' name 'WinExec'; deprecated;

So, nice to just try but if you are really going to use it, read about the newer functions like Marco suggested.
« Last Edit: September 18, 2023, 03:29:05 pm by rvk »

440bx

  • Hero Member
  • *****
  • Posts: 4887
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #3 on: September 18, 2023, 03:37:27 pm »
It would also be a good idea for the OP to read about all the function's "idiosyncrasies"  which are found at: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winexec

IOW, OP, avoid unpleasant surprises by not using the function unless you know what you're doing.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 2792
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #4 on: September 18, 2023, 03:57:02 pm »
IOW, OP, avoid unpleasant surprises by not using the function unless you know what you're doing.
.... which considering he posted in "Installation".....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

BasicOne

  • New Member
  • *
  • Posts: 16
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #5 on: September 18, 2023, 04:21:56 pm »
you could use something with tProcess...

...or a really quick and dirty ShellExec on Windows:

Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2.  
  3. function ShellExecuteA(hWnd: tHandle; Operation, FileName, Parameters, Directory: PChar; ShowCmd: integer): integer; stdcall; external 'shell32.dll' Name 'ShellExecuteA';
  4.  
  5. {$ENDIF}
  6.  
  7. function myCommandline.SendCommandShellExec(fileName, Parameters: ansistring): boolean;
  8. begin
  9.  Result := False;
  10.   {$IFDEF WINDOWS}
  11.  if self.fHideWindow then
  12.   Result := (ShellExecuteA(0, PChar('open'), PChar(ExtractFileName(fileName)), PChar(Parameters), PChar(ExtractFilePath(fileName)), 0) > 32)
  13.  else
  14.   Result := (ShellExecuteA(0, PChar('open'), PChar(ExtractFileName(fileName)), PChar(Parameters), PChar(ExtractFilePath(fileName)), 5) > 32);
  15.   {$ENDIF}
  16. end;
  17.  
  18.  

I use this in a "myCommandline" class to start external processes. In this class, there is a call using the complex tProcess with output-pipes. And there is this simple funktion calling ShellExecute of the Windows API).

With ShellExecute, Windows starts the application according th file extension, e.g. the application Calc.exe or e.g. some registered pdf-reader if fileName is something like Document.pdf

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 278
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #6 on: September 18, 2023, 11:19:12 pm »
Okay everyone,
     I have quite the learning curve from way back long time ago, LoL! Anyway thanks alot for all the great suggestions.
Donald aka 1HuntnMan

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #7 on: September 19, 2023, 01:50:28 am »
@BasicOne, your approach might work right now but not in future, if you call an A (Ansi) method please use also the Ansi convention in code (PAnsiChar) or straight upgrade to a more modern W (Wide) variant that is full unicode capable on Windows systems.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11984
  • FPC developer.
Re: Windows WinExec('CALC.EXE', SW_SHOWNORMAL);
« Reply #8 on: September 19, 2023, 10:38:42 am »
I added a small section about winexec to the wiki about executing programs.

https://wiki.freepascal.org/Executing_External_Programs

1HuntMan: that is a good place to start with finding a replacement. Or just leave winexec if it is a short term project.

 

TinyPortal © 2005-2018