Recent

Author Topic: Got SIGSEGV exception when calling CreateProcess  (Read 2397 times)

speller

  • Newbie
  • Posts: 6
Got SIGSEGV exception when calling CreateProcess
« on: July 08, 2021, 09:09:50 am »
I'm stuck with the following code:

Code: Pascal  [Select][+][-]
  1. var
  2.   SI : STARTUPINFOW;
  3.   PI: TPROCESSINFORMATION;
  4. begin
  5.   FillChar(SI,SizeOf(SI),0);
  6.   SI.cb := SizeOf(SI);
  7.   FillChar(PI,SizeOf(PI),0);
  8.  
  9.   CreateProcessW (
  10.     nil,
  11.     'cmd.exe -v',
  12.     nil,
  13.     nil,
  14.     false,
  15.     0,
  16.     nil,
  17.     nil,
  18.     SI,
  19.     PI
  20.   );
  21. end;

It produces the following error:

Quote
Project anyfs raised exception class 'External: SIGSEGV'.

At address 7FF8D8FA800C

What I'm doing wrong?
« Last Edit: July 08, 2021, 09:20:51 am by speller »

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Got SIGSEGV exception when calling CreateProcess
« Reply #1 on: July 08, 2021, 09:30:18 am »
From https://docs.microsoft.com/de-de/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw
Quote
lpStartupInfo

A pointer to a STARTUPINFO or STARTUPINFOEX structure.

lpProcessInformation

A pointer to a PROCESS_INFORMATION structure that receives identification information about the new process.

I just look at the Source-code in
C:\lazarus\fpc\3.2.0\source\rtl\win\wininc\redef.inc
Code: [Select]
function CreateProcessW(lpApplicationName: LPWSTR; lpCommandLine: LPWSTR; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPWSTR;
  const lpStartupInfo: TStartupInfoW; var lpProcessInformation: TProcessInformation): BOOL; external 'kernel32' name 'CreateProcessW';   
This is not making any sense to me.
Mode Delphi?
« Last Edit: July 08, 2021, 09:37:37 am by Zvoni »
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

speller

  • Newbie
  • Posts: 6
Re: Got SIGSEGV exception when calling CreateProcess
« Reply #2 on: July 08, 2021, 09:33:34 am »
The declaration for the mentioned parameters is as follows:

Code: Pascal  [Select][+][-]
  1. function CreateProcessW(lpApplicationName: LPWSTR; lpCommandLine: LPWSTR; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPWSTR;
  2.   const lpStartupInfo: TStartupInfoW; var lpProcessInformation: TProcessInformation): BOOL; external 'kernel32' name 'CreateProcessW';
  3.  

const and var parameters are passed by reference, isn't it?

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Got SIGSEGV exception when calling CreateProcess
« Reply #3 on: July 08, 2021, 09:42:14 am »
const and var parameters are passed by reference, isn't it?
From: https://freepascal.org/docs-html/current/ref/refsu65.html#x181-20500014.4.2
Quote
The procedure gets a pointer to the variable that was passed, and uses this pointer to access the variable’s value.
Then what i said above is wrong

EDIT: Just took a closer look: You pass an empty StartupInfo-Record (except setting cb)
but that record has a member dwFlags, which the MS-Docs don't mention zero being a valid value
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow

That said: i'm poking in the dark.....
« Last Edit: July 08, 2021, 09:49:53 am by Zvoni »
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

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Got SIGSEGV exception when calling CreateProcess
« Reply #4 on: July 08, 2021, 01:06:33 pm »
Or use TProcess class and don't worry about it. its even cross the river floatable !  :)
The only true wisdom is knowing you know nothing

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: Got SIGSEGV exception when calling CreateProcess
« Reply #5 on: July 08, 2021, 01:13:46 pm »
Try to swap first two parameters:
Code: Pascal  [Select][+][-]
  1. CreateProcessW (
  2.     'cmd.exe -v',
  3.     nil,
  4.     nil,
  5.     nil,
  6.     false,
  7.     0,
  8.     nil,
  9.     nil,
  10.     SI,
  11.     PI
  12.   );

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Got SIGSEGV exception when calling CreateProcess
« Reply #6 on: July 08, 2021, 01:40:04 pm »
The idea behind redef is this:

Originally the windows headers were generated with VAR and CONST support for a more pascal feel, but this turned out to be a problem because some calls allowed to pass NIL to the parameters.

Most core functions were changed to pointer syntax only, but often used functions where either old FPC or Delphi had a VAR construct were overloaded in redef.inc.

IOW this means that there also is a version with only pointers in func.inc

 

TinyPortal © 2005-2018